美文网首页
sql 主键自动增长 identity

sql 主键自动增长 identity

作者: Vergil_wj | 来源:发表于2021-06-26 09:00 被阅读0次

    主键自动增长

    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 李四

    相关文章

      网友评论

          本文标题:sql 主键自动增长 identity

          本文链接:https://www.haomeiwen.com/subject/avojyltx.html