Questions tagged [auto-increment]

Numerical sequence generation for automatic key creation

An attribute of an integer column in a table which allows a Database Server Process to automatically generate a new sequence number every time a new row is created.

You define it in MySQL with

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

You define it in MS SQL Server

id INT PRIMARY KEY IDENTITY,

Oracle has a mechanized object called a SEQUENCE

You would create the SEQUENCE object first

CREATE SEQUENCE mysequence
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;

Then, you call for the next value attribute of the SEQUENCE like this:

INSERT INTO mytable (id,column1,column2, ...)
VALUES (mysequence.nextval,'value1','value2', ...)l
196 questions
41
votes
2 answers

Is MySql's LAST_INSERT_ID() function guaranteed to be correct?

When I do a single row INSERT to a table that has an AUTO_INCREMENT column I'd like to use the LAST_INSERT_ID() function to return the new AUTO_INCREMENT'ed value stored for that row. As many Microsoft SQL Server devs and admins no doubt are aware…
Kev
  • 1,336
  • 5
  • 14
  • 24
26
votes
3 answers

Postgres: How to insert row with autoincrement id

There is a Table "context". There is an autoincrement id "context_id". I am using sequence to retrieve the next value. SELECT nextval('context_context_id_seq') The result is: 1, 2, 3,...20.... But there are 24780 rows in the "context" table How can…
user3631472
  • 361
  • 1
  • 3
  • 3
25
votes
3 answers

How can I reset a mysql table auto-increment to 1 in phpMyAdmin?

I know that in MySQL at the command line I can reset a table's auto-increment field to 1 with this: ALTER TABLE tablename AUTO_INCREMENT = 1 I am curious if there is a way to do this from within phpMyAdmin. Something like a check box to reset the…
BitBug
  • 355
  • 1
  • 5
  • 9
25
votes
3 answers

How do I specify that a column should be auto-incremented in pgAdmin?

I have started to learn pgAdmin III to manage a PostgreSQL database. But it wasn't an easy to use application. If I create or have created a table with pgAdmin III, how can I add "auto-increment"-functionality on a column id that has type integer?
Jonas
  • 33,945
  • 27
  • 62
  • 64
20
votes
1 answer

Fixing table structure to avoid `Error: duplicate key value violates unique constraint`

I have a table which is created this way: -- -- Table: #__content -- CREATE TABLE "jos_content" ( "id" serial NOT NULL, "asset_id" bigint DEFAULT 0 NOT NULL, ... "xreference" varchar(50) DEFAULT '' NOT NULL, PRIMARY KEY ("id") ); Later…
18
votes
3 answers

Reset IDENTITY value

I have a table with an IDENTITY column. While developing I delete the rows from time to time and add them again. But the IDENTITY values always kept increasing and didn't start from 1 when I added them again. Now my id's go from 68 -> 92 and this…
Gijs
  • 333
  • 1
  • 2
  • 7
17
votes
8 answers

Reasons for avoiding large ID values

We are working on a web application, not yet accessible to users. My boss noticed that newly created records get an ID of over 10 000, even though we only have under 100 records in the table. She assumed that the web interface for some reason…
rumtscho
  • 271
  • 2
  • 7
17
votes
2 answers

How are auto_increment keys handled in INSERT (SELECT * FROM...)

I have table1 and table2 in MySQL. Both have a primary auto_increment key id. If the table schemas match and I do INSERT INTO table1 (SELECT * FROM table2) what happens with regards to the new rows inserted in to table1? Do they keep their old id…
Thomas Johnson
  • 481
  • 2
  • 5
  • 13
17
votes
3 answers

Why does auto increment jumps by more than the number of rows inserted?

I am very perturbed by this weird behaviour I am seeing in the auto_increment value recorded in the bidID of a Bids table after performing bulk insertion using a stored procedure: INSERT INTO Bids (itemID, buyerID, bidPrice) SELECT itemID,…
Question Overflow
  • 1,009
  • 6
  • 16
  • 21
17
votes
1 answer

Autoincrement primary key in CREATE TABLE ... AS SELECT

I created table using a complicated select query via CREATE TABLE ... AS SELECT.... How can I add an autoincrement primary key in this query? For example: create table `user_mv` select `user`.`firstname` as `firstname`, `user`.`lastname` as…
Arash Mousavi
  • 673
  • 2
  • 12
  • 21
16
votes
5 answers

Add autoincrement to existing PK

I created a table in a DB that already exists in another DB. It was initially populated with the old DB data. The table's PK had to receive the values that already exist on those records, so it couldn't be autoincrement. Now I need the new table to…
Hikari
  • 1,603
  • 12
  • 28
  • 42
13
votes
5 answers

MySQL: Why is auto_increment limited to just primary keys?

I know MySQL limits auto_increment columns to primary keys. Why is this? My first thought is that it's a performance restriction, since there probably is some counter table somewhere that must be locked in order to get this value. Why can't I have…
12
votes
4 answers

Identity column re-seed: when it is necessary?

During one of the last lessons at university (I'm a student), lecturer asked us to develop a database (MySQL Server if it matters) and tiny client app that would consume the database as data source. One of requirements was that the identity column…
Crypt32
  • 223
  • 2
  • 8
9
votes
2 answers

Randomizing the Auto Increment in Mysql

We have a database, where we insert data into table whenever we receive an order from our services. We use auto_increment to generate the ID and assign the order with the same. This is usually same everywhere. But the problem is, since this…
RaceBase
  • 203
  • 1
  • 2
  • 7
9
votes
2 answers

Create a "INTO" table with primary key

Maybe for this community my problem is easy, but for me (a simple Java programmer) it's a BIG problem. I have a Big DB with more and more data. So, the external db admin had create a job that show me in a temporary table the data that I need. But he…
PaolaG
  • 193
  • 1
  • 6
1
2 3
13 14