Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-01 13:06:23
Original
888 people have browsed it

分享下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");
Copy after login

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

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

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template