欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql把主键定义为自动增长标识符类型

程序员文章站 2024-03-01 18:15:34
1、把主键定义为自动增长标识符类型 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如: create tabl...

1、把主键定义为自动增长标识符类型

在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:

create table customers(id int auto_increment primary key notnull, name varchar(15));

insert into customers(name) values("name1"),("name2");

一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。

在ms sqlserver中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:

create table customers(id int identity(1,1) primary key notnull, name varchar(15));

insert into customers(name) values("name1"),("name2");

select id from customers;

查询结果和mysql的一样。由此可见,一旦把id设为identity类型,mssqlserver数据库会自动按递增的方式为主键赋