【MySQL】5.MySQL表的约束


英语汇总

composite_primary_key 符合主键
course                课程
auto_increment        自增长
unique                唯一性
foreign_key           外键
references            关联

表的约束

空属性

  • 两个值:null (默认) 和 not null
  • 数据库默认字段基本都是字段为空,但是在实际使用过程中许愿哦尽可能保证字段不为空,因为数据为空没有办法参与运算
  • 所以我们在设计数据库表的时候,一定要在表中进行限制,满足上面条件的数据就不能插入到表中。这就是“约束”。
MariaDB [table_constraint]> create table if not exists null_test(
    -> id int,
    -> name varchar(16) not null
    -> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [table_constraint]> desc null_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(16) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
#可以看到表中Null列下name被设置成了NO
2 rows in set (0.00 sec)
MariaDB [table_constraint]> insert into null_test (id, name) values ('1', '张三');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into null_test (id) values ('2');
 #出现了一个warning提示,这是因为name 属性被设置成了not null,而我们插入记录时并未设置name的值 
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [table_constraint]> insert into null_test (name) values ('王五');
Query OK, 1 row affected (0.01 sec)

MariaDB [table_constraint]> select * from test_table;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 张三   |
|    2 |        |     #若字段属性为not null, 系统生成一个空串填入
| NULL | 王五   |     #若字段属性为null, 若没有数据填入直接为NULL 
+------+--------+
3 rows in set (0.00 sec)

标准的MySQL和MariaDB对NULL属性的处理有所不同,若是在标准MySQL中插入一条记录,其中一个字段为空,但是其属性设置为NOT NULL则会直接报错,但是在MariaDB中,数据库会自动生成一个默认值帮你填入,并且只会报一个警告

默认值 ( default )

默认值:某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值。

MariaDB [table_constraint]> create table if not exists default_test( 
    -> name varchar(16) not null,
    -> sex char(1) default '男'      #将性别默认设置成男
    -> );
Query OK, 0 rows affected (0.00 sec)

MariaDB [table_constraint]> desc default_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(16) | NO   |     | NULL    |       |
| sex   | char(1)     | YES  |     | 男      |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#默认值生效:数据在插入的时候不给该字段进行赋值,就会使用默认值
#只有设置了default的列,才可以在插入值的时候使用默认值
MariaDB [table_constraint]> insert into default_test (name) values ('张三'); 
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> select * from default_test;
+--------+------+
| name   | sex  |
+--------+------+
| 张三   | 男   |
+--------+------+
1 row in set (0.00 sec)

列描述

列描述:comment,没有实际含义,专门用来描述字段,会根据表创建语句保存,便于维护。

MariaDB [table_constraint]> create table if not exists comment_test(
    -> name varchar(16) not null comment '用户的姓名',
    -> age int not null comment '用户的年龄');
Query OK, 0 rows affected (0.01 sec)

MariaDB [table_constraint]> desc comment_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(16) | NO   |     | NULL    |       |
| age   | int(11)     | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#我们可以使用show create table 语句查看表创建时的信息
MariaDB [table_constraint]> show create table comment_test \G;
*************************** 1. row ***************************
       Table: comment_test
Create Table: CREATE TABLE `comment_test` (
  `name` varchar(16) COLLATE utf8_unicode_ci NOT NULL COMMENT '用户的姓名',
  `age` int(11) NOT NULL COMMENT '用户的年龄'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

ERROR: No query specified

zerofill

用于填充字段的数据

MariaDB [table_constraint]> create table if not exists zerofill_test( name varchar(16) not null, a int(5) zerofill not null, b int(5) not null);
Query OK, 0 rows affected (0.03 sec)

MariaDB [table_constraint]> desc zerofill_test;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type                     | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| name  | varchar(16)              | NO   |     | NULL    |       |
| a     | int(5) unsigned zerofill | NO   |     | NULL    |       |
| b     | int(5)                   | NO   |     | NULL    |       |
+-------+--------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MariaDB [table_constraint]> insert into zerofill_test (name, a, b) values ('张三', 222, 222);
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into zerofill_test (name, a, b) values ('张三', 222222, 222222);
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> select * from zerofill_test;
+--------+--------+--------+
| name   | a      | b      |
+--------+--------+--------+
| 张三   |  00222 |    222 |
| 张三   | 222222 | 222222 |
+--------+--------+--------+
2 rows in set (0.00 sec)
MariaDB [table_constraint]> select a, hex(a) from zerofill_test;
+--------+--------+
| a      | hex(a) |
+--------+--------+
|  00222 | DE     |
| 222222 | 3640E  |
+--------+--------+
2 rows in set (0.00 sec)
#可以看出数据库内部存储的还是1,00001只是设置了zerofill属性后的一种格式化输出而已

zerofill_testa, b两个字段使用的类型分别是 int(5) unsigned zerofill, int(5) 在插入相同的数据时,若数据不足五位,则a字段中会使用填充至五位,而b将直接显示。若超过五位则两者显示的都是一样的

给一个字段添加zerofill属性后,会自带增加 unsigned属性,而类似int(5)括号中的数字代表显示的宽度,若没有五位则添加了zero fill属性后就会使用0填充至五位

主键 (primary key)

primary key 用来唯一约束该字段里面的数据,不能重复也不能为空,一张表最多只能有一个主键,并且主键所在的列通常是整数类型

MariaDB [table_constraint]> create table if not exists primary_key_test(
    -> id int unsigned primary key comment '这是主键',
    -> name varchar(16) not null);
Query OK, 0 rows affected (0.01 sec)

MariaDB [table_constraint]> desc primary_key_test;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI | NULL    |       |
| name  | varchar(16)      | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [table_constraint]> insert into primary_key_test (id, name) values (1, '张三');
Query OK, 1 row affected (0.00 sec)
#使用主键字段中已经存在的数据,报错
MariaDB [table_constraint]> insert into primary_key_test (id, name) values (1, '李四');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
#对主键字段进行省略,在MariaDB中报warning,并将主键字段填充默认值
MariaDB [table_constraint]> insert into primary_key_test (name) values ('李四');
Query OK, 1 row affected, 1 warning (0.00 sec)
#再次对主键字段进行省略,因为默认值在主键字段中已经存在,所以重复报错
MariaDB [table_constraint]> insert into primary_key_test (name) values ('王五');
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
MariaDB [table_constraint]> select * from primary_key_test;
+----+--------+
| id | name   |
+----+--------+
|  0 | 李四   |
|  1 | 张三   |
+----+--------+
2 rows in set (0.00 sec)

当一个字段被设置成了主键字段则其默认附带了not null 的属性,想要插入必须要带有属性字段的数据

删除主键约束: alter table primary_key_test drop primary key;

MariaDB [table_constraint]> desc primary_key_test;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI | NULL    |       |
| name  | varchar(16)      | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [table_constraint]> alter table primary_key_test drop primary key;
Query OK, 2 rows affected (0.02 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [table_constraint]> desc primary_key_test;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   |     | NULL    |       |
| name  | varchar(16)      | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

添加主键约束:alter table primary_key_test add primary key(id);

若此时表中id列存在重复的 id 则会报错

复合主键

我们可以将一张表中的两个甚至是多个字段复合创建复合主键,复合主键中部分字段数据重复并不报错,只有所有字段完全相同才会导致主键数据重复报错

MariaDB [table_constraint]> create table if not exists composite_primary_key_test(
    -> id int unsigned,
    -> course char(10) comment '课程编号',
    -> score tinyint unsigned default 60 comment '默认得分',
    -> primary key(id, course));
Query OK, 0 rows affected (0.01 sec)

MariaDB [table_constraint]> desc composite_primary_key_test;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| id     | int(10) unsigned    | NO   | PRI | 0       |       |
| course | char(10)            | NO   | PRI |         |       |
| score  | tinyint(3) unsigned | YES  |     | 60      |       |
+--------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [table_constraint]> insert into composite_primary_key_test (id, course, score) values (1, '1111', 60);
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into composite_primary_key_test (id, course, score) values (1, '2222', 60);
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into composite_primary_key_test (id, course, score) values (2, '1111', 60);
Query OK, 1 row affected (0.01 sec)
#主键字段完全相同导致报错
MariaDB [table_constraint]> insert into composite_primary_key_test (id, course, score) values (1, '1111', 60);
ERROR 1062 (23000): Duplicate entry '1-1111' for key 'PRIMARY'


MariaDB [table_constraint]> select * from composite_primary_key_test;
+----+--------+-------+
| id | course | score |
+----+--------+-------+
|  1 | 1111   |    60 |
|  1 | 2222   |    60 |
|  2 | 1111   |    60 |
+----+--------+-------+
3 rows in set (0.00 sec)

自增长( auto_increment )

auto_increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。通常和主键搭配使用,作为逻辑主键

特点

  • 任何字段要做自增长前提本身就是一个索引
  • 自增长字段必须是一个整数
  • 一张表最多只能有一个自增长字段
MariaDB [table_constraint]> create table if not exists auto_increment_test(
    -> id int unsigned primary key auto_increment comment '自增主键(索引)',
    -> name varchar(20) not null);
Query OK, 0 rows affected (0.01 sec)
MariaDB [table_constraint]> desc auto_increment_test;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)      | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
MariaDB [table_constraint]> alter table auto_increment rename to auto_increment_test;
Query OK, 0 rows affected (0.01 sec)

MariaDB [table_constraint]> insert into auto_increment_test (name) values ('张三');
Query OK, 1 row affected (0.01 sec)

MariaDB [table_constraint]> insert into auto_increment_test (name) values ('李四');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into auto_increment_test (name) values ('王五');
Query OK, 1 row affected (0.01 sec)
MariaDB [table_constraint]> select * from auto_increment_test;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
|  3 | 王五   |
+----+--------+
3 rows in set (0.00 sec)
MariaDB [table_constraint]> insert into auto_increment_test (id, name) values (10, '赵六');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into auto_increment_test (name) values ('宋七');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> select * from auto_increment_test;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
|  3 | 王五   |
| 10 | 赵六   |
| 11 | 宋七   |
+----+--------+
5 rows in set (0.00 sec)


可以看到我们在插入数据的受并没有指定id字段的数据,但是MySQL给自增创建了一个从1开始的自增索引,当然我们也可以指定id进行插入,因为此处设置的id字段属性为主键,所以不能重复

注意事项

当一个字段拥有自增长属性时,它必须也要拥有键属性(保证唯一性),否则编译器会报错

MariaDB [table_constraint]> create table if not exists auto_increment_test2 (
    -> id int unsigned auto_increment,
    -> name varchar(16) not null);
   #只能设置一个自增字段并且其必须是定义成一个键
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

唯一键( unique )

一张表中有往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键:唯一键就可以解决表中有多个字段需要唯一性约束的问题。

唯一键的本质和主键差不多,唯一键允许为空,而且可以多个为空,空字段不做唯一性比较。

MariaDB [table_constraint]> create table if not exists unique_key_test( id int unsigned unique comment '唯一键', name char(2) not null);
Query OK, 0 rows affected (0.00 sec)

MariaDB [table_constraint]> desc unique_key_test;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | YES  | UNI | NULL    |       |
| name  | char(2)          | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [table_constraint]> insert into unique_key_test (id, name) values (1, '张三');
Query OK, 1 row affected (0.01 sec)
#重复使用 id=1 ,id字段存在unique属性,重复报错
MariaDB [table_constraint]> insert into unique_key_test (id, name) values (1, '李四');
ERROR 1062 (23000): Duplicate entry '1' for key 'id'
#但是unique属性的字段是可以为空的,若字段为空,则不能进行任何运算,也不能判断重复(冲突)
MariaDB [table_constraint]> insert into unique_key_test (name) values ('李四');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into unique_key_test (name) values ('李四');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into unique_key_test (name) values ('李四');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> select * from unique_key_test;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 张三   |
| NULL | 李四   |
| NULL | 李四   |
| NULL | 李四   |
+------+--------+
4 rows in set (0.00 sec)

通过上面的实验我们发现,拥有uique的字段是可以为空的,那么如果给这个字段加上not null属性会怎么样呢

MariaDB [table_constraint]> create table if not exists unique_key_test2(
    -> id int unsigned not null unique,   #创建id字段并赋予其not null和 unique约束
    -> name varchar(16) not null);
Query OK, 0 rows affected (0.01 sec)

MariaDB [table_constraint]> desc unique_key_test2;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI | NULL    |       |
| name  | varchar(16)      | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#可以看到id字段的Key属性变成了primary key(主键)

字段若同时拥有unique约束和not null约束,则其就是主键

主键 vs 唯一键

主键和唯一键并不冲突!在一张表中可以有一个主键,也可以同时具有唯一键!

表其实就是MySQL级别的”类“ 或者 ”对象“的集合(顺序表),表中保存的其实就是某些场景下的属性字段!

数据库本身是一个对数据约束非常严格的技术,我们身上有很多属性具有唯一性(qq账号, 电话, 父母电话, B站账号等),当然也存在不具有唯一性的列(姓名,身高,体重,籍贯等)

**主键并不是设置了主键才成为主键的,而是我们通过场景来从具有对应的具有唯一性的属性中选择或者创造出来的!!**那么没有被选择的具有唯一性的属性,任然具有唯一性,而数据库对这些属性的要求非常严格,要求其虽没有被选中任然要保持唯一性(unique)

MariaDB [table_constraint]> create table if not exists unique_key_test3(
    -> id int unsigned primary key comment'学号',
    -> qq char(20));
Query OK, 0 rows affected (0.03 sec)

MariaDB [table_constraint]> insert into unique_key_test3 (id, qq) values (1, '123@qq.com');
Query OK, 1 row affected (0.00 sec)

MariaDB [table_constraint]> insert into unique_key_test3 (id, qq) values (2, '123@qq.com');
Query OK, 1 row affected (0.01 sec)

MariaDB [table_constraint]> insert into unique_key_test3 (id, qq) values (3, '123@qq.com');
Query OK, 1 row affected (0.01 sec)

MariaDB [table_constraint]> select * from unique_key_test3;
+----+------------+
| id | qq         |
+----+------------+
|  1 | 123@qq.com |
|  2 | 123@qq.com |
|  3 | 123@qq.com |
+----+------------+
3 rows in set (0.00 sec)

在这个表中我们对qq字段没有设置任何属性,因为学号对应的是一名同学具有唯一性,我们将id字段设置为主键。在现实生活中一个qq对应的主人只能有一个,所以不可能有很多学生公用一个qq。所以这张表和现实不符合,这样的操作可能是用户误操作导致的,若这样的数据可以插入数据库则是属性设置不合理导致的

表属性的约束是在倒逼程序员或者用户插入和当前现实世界存在且合理的数据,保证我们数据合理以保证业务逻辑也复合常理

外键

通过外键产生关系的表,除了逻辑上要有关联(外键),MySQL操作上也要维护这种逻辑关系的正确性(外键约束)!!

 foreign key (class_id) references foreignkey_class_test(id)  #设计外键约束

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3j1VzIew-1671509909243)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20221210171606297.png)]

学生表中包含class_id的字段,而这个字段在班级表中担当主键,class_id将两张表联系在了一起,而班级表成为主表,学生表成为从表,学生表中的class id字段应带有外键属性。

外键属性有什么作用呢?它是怎么进行约束数据的呢?

300王五10003
  1. 如果我们想要向学生表中插入这样一个条数据,那么我们不仅要保证student_id, student_name字段的合法,还得到班级表中去确认class_id是否存在,若班级表中不存在10003这个班级,则不能允许插入
  2. 同理想要删除班级表中class_id = 1的记录,我们必须去从表中确认是否还存在class_id = 1的学生存在,只要还有学生在1班就不能删除班级表中的记录
#创建班级表
MariaDB [table_constraint]> create table if not exists foreignkey_class_test(id int primary key, name varchar(20) not null);
Query OK, 0 rows affected (0.02 sec)

MariaDB [table_constraint]> desc foreignkey_class_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#创建学生表,并为其中的 class_id字段与 班级表中的 id字段进行外链约束
MariaDB [table_constraint]> create table foreignkey_stu_test(
    -> id int primary key,
    -> name varchar(20) not null, 
    -> class_id int,
    -> foreign key (class_id) references foreignkey_class_test(id)  #设计外键约束
    -> );
Query OK, 0 rows affected (0.04 sec)
MariaDB [table_constraint]> desc foreignkey_stu_test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | NO   |     | NULL    |       |
| class_id | int(11)     | YES  | MUL | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

#向班级中插入一条记录
MariaDB [table_constraint]> insert into foreignkey_class_test values (1, '小猪1班');
Query OK, 1 row affected (0.00 sec)
#向1班中插入一个学号为100,姓名为张三的学生 (成功)
MariaDB [table_constraint]> insert into foreignkey_stu_test (id, name, class_id) values (100, '张三', 1);
Query OK, 1 row affected (0.00 sec)
#向10班中插入一个学号为200, 姓名为李四的学生 (失败,因为根本就没有10班这个班级) 
MariaDB [table_constraint]> insert into foreignkey_stu_test (id, name, class_id) values (200, '李四', 10);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`table_constraint`.`foreignkey_stu_test`, CONSTRAINT `foreignkey_stu_test_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `foreignkey_class_test` (`id`))

#外键约束,外键关联的字段一定存在
MariaDB [table_constraint]> select * from foreignkey_stu_test;
+-----+--------+----------+
| id  | name   | class_id |
+-----+--------+----------+
| 100 | 张三   |        1 |
+-----+--------+----------+
1 row in set (0.00 sec)

MariaDB [table_constraint]> select * from foreignkey_class_test;
+----+------------+
| id | name       |
+----+------------+
|  1 | 小猪1班    |
+----+------------+
1 row in set (0.00 sec)
#删除id=1字段的班级(失败)因为班级表中id=1的这个字段和从表中的某个字段还存在着关联,只有从表中没有1班的学生后,这个班级才可以删除
MariaDB [table_constraint]> delete from foreignkey_class_test where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`table_constraint`.`foreignkey_stu_test`, CONSTRAINT `foreignkey_stu_test_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `foreignkey_class_test` (`id`))


准确区分: 外键 vs 外键约束

外键是用来实现表和表之间的有关系的字段,其实只是我们自己知道,但是只要是人操作就可以能犯错。为何强约束表之间的关系,需要添加外键约束,添加了外键约束,MySQL才会帮你去维护表之间的逻辑关系!!

1 row in set (0.00 sec)
#删除id=1字段的班级(失败)因为班级表中id=1的这个字段和从表中的某个字段还存在着关联,只有从表中没有1班的学生后,这个班级才可以删除
MariaDB [table_constraint]> delete from foreignkey_class_test where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (table_constraint.foreignkey_stu_test, CONSTRAINT foreignkey_stu_test_ibfk_1 FOREIGN KEY (class_id) REFERENCES foreignkey_class_test (id))

准确区分: 外键 vs 外键约束

外键是用来实现表和表之间的有关系的字段,其实只是我们自己知道,但是只要是人操作就可以能犯错。为何强约束表之间的关系,需要添加外键约束,添加了外键约束,MySQL才会帮你去维护表之间的逻辑关系!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白在进击

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

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

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

打赏作者

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

抵扣说明:

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

余额充值