本文简单阐述下其使用方式以及相关实现

其实在之前的版本中,已经实现了标准语法 CHECK(expr), 但是实际上是被忽略掉的,在新版本中,可以在列或者表上做一些约束条件,语法如下:

1.如下是表级约束

其中symbol用来命名约束条件的唯一名字,如果没有指定的话,Mysql也会自动生成约束名,但要注意,在同一个库下面,约束名字不能重复,必须具有唯一性 expr是一个表达式,结果为bool类型 enforced是默认选定的,你也可以手动选定,表示必须满足约束条件才允许写入, 但如果选择NOT ENFORCED的话,则表示约束条件虽然创建了,但并不强制

2.如下是列级别约束, 可以在创建列的时候同时指定约束条件

3.示例:

我们举个简单的例子:

既然约束名必须唯一,那如果我们把t1 rename成t2, 再新建一个t1会怎么样呢 ?

  1. root@test 10:25:35>rename table t1 to t2;
  2. Query OK, 0 rows affected (0.01 sec)
  3. root@test 10:25:37>show create table t2\G
  4. *************************** 1. row ***************************
  5. Table: t2
  6. Create Table: CREATE TABLE `t2` (
  7. `c1` int(11) DEFAULT NULL,
  8. `c2` int(11) DEFAULT NULL,
  9. `c3` int(11) DEFAULT NULL,
  10. CONSTRAINT `c1_nonzero` CHECK ((`c1` <> 0)),
  11. CONSTRAINT `c2_positive` CHECK ((`c2` > 0)),
  12. CONSTRAINT `t2_chk_1` CHECK ((`c1` <> `c2`)),
  13. CONSTRAINT `t2_chk_2` CHECK ((`c1` > 10)),
  14. CONSTRAINT `t2_chk_3` CHECK ((`c3` < 100)),
  15. CONSTRAINT `t2_chk_4` CHECK ((`c1` > `c3`))
  16. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  17. root@test 10:25:40>create table t1 like t2;
  18. root@test 10:25:51>show create table t1;
  19. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | Table | Create Table |
  21. +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | t1 | CREATE TABLE `t1` (
  23. `c1` int(11) DEFAULT NULL,
  24. `c2` int(11) DEFAULT NULL,
  25. `c3` int(11) DEFAULT NULL,
  26. CONSTRAINT `t1_chk_1` CHECK ((`c1` <> 0)),
  27. CONSTRAINT `t1_chk_2` CHECK ((`c2` > 0)),
  28. CONSTRAINT `t1_chk_3` CHECK ((`c1` <> `c2`)),
  29. CONSTRAINT `t1_chk_4` CHECK ((`c1` > 10)),
  30. CONSTRAINT `t1_chk_5` CHECK ((`c3` < 100)),
  31. CONSTRAINT `t1_chk_6` CHECK ((`c1` > `c3`))
  32. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

你也可以通过alter table的方式增加或删除约束:

InnoDB新增了一个data dictionary表check_constrains, 你可以从information_schema表下面查询:

  1. root@(none) 10:54:05>SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
  2. | CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | CHECK_CLAUSE |
  3. +--------------------+-------------------+-----------------+----------------+
  4. | def | test | t2_chk_2 | (`c1` > 10) |
  5. | def | test | c2_positive | (`c2` > 0) |
  6. | def | test | t2_chk_3 | (`c3` < 100) |
  7. | def | test | c1_nonzero | (`c1` <> 0) |
  8. | def | test | t2_chk_4 | (`c1` > `c3`) |
  9. | def | test | t1_chk_1 | (`c1` <> 0) |
  10. | def | test | t1_chk_2 | (`c2` > 0) |
  11. | def | test | t1_chk_3 | (`c1` <> `c2`) |
  12. | def | test | t1_chk_4 | (`c1` > 10) |
  13. | def | test | t1_chk_5 | (`c3` < 100) |
  14. | def | test | t1_chk_6 | (`c1` > `c3`) |
  15. +--------------------+-------------------+-----------------+----------------+
  16. 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

  1. open_table
  2. |-> get_table_share_with_discover
  3. |-> get_table_share
  4. |-> dd::cache::Dictionary_client::acquire //去dd获取uncached的表上的定义,存储到dd:Table中
  5. |--> fill_check_constraints_from_dd // 将约束条件拷贝到table share中

在每次实例化线程可操作的TABLE对象时,再从table share中读取,存储到TABLE::table_check_constraint_list中 参考函数:

4.检查约束

每次插入或修改数据,都需要检查对应的约束条件 参考函数: invoke_table_check_constraints

WL#929: CHECK constraints

相关代码