主键自动增长
create table student
(
student_id int primary key identity(100,5),
student_name nvarchar(20) not null
)
- identity:主键自动增长,默认从1开始,每次加1。也可指定起始值 identidy(100,5),从 100 开始每次增长5。
插入值:
insert into student(student_name) values('张三') -- ok
insert into student values ('李四') -- ok
- 指定主键自增长
identidy
后,插入数据时,可直接使用第二种方式,不用再写出每一列的字段名。如果没有指定自增长,则需要像第一种写法那样还需要写出所有的字段名。
编号 | 姓名 |
---|---|
100 | 张三 |
105 | 李四 |
删除值
delete student3 where student_name = '李四'
insert into student values ('李四') -- ok
- 表中删除数据又插入数据会导致主键不连续递增。
编号 | 姓名 |
---|---|
100 | 张三 |
110 | 李四 |
网友评论