gpt4 book ai didi

mysql - MySQL.Cluster (NDB) 的外键

转载 作者:行者123 更新时间:2023-11-29 05:30:52 25 4
gpt4 key购买 nike

从 7.3 版本开始,MySQL Cluster 应该能够进行外键约束。但这是发生了什么:

DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;

CREATE TABLE t1 (
id INT PRIMARY KEY
) ENGINE='InnoDB';

CREATE TABLE t2 (
id INT PRIMARY KEY,
t1id INT
) ENGINE='InnoDB';

ALTER TABLE t2
ADD CONSTRAINT t2t1 FOREIGN KEY (t1id) REFERENCES t1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;

这是使用 InnoDB,一切正常。现在用 NDB 试试:

DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;

CREATE TABLE t1 (
id INT PRIMARY KEY
) ENGINE='NDB';

CREATE TABLE t2 (
id INT PRIMARY KEY,
t1id INT
) ENGINE='NDB';

ALTER TABLE t2
ADD CONSTRAINT t2t1 FOREIGN KEY (t1id) REFERENCES t1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;

-- ERROR 150 (HY000): Cannot add foreign key constraint

现在是奇怪的部分:

DROP TABLE IF EXISTS t2;
DROP TABLE IF EXISTS t1;

CREATE TABLE t1 (
id INT UNIQUE KEY
) ENGINE='NDB';

CREATE TABLE t2 (
id INT PRIMARY KEY,
t1id INT
) ENGINE='NDB';

ALTER TABLE t2
ADD CONSTRAINT t2t1 FOREIGN KEY (t1id) REFERENCES t1 (id)
ON DELETE CASCADE ON UPDATE CASCADE;

工作正常。

是否有任何规则说“使用 NDB 存储引擎,您不能在外键约束中引用主键列”或“使用 NDB,您必须在外键约束中引用 UNIQUE KEYS”?

让事情变得更奇怪:

如果将 t1 的定义替换为

CREATE TABLE t1 (
id INT UNIQUE KEY NOT NULL
) ENGINE='NDB';

你得到同样的错误。

我认为 PRIMARY KEY 意味着 NOT NULL 并且问题不在于前者而在于后者。

最佳答案

根据我从 MySQL 站点和开发者博客收集的内容:

An important difference to note with the Foreign Key implementation in InnoDB is that MySQL Cluster does not support the updating of Primary Keys from within the Data Nodes themselves - instead the UPDATE is emulated with a DELETE followed by an INSERT operation. Therefore an UPDATE operation will return an error if the parent reference is using a Primary Key, unless using CASCADE action, in which case the delete operation will result in the corresponding rows in the child table being deleted. The Engineering team plans to change this behavior in a subsequent preview release.

尚未确认是否支持此类操作。

关于mysql - MySQL.Cluster (NDB) 的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14876649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com