Questions tagged [auto-increment]

a database constraint that automatically increases from the last record when making an INSERT

AUTO_INCREMENT is a flag set on a field in a database table that forces the RDBMS to create a unique identifier for the record. This is useful when no other obvious primary key field is apparent in a record.

Useful questions

Related tags

2677 questions
65
votes
5 answers

Is there a way to retrieve the autoincrement ID from a prepared statement

Is there a way to retrieve the auto generated key from a DB query when using a java query with prepared statements. For example, I know AutoGeneratedKeys can work as follows. stmt = conn.createStatement(); stmt.executeUpdate(sql,…
jW.
  • 9,280
  • 12
  • 46
  • 50
63
votes
5 answers

How to insert a record into a table with a column declared with the SERIAL function

My database is using PostgreSQL. One table is using the serial auto-increment macro. If I want to insert a record into the table, do I still need to specify that value, or it is be automatically assigned for me? CREATE TABLE dataset ( id serial…
AntiGMO
  • 1,535
  • 5
  • 23
  • 38
62
votes
7 answers

make an ID in a mysql table auto_increment (after the fact)

I acquired a database from another developer. He didn't use auto_incrementers on any tables. They all have primary key ID's, but he did all the incrementing manually, in code. Can I turn those into Auto_incrementers now? Wow, very nice, thanks a…
Gene R
  • 1,555
  • 5
  • 19
  • 32
61
votes
6 answers

How do I add a auto_increment primary key in SQL Server database?

I have a table set up that currently has no primary key. All I need to do is add a primary key, no null, auto_increment. I'm working with a Microsoft SQL Server database. I understand that it can't be done in a single command but every command I try…
dcp3450
  • 10,959
  • 23
  • 58
  • 110
60
votes
5 answers

ROWID INTEGER PRIMARY KEY AUTOINCREMENT - How to insert values?

I created an SQLite table in Java: create table participants (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, col1,col2); I tried to add rows : insert into participants values ("bla","blub"); Error: java.sql.SQLException: table participants has 3…
Anthea
  • 3,741
  • 5
  • 40
  • 64
59
votes
3 answers

MySQL: bigint Vs int

I have been using int(10) and just noticed that Wordpress uses bigint(20) - What is different to use bigint(20) and int(10) for id auto increment? Which one should I use for id column? `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, Vs `id`…
Run
  • 54,938
  • 169
  • 450
  • 748
59
votes
17 answers

Mongoose auto increment

According to this mongodb article it is possible to auto increment a field and I would like the use the counters collection way. The problem with that example is that I don't have thousands of people typing the data in the database using the mongo…
HMR
  • 37,593
  • 24
  • 91
  • 160
54
votes
5 answers

currval has not yet been defined this session, how to get multi-session sequences?

My objective is to get a primary key field automatically inserted when inserting new row in the table. How to get a sequence going from session to session in PostgreSQL? doubleemploi@hanbei:/home/yves$ psql -d test Mot de passe : psql…
MUY Belgium
  • 2,330
  • 4
  • 30
  • 46
53
votes
4 answers

MSSQL Select statement with incremental integer column... not from a table

I need, if possible, a t-sql query that, returning the values from an arbitrary table, also returns a incremental integer column with value = 1 for the first row, 2 for the second, and so on. This column does not actually resides in any table, and…
Rodrigo
  • 4,365
  • 3
  • 31
  • 49
53
votes
4 answers

MySQL: Auto increment temporary column in select statement

How do I create and auto increment a temporary column in my select statement with MySQL? Here is what I have so far: SET @cnt = 0; SELECT (@cnt =@cnt + 1) AS rowNumber, rowID FROM myTable WHERE CategoryID = 1 Which returns:…
Sg1456
  • 954
  • 2
  • 10
  • 17
52
votes
4 answers

Why does MySQL autoincrement increase on failed inserts?

A co-worker just made me aware of a very strange MySQL behavior. Assuming you have a table with an auto_increment field and another field that is set to unique (e.g. a username-field). When trying to insert a row with a username thats already in the…
Sorcy
  • 2,587
  • 5
  • 26
  • 34
52
votes
5 answers

PHP MYSQL - Insert into without using column names but with autoincrement field

I need to insert a long row with 32 fields into a MySQL table. I'd like to do something like this: $sql="insert into tblname values (... 32 fields ...)"; Obviously, it works fine if the fields are in the same order as the MySQL table fields. But,…
Paulo Bueno
  • 2,499
  • 6
  • 42
  • 68
51
votes
9 answers

Auto increment in MongoDB to store sequence of Unique User ID

I am making a analytics system, the API call would provide a Unique User ID, but it's not in sequence and too sparse. I need to give each Unique User ID an auto increment id to mark a analytics datapoint in a bitarray/bitset. So the first user…
est
  • 11,429
  • 14
  • 70
  • 118
51
votes
5 answers

PHP mySQL - Insert new record into table with auto-increment on primary key

Wondering if there is a shorthand version to insert a new record into a table that has the primary key enabled? (i.e. not having to include the key column in the query) Lets say the key column is called ID, and the other columns are Fname, Lname,…
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
51
votes
3 answers

How to insert new row to database with AUTO_INCREMENT column without specifying column names?

I have a table with the following columns: id - INT UNSIGNED AUTO_INCREMENT name - VARCHAR(20) group - VARCHAR(20) I know that I can add a row like this: INSERT INTO table_name (name, group) VALUES ('my name', 'my group') I wonder if there is a…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746