DDR爱好者之家 Design By 杰米

实体完整性
1.建表时定义主键

  Create table 表名
   (
        Sno int identity(1,1),
        Sname nvarchar(20),
        --设置主键
        Primary key (Sno)
   )
 
2.添加主键

    alter table 表名
    add constraint PK_表名_Sno
    primary key(id)
参照完整性1.建表时定义外键

  create table 表名
  (
      sno int identity(1,1) primary key,
      cno int not null,
      foreign key(cno) References
      表名2(Cno)
      on Delete cascade     --级联删除
      on update cascade    --级联更新
      -- on delete on action  删除管制
  )
 
2.添加外键
   alter table 表名
   add constraint FK_表名_表名2
   Foreign key(cid) references 表名2(cid)
用户定义完整性1.非空约束
   alter table 表名
   alter column name varchar(20) not null
 
2.唯一约束
   alter table 表名
   add constraint UQ_表名_列名 unique(列)
 
3.检查约束
   alter table 表名
   add constraint CK_表名_列名 check(age>5)
 
4.默认约束
   alter table 表名
   add constraint DF_表名_列名 default('男')
   for gender
删除约束    --删除约束
   alter table 表名 drop constraint DF_表名_列

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米