ITPub博客

首页 > IT职业 > IT图书 > mysql把主键定义为自动增长标识符类型

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

IT图书 作者:chunshui 时间:2021-09-09 09:34:29 0 删除 编辑

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

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

?

123 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类型,数据库就会自动为主键赋值。例如:

?

12345 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数据库会自动按递增的方式为主键赋

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3527/viewspace-2804854/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: php 安装扩展
请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    98
  • 访问量
    1075