保护数据库

完整性

  1. 定义
  2. 分类:实体完整性、域完整性、用户定义完整性
  3. 创建约束(主键、外键)
    (1) 创建表的同时创建约束
    1) 主键primary key
    2) 外键foreign key
     父关键字在自己表中必须是主键;
     父子必须完全一样
    3) 唯一unique
    4) 默认值default
    举例
    create table student
    (sno char(4) primary key,
    sname char(20) not null,
    sageint,
    ssex char(2) default ‘男’);

create table sc
(sno char(4),
cno char(3),
gradeint,
constraintpk_sc primary key(sno,cno),
constraint fk1_sc foreign key(sno) references student(sno));
(2) 表已经存在,添加约束
1) 主键
alter table student
add primary key(sno);

alter table sc
add primary key(sno,cno);
2) 外键
alter table sc
add constraint fk1_sc foreign key(sno) references student(sno);
3) 唯一
alter table student
add constraint uni unique(sname);
4. 查看约束
show create table student;
5. 删除约束
1)主键
alter table student
drop primary key;
2)外键
alter table sc
drop foreign key fk1_sc;
3)唯一
alter table student
drop index uni;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值