From ce2f8630e74667a5c97238c397208cd2926c9241 Mon Sep 17 00:00:00 2001 From: yangj1211 <153493538+yangj1211@users.noreply.github.com> Date: Mon, 6 May 2024 14:09:46 +0800 Subject: [PATCH] Foreign key checks (#1045) * add doc of foreign_key_checks * add doc of foreign_key_checks --- .../data-integrity/foreign-key-constraints.md | 2 + .../system-variables/foreign_key_checks.md | 100 ++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 103 insertions(+) create mode 100644 docs/MatrixOne/Reference/Variable/system-variables/foreign_key_checks.md diff --git a/docs/MatrixOne/Develop/schema-design/data-integrity/foreign-key-constraints.md b/docs/MatrixOne/Develop/schema-design/data-integrity/foreign-key-constraints.md index 0a4c38a390..eb1b7fddf0 100644 --- a/docs/MatrixOne/Develop/schema-design/data-integrity/foreign-key-constraints.md +++ b/docs/MatrixOne/Develop/schema-design/data-integrity/foreign-key-constraints.md @@ -2,6 +2,8 @@ FOREIGN KEY 外键约束允许表内或跨表交叉引用相关数据,有助于保持相关数据的一致性。 +当建立外键时,Matrixone 默认会检查外键约束的完整性。如需禁用外键约束检查,请参考章节[外键约束检查](../../../Reference/Variable/system-variables/foreign_key_checks.md)。 + **遵循规则** 定义外键时,需要遵守下列规则: diff --git a/docs/MatrixOne/Reference/Variable/system-variables/foreign_key_checks.md b/docs/MatrixOne/Reference/Variable/system-variables/foreign_key_checks.md new file mode 100644 index 0000000000..86449af10c --- /dev/null +++ b/docs/MatrixOne/Reference/Variable/system-variables/foreign_key_checks.md @@ -0,0 +1,100 @@ +# 外键约束检查 + +在 MatrixOne 中,`foreign_key_checks` 是一个系统变量,用于控制外键约束的检查。这个变量可以是全局的,也可以是会话级别的。当设置为 1(默认值)时,MatrixOne 会检查外键约束的完整性,确保数据的参照完整性。如果设置为 0,则跳过这些检查。 + +!!! note + 与 MySQL 行为不一致的是,当关闭外键约束检查时,删除父表,MySQL 不会删除子表引用父表的外键关系,但 MatrixOne 会删除子表引用父表的外键关系,重建父表后会重新建立外键关系。 + +## 查看 foreign_key_checks + +在 MatrixOne 中使用以下命令查看 foreign_key_checks: + +```sql + +--全局模式 +SELECT @@global.foreign_key_checks; +SHOW global VARIABLES LIKE 'foreign_key_checks'; + +--会话模式 +SELECT @@session.foreign_key_checks; +SHOW session VARIABLES LIKE 'foreign_key_checks'; +``` + +## 设置 foreign_key_checks + +在 MatrixOne 中使用以下命令设置 foreign_key_checks: + +```sql +--全局模式,重新连接数据库生效 +set global foreign_key_checks = 'xxx' + +--会话模式 +set session foreign_key_checks = 'xxx' +``` + +## 示例 + +```sql +mysql> SELECT @@session.foreign_key_checks; ++----------------------+ +| @@foreign_key_checks | ++----------------------+ +| 1 | ++----------------------+ +1 row in set (0.00 sec) + +create table t2(a int primary key,b int); +create table t1( b int, constraint `c1` foreign key `fk1` (b) references t2(a)); + +insert into t2 values(1,2); +mysql> insert into t1 values(3);--开启外键约束检查时,不能插入违反约束的值 +ERROR 20101 (HY000): internal error: Cannot add or update a child row: a foreign key constraint fails + +mysql> drop table t2;--开启外键约束检查时,不能删除父表 +ERROR 20101 (HY000): internal error: can not drop table 't2' referenced by some foreign key constraint + +set session foreign_key_checks =0;--关闭外键约束检查 +mysql> SELECT @@session.foreign_key_checks; ++----------------------+ +| @@foreign_key_checks | ++----------------------+ +| 0 | ++----------------------+ +1 row in set (0.00 sec) + +mysql> insert into t1 values(3);--关闭外键约束检查时,可以插入违反约束的值 +Query OK, 1 row affected (0.01 sec) + +mysql> drop table t2;--关闭外键约束检查时,可以删除父表, +Query OK, 0 rows affected (0.02 sec) + +mysql> show create table t1;--删除父表,外键约束也被删除 ++-------+--------------------------------------------+ +| Table | Create Table | ++-------+--------------------------------------------+ +| t1 | CREATE TABLE `t1` ( +`b` INT DEFAULT NULL +) | ++-------+--------------------------------------------+ +1 row in set (0.00 sec) + +mysql> create table t2(n1 int);--重建被删除的父表 t2,需包含子表原外键引用列 +ERROR 20101 (HY000): internal error: column 'a' no exists in table '' + +mysql> create table t2(n1 int,a int primary key);--包含被引用的主键列 a,重建成功 +Query OK, 0 rows affected (0.01 sec) + +mysql> show create table t1;--重建 t2 后,外键关系会自动重新建立 ++-------+-------------------------------------------------------------------------------------------------------------------------------------------+ +| Table | Create Table | ++-------+-------------------------------------------------------------------------------------------------------------------------------------------+ +| t1 | CREATE TABLE `t1` ( +`b` INT DEFAULT NULL, +CONSTRAINT `c1` FOREIGN KEY (`b`) REFERENCES `t2` (`a`) ON DELETE RESTRICT ON UPDATE RESTRICT +) | ++-------+-------------------------------------------------------------------------------------------------------------------------------------------+ +1 row in set (0.00 sec) + + + +``` diff --git a/mkdocs.yml b/mkdocs.yml index 552b4c5872..731738d686 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -256,6 +256,7 @@ nav: - 保存查询结果支持: MatrixOne/Reference/Variable/system-variables/save_query_result.md - 时区支持: MatrixOne/Reference/Variable/system-variables/timezone.md - 大小写敏感支持: MatrixOne/Reference/Variable/system-variables/lower_case_tables_name.md + - 外键检查支持: MatrixOne/Reference/Variable/system-variables/foreign_key_checks.md - 自定义变量: MatrixOne/Reference/Variable/custom-variable.md - SQL 结构与语法: - 关键字: MatrixOne/Reference/Language-Structure/keywords.md