本文简单阐述下其使用方式以及相关实现
其实在之前的版本中,已经实现了标准语法 CHECK(expr), 但是实际上是被忽略掉的,在新版本中,可以在列或者表上做一些约束条件,语法如下:
1.如下是表级约束
其中symbol用来命名约束条件的唯一名字,如果没有指定的话,Mysql也会自动生成约束名,但要注意,在同一个库下面,约束名字不能重复,必须具有唯一性 expr是一个表达式,结果为bool类型 enforced是默认选定的,你也可以手动选定,表示必须满足约束条件才允许写入, 但如果选择NOT ENFORCED的话,则表示约束条件虽然创建了,但并不强制
2.如下是列级别约束, 可以在创建列的时候同时指定约束条件
3.示例:
我们举个简单的例子:
既然约束名必须唯一,那如果我们把t1 rename成t2, 再新建一个t1会怎么样呢 ?
root@test 10:25:35>rename table t1 to t2;
Query OK, 0 rows affected (0.01 sec)
root@test 10:25:37>show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
CONSTRAINT `c1_nonzero` CHECK ((`c1` <> 0)),
CONSTRAINT `c2_positive` CHECK ((`c2` > 0)),
CONSTRAINT `t2_chk_1` CHECK ((`c1` <> `c2`)),
CONSTRAINT `t2_chk_2` CHECK ((`c1` > 10)),
CONSTRAINT `t2_chk_3` CHECK ((`c3` < 100)),
CONSTRAINT `t2_chk_4` CHECK ((`c1` > `c3`))
) ENGINE=InnoDB DEFAULT CHARSET=latin1
root@test 10:25:40>create table t1 like t2;
root@test 10:25:51>show create table t1;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` <> 0)),
CONSTRAINT `t1_chk_2` CHECK ((`c2` > 0)),
CONSTRAINT `t1_chk_3` CHECK ((`c1` <> `c2`)),
CONSTRAINT `t1_chk_4` CHECK ((`c1` > 10)),
CONSTRAINT `t1_chk_5` CHECK ((`c3` < 100)),
CONSTRAINT `t1_chk_6` CHECK ((`c1` > `c3`))
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
你也可以通过alter table的方式增加或删除约束:
InnoDB新增了一个data dictionary表check_constrains, 你可以从information_schema表下面查询:
root@(none) 10:54:05>SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | CHECK_CLAUSE |
+--------------------+-------------------+-----------------+----------------+
| def | test | t2_chk_2 | (`c1` > 10) |
| def | test | c2_positive | (`c2` > 0) |
| def | test | t2_chk_3 | (`c3` < 100) |
| def | test | c1_nonzero | (`c1` <> 0) |
| def | test | t2_chk_4 | (`c1` > `c3`) |
| def | test | t1_chk_1 | (`c1` <> 0) |
| def | test | t1_chk_2 | (`c2` > 0) |
| def | test | t1_chk_3 | (`c1` <> `c2`) |
| def | test | t1_chk_4 | (`c1` > 10) |
| def | test | t1_chk_5 | (`c3` < 100) |
| def | test | t1_chk_6 | (`c1` > `c3`) |
+--------------------+-------------------+-----------------+----------------+
12 rows in set (0.00 sec)
1.新增代码文件
sql/sql_check_constraint.cc
sql/dd/impl/system_views/check_constraints.cc
sql/dd/impl/types/check_constraint_impl.cc
2.表达式定义及存储
InnoDB新增了一个数据词典表mysql.check_constraints用来存储所有的约束条件,表的定义在文件sql/dd/impl/tables/check_constraints.cc
中, 相关堆栈
先存储到dd::Table中,当打开table share时,拷贝到TABLE_SHARE::check_constraint_share_list
open_table
|-> get_table_share_with_discover
|-> get_table_share
|-> dd::cache::Dictionary_client::acquire //去dd获取uncached的表上的定义,存储到dd:Table中
|--> fill_check_constraints_from_dd // 将约束条件拷贝到table share中
在每次实例化线程可操作的TABLE对象时,再从table share中读取,存储到TABLE::table_check_constraint_list中 参考函数:
4.检查约束
每次插入或修改数据,都需要检查对应的约束条件 参考函数: invoke_table_check_constraints