SQL-基础 约束(添加-删除外键)

一.概念

  1. 约束:作用于表中字段上的规则,用于限制存储在表中的数据
  2. 目的:保证数据库中数据的正确,有效性和完整性。
  3. 分类:非空约束(not null),唯一约束(unique),主键约束(primary key),默认约束(default),检查约束(check),外键约束(foreign key)。

二.运用

创建约束表:(也可以直接在创建新表目录,选择创建约束,不需要写代码)

create table user(
    id int primary key auto_increment comment '主键',
    name varchar(10) not null unique comment '姓名',
    age int check ( age > 0 && age <= 100 ) comment '年龄',
    status char(1) default '1' comment '状态',
    gender char(1) comment '性别'
) comment '用户表';

约束条件:

插入数据:(插入的数据一定要在约束条件内,否则报错)

insert into user(name,age,status,gender) values ('是1',99,'1','男'),('是二',22,'1','女');

三.外键约束

概念:用来让两张表的数据之间建立连接,保证数据的一致性和完整性

语法:

create table 表名(
       字段名 数据类型,

      ....
       [constraint] [外键名称] foreign key(外键字段名) references 主表(主表列名)

);

准备副表数据:

create table dept(
    id int auto_increment comment 'ID' primary key ,
    name varchar(50) not null comment '部门名称'
)comment '部门表';
insert into dept (id, name) values (1, '研发部'), (2, '市场部'), (3, '财务部'), (4, '销售部'), (5, '技术部');

运行结果:

主表数据:

create table emp2(
    id int auto_increment comment 'ID' primary key ,
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导ID',
    dept_id int comment '部门ID'
)comment '员工表';

结果:

插入员工数据:

insert into emp2 (id, name, age, job,salary, entrydate, managerid, dept_id) VALUES
                            (1, '谢谢', 26, '董事长',30000, '2004-02-08', null,5),
                        (2, '孙悟空', 56, '项目经理',20000, '2007-07-08', 1,1),
                        (3, '猪八戒', 43, '开发',15000, '2012-05-12', 2,1),
                        (4, '沙和尚', 38, '开发',17000, '2014-03-08', 2,1),
                        (5, '唐僧', 57, '总经理',25000, '2016-02-12', 3,1),
                        (6, '飞天', 67, '财务',11000, '2018-02-08', 2,1);

添加外键(注意表名的不同)

--添加外键

alter table emp2 add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);

删除外键

--删除外键

alter table emp2 drop foreign key fk_emp_dept_id ;

外键的删除和更新行为:

  1. no action---当父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有不允许删除/更新
  2. restrict---当父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有不允许删除/更新
  3. cascade----当父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有,则也删除/更新外键在子表中的记录
  4. set nll----当父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null
  5. set default---父表有变更时,子表将外键列设置成一个默认的值

演示cascade行为(也可以在表中点击modify table,shan删除/添加外键操作)

--外键的删除和更新

alter table emp2 add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade ;

  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小菜机一枚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值