Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a primary key start from 1000?

create table tablename (     id integer unsigned not null AUTO_INCREMENT,     ....     primary key id ); 

I need the primary key to start from 1000.

I'm using MySQL.

like image 384
user198729 Avatar asked Jan 25 '10 06:01

user198729


People also ask

How do you start a primary key from 1?

alter table yourTableName AUTO_INCREMENT=1; truncate table yourTableName; After doing the above two steps, you will get the primary key beginning from 1.

How do you start a primary key with 0?

Primary Key Can be Zero, but if you set Identity on the column it normally will start at 1 rather than Zero. Primary Key will have Identity Column .. (Most likely or Most Scenario's) .. So Can i assume, if there is an identity Column we can start the seed / increment by 0.

Does a primary key have to start at 1?

Not all primary keys have to start at 1, as in the case of an order number. If the PK's are starting from different ranges then it helps in testing reports.

How do you make a primary key auto generated?

AUTO INCREMENT FieldAuto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.


2 Answers

If your table has already been created with an auto-increment. so you can use

ALTER TABLE tbl AUTO_INCREMENT = 1000; 

otherwise put the AUTO_INCREMENT = 1000; in your CREATE TABLE

it goes before the final );

like image 98
davidosomething Avatar answered Sep 24 '22 08:09

davidosomething


You can use ALTER TABLE to accomplish this:

ALTER TABLE tablename AUTO_INCREMENT = 1000; 

If you want it as part of the CREATE TABLE statement, just put it after the table definition:

CREATE TABLE tablename (   ... ) ENGINE=InnoDB AUTO_INCREMENT=1000; 
like image 38
zombat Avatar answered Sep 25 '22 08:09

zombat