数据库约束条件

约束条件

1、主键约束

  • 关键字:primary key
  • 特点:非空唯一
  • 添加:
    • 创建表时字段后跟 primary key

    • 添加表时添加使用:constraints pk_student_sno primary key(sno)

    • 表存在的情况下:
      添加:alter table 表名 添加(add)constraints 主键名 关键字(primary key(字段名))

      alter table student add constraints pk_student_sno primary key(sno)

      删除:alter table 表名 删除(drop) constraints 主键名
      alter table student drop constraints pk_student

2、非空约束

  • 关键字:not null()
  • 添加:
    • 创建表时字段后跟 not null
    • 添加表时添加使用:constraints ck_student_sname check(sname is not null)
      3、表存在的情况下:
      alter table 表名 添加(add)constraints 主键名 关键字(check(字段名 is not null))
      删除:alter table 表名 删除(drop) constraints 主键名
      alter table student drop constraints ck_student_sname

3、检查约束

  • 关键字:check
  • 添加:
    • 创建表时字段后跟check(要检查的条件)
    • 在创建表语句最后添加使用:constraints ck_student_sage check(sage>0 and sage < 200)
    • 表存在的情况下:
      alter table student add constraints ck_student_sage check(sage > 0 and sage < 200)

4、唯一约束

  • 关键字:unique
  • 添加:
    • 创建表时字段后跟unique
    • 再创建表时语句最后使用:constraints un_student_sex unique(sql)
    • 表存在的情况下:
      alter table student add constraints un_student_ssex unique(sqq)
create table student(
              sno number(10) primary key,
              sname varchar2(20) not null,
              sage number(3) check(sage>0 and sage<20),
              ssex char(4),
              sfav varchar2(500),
              sbirth date,
              sqq varchar(20),
              --constraints ck_student_sname check(sname is not null),
              --constraints ck_student_sage check(sage>0 and sage < 200)
              --constraints ck_student_sage check(ssex>'男' or ssex < '女')
              --constraints un_student_sex unique(sqq)
)

5、外键约束

  • 关键字:foreign key(本表字段名) references 要关联的表(字段)
  • 作用:当在字表中插入的数据在父表中不存在,将会报错
  • 概念:一张表的某个字段的值依赖于另一张表的某个字段的值,可以使用外键约束
    主动约束的表称为子表,被依赖的表称为父表
  • 添加:
    • 创建表时字段后跟references 要关联的表(字段)
    • 再创建表时语句最后使用:constraints 外键名 foregin key(本表字段) references 要关联的表(字段)
    • 表存在的情况下:
      alter table 表名 add constraints 外键名 foreign key(字段) references 要关联的表名(字段)
  • 删除外键:alter table 表名 drop constraints 外键约束名
  • 外键的缺点:
    • 无法直接删除父表数据,除非级联删除
      • 级联删除:在添加外键约束的时候 使用关键字,on delete cascade
        • 使用:当删除父表数据时,自动删除字表相关的的所有数据
        • 缺点:无法保留历史数据
      • 解决:在添加外键约束的时候,使用关键字,on delete set null
        • 删除父表数据时,将子表中的依赖字段值设置为null
          注:不可将依赖字段设置为非空
create table student(
	                      sno number(10) primary key,
	                      sname varchar(30) not null,
	                      sage number(3) check(sage >0 and sage < 200),
	                      ssex char(4) check(ssex='男' or ssex='女'),
	                      sfav varchar2(500),
	                      sqq varchar(30) unique,
	                      cno number(10) --references clazz(cno)
	                      --constraints  fk_student_cno foreign key(cno) references clazz(cno)
	        )
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值