On MySQL auto-increment field

1. Add auto-increment field

1.1 added when creating tables

1 create table emp(
2 empno int(5) auto_increment primary key
3 );

1.2 added after creating the table

create table emp(
ename varchar(20)
);
alter table emp add empno int(5) auto_increment primary key;

Note : in mysql auto-increment primary key field must, otherwise there will be about error

create table emp(
empno int(5) auto_increment 
);
0 33 15:20:54 create table emp( empno int(5) auto_increment ) Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key 0.000 sec

2. Set the starting value increment

Starting from the value in mysql increment field to 1, but sometimes we are not asking from the beginning increment, then we need to manually set.

2.1 setting when you create a table

1 create table emp(
2 empno int(5) auto_increment primary key 
3 )auto_increment=100;

2.1 modify the self-energizing start value after creating the table

alter table emp auto_increment=100;

Note: In the case of creating a table and then set a custom value, if it has been inserted before setting up the data, and then insert the data, the data in the database will not change, but doing so can cause data can not be inserted case, since the auto-increment primary key insertion possible the same as the previous value.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160195.htm