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
106
votes
8 answers

How to retrieve the last autoincremented ID from a SQLite table?

I have a table Messages with columns ID (primary key, autoincrement) and Content (text). I have a table Users with columns username (primary key, text) and Hash. A message is sent by one Sender (user) to many recipients (user) and a recipient (user)…
Dabblernl
  • 15,831
  • 18
  • 96
  • 148
104
votes
4 answers

How to generate auto increment field in select query

For example I have a table with 2 columns, first_name and last_name with these values Ali Khani Elizabette Amini Britney Spears ,... I want to write a select query that generate a table like this: 1 Ali Khani 2 …
wf Khani
  • 1,055
  • 2
  • 7
  • 5
101
votes
12 answers

How to add AUTO_INCREMENT to an existing column?

How do I add auto_increment to an existing column of a MySQL table?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
99
votes
7 answers

Hibernate Auto Increment ID

I have a j2ee application using hibernate with annotation. How do I annotate the Id field in my pojo class to set it as auto increment or auto generated. and in adding the bean do I leave that field in my bean null?
cedric
  • 3,107
  • 15
  • 54
  • 65
98
votes
4 answers

How to make MySQL table primary key auto increment with some prefix

I have table like this table id Varchar(45) NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(30) NOT NULL, I want to increment my id field like 'LHPL001','LHPL002','LHPL003'... etc. What should I have to do for that? Please let me know any possible…
Vijay
  • 8,131
  • 11
  • 43
  • 69
96
votes
5 answers

mysqldump - Export structure only without autoincrement

I have a MySQL database and I am trying to find a way to export its structure only, without the auto increment values. mysqldump --no-data would almost do the job, but it keeps the auto_increment values. Is there any way to do it without using…
Paris
  • 6,323
  • 7
  • 31
  • 49
92
votes
18 answers

Auto Increment after delete in MySQL

I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I've noticed people with the same problem and with varied answers. Some recommend not using this feature, others state it can't be 'fixed'. I…
OmidTahouri
  • 1,312
  • 2
  • 13
  • 19
90
votes
11 answers

MySQL AUTO_INCREMENT does not ROLLBACK

I'm using MySQL's AUTO_INCREMENT field and InnoDB to support transactions. I noticed when I rollback the transaction, the AUTO_INCREMENT field is not rollbacked? I found out that it was designed this way but are there any workarounds to this?
codegy
  • 2,259
  • 4
  • 25
  • 24
87
votes
4 answers

postgres autoincrement not updated on explicit id inserts

I have the following table in postgres: CREATE TABLE "test" ( "id" serial NOT NULL PRIMARY KEY, "value" text ) I am doing following insertions: insert into test (id, value) values (1, 'alpha') insert into test (id, value) values (2,…
jerrymouse
  • 16,964
  • 16
  • 76
  • 97
82
votes
4 answers

How to force MySQL to take 0 as a valid auto-increment value

Long story short, I have a SQL file that I want to import as a skel style file, so this will be done repeatedly, programmatically. I can edit the SQL file however I want, but I'd rather not touch the application itself. This application uses userid…
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
78
votes
5 answers

auto increment ID in H2 database

Is there a way to have an auto_incrementing BIGINT ID for a table. It can be defined like so id bigint auto_increment but that has no effect (it does not increment automatically). I would like to insert all fields but the ID field - the ID field…
eriq
  • 1,524
  • 3
  • 13
  • 22
78
votes
13 answers

How to autoincrement versionCode in Android Gradle

I'm experimenting with new Android build system based on Gradle and I'm thinking, what is the best way to autoincrease versionCode with it. I am thinking about two options create versionCode file, read number from it, increase it and write it back…
sealskej
  • 7,281
  • 12
  • 53
  • 64
77
votes
6 answers

What happens when auto_increment on integer column reaches the max_value in databases?

I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than…
Jonas
  • 121,568
  • 97
  • 310
  • 388
73
votes
10 answers

What is the difference between "++" and "+= 1 " operators?

In a loop in C++, I usually encounter situations to use ++ or +=1, but I can't tell their difference. For instance, if I have an integer int num = 0; and then in a loop I do: num ++; or num += 1; they both increase the value of num, but what is…
E_learner
  • 3,512
  • 14
  • 57
  • 88
69
votes
2 answers

What is the biggest ID number that autoincrement can produce in mysql

I have an database that is rapidly filled with data we talk about 10-20k rows per day. What is a limit of an ID with and autoincrement option? If ID is created as INTEGER then I can do max value of 2,147,483,647 for unsigned values? But what when…
user123_456
  • 5,635
  • 26
  • 84
  • 140