MySQL学习(三)——约束、多表关系以及范式详解

256 阅读5分钟

「这是我参与2022首次更文挑战的第4天,活动详情查看:2022首次更文挑战」。

前言

 大家好,我是程序猿小白 GW_gw,很高兴能和大家一起学习进步。

以下内容部分来自于网络,如有侵权,请联系我删除,本文仅用于学习交流,不用作任何商业用途。

摘要

 本文主要介绍Mysql中约束、多表关系以及范式基本概念和使用。

1. 约束

为了保证在向数据库中添加有效的数据,mysql数据库提供了6种约束,用来保证数据库中数据的准确和可靠性,保证数据的实体完整性。

  1. 非空约束
  2. 唯一约束
  3. 主键约束
  4. 默认值约束
  5. 外键约束
  6. 检查约束

也有人说是5类,因为有人说检查约束check不起作用,然后我就试了一下,发现确实不行,那为什么别人有的可以呢。经过查看官方文档,发现在MySQL 8.0.16之前mysql只会解析check,但是不会执行,也就是说就是写了也没用。但是从8.0.16开始,check就可以生效了。

官方文档链接: dev.mysql.com/doc/refman/…

1.1 非空约束

非空约束not null:列的取值不能为null。

【实例展示】

create table stu(
    id int not null,
    name varchar(32)
);
  1. 删除非空约束
alert table stu modify id int;
  1. 添加非空约束
alert table stu modify id int not null;

1.2 唯一约束

唯一约束unique:列的取值不能重复,即对于该列来说,值具有唯一性。

【实例展示】

//值不能重复,null认为不是重复的
create table stu(
    id int unique,
    name varchar(32)
);
  1. 删除唯一约束
alert table stu drop index id;
  1. 添加唯一约束
alert table stu modify id int unique;

1.3 主键约束

主键约束primary key:主键约束相当于非空约束和唯一约束的合并。即该列的值唯一且不为null。

【实例展示】

//primary key非空且唯一
create table stu(
    id int primary key,
    name varchar(32)
);
  1. 删除主键约束
alter table stu drop primary key;
  1. 添加主键约束
alert table stu modify id int primary key;

1.4 默认值约束

默认值约束default:当该列插入时没有值,则用默认值代替。

【实例展示】

create table stu(
    id int primary key,
    age int default 20
);
  1. 删除默认值约束
1. alter table 表名 modify 列名 列的数据类型(长度)
2. alter table 表名 alter column 列名 drop default

通过第一种删除时,如果插入数据时该列没有值,则填入null,而第二种如果不填写值则会报错。

  1. 添加默认值约束
alter table 表名 modify 列名 列的数据类型(长度) default 默认值

1.5 外键约束

外键约束foreign key:常用于关联两个表。

【实例展示】

//constraint 外键约束名 foreign key (作为外键的列) references 对应的表(对应的作为外键的列名)
create table stu(
    id int,
    age int,
    cid int,
    constraint sc_fk foreign key (cid) references c(id)
);

  1. 删除外键
alter table stu drop foreign key 外键约束名;
  1. 添加外键
alter table stu add constraint 外键约束名 foreign key (cid) references c(id);

1.6 检查约束

检查约束check:

【实例展示】

create table stu(
    id int primary key,
    age int 
    check(age>18)
);
  1. 删除检查约束
alter table 表名 drop constraint 检查约束名
  1. 添加检查约束
alter table 表名 modify add constraint 检查约束名 check(检查约束)

2. 级联操作

有时我们会遇到,需要删除一个人的所有信息的情况,例如员工离职,该员工的所有信息都会被删除,如果我们一张表一张表的删除未免太麻烦,这时我们就需要使用级联操作cascade,这时如果我们删除了主表,那么其他从表中的内容也会被删除。

【实例展示】

//级联更新
create table stu(
    id int,
    age int,
    cid int,
    constraint sc_fk foreign key (cid) references c(id) on update cascade
);
​
//级联删除
create table stu(
    id int,
    age int,
    cid int,
    constraint sc_fk foreign key (cid) references c(id) on d cascade
);
​

3. 主键自增

当我们给主键设置了自增时,我们就不需要再手动的为主键进行赋值,并且不需要再考虑重复的问题,给了我们很大的方便。

【实例展示】

//auto_increment
create table stu(
    id int primary key auto_increment,
    name varchar(32)
);
  1. 删除自动增长
alert table stu modify id int;
  1. 添加自动增长
alert table stu modify id int auto_increment;

4. 多表关系

  1. 一对多

    1. 在多的一方建立外键,指向一的一方的主键
  2. 多对多

    1. 借助第三张中间表来完成。中间表至少包含两个字段,这两个字段作为第三张表的外键,分别指向两张表的主键。
  3. 一对一

    1. 在任意一方添加外键,指向另一方的主键。

5. 范式

  1. 第一范式:每一列不可分割。
  2. 第二范式:消除非主属性对主属性的部分函数依赖。
  3. 第三范式:消除非主属性对主属性的传递函数依赖。

小结

以上就是关于mysql约束、多表关系和范式的一些基本介绍,其中check约束的问题需要我们稍微关注一下,避免因为版本问题而不生效的。希望以上内容能对读者有所帮助,如有不正之处,欢迎留言指正。