- 5
- 0
- 约1.9千字
- 约 3页
- 2020-09-01 发布于江苏
- 举报
几个常见数据库关于主键自动增加的设置
1、把主键定义为自动增长标识符类型?在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:create table customers(id int auto_increment primary key not null, name varchar(15));insert into customers(name) values("name1),("name2");select id from customers;?以上sql语句先创建了customers表,然后插入两条记录,在插入时仅仅设定了name字段的值。最后查询表中id字段,查询结果为:?id?12由此可见,一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。?在MS SQLServer中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:create table customers(id int identity(1,1) primary key not null, name varchar(15));insert into customers(name) values(name1),("name2");?select id from customers;查询
原创力文档

文档评论(0)