分享下mysql中如何把主键定义为自动增长标识符类型。

1、把主键定义为自动增长标识符类型
在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:

create table customers(id int auto_increment primary key notnull, name varchar(15));
insert into customers(name) values("name1"),("name2");

mysql数据库会自动按递增的方式为主键赋值。

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

create table customers(id int identity(1,1) primary key notnull, name varchar(15));
-- www.jbxue.com
insert into customers(name) values("name1"),("name2");
select id from customers;

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

相关文章:

  • 2022-12-23
  • 2021-12-10
  • 2021-07-14
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-01-02
  • 2021-12-09
  • 2022-12-23
相关资源
相似解决方案