外键到非候选键以及删除级联

发布于 2024-11-14 10:13:08 字数 461 浏览 3 评论 0原文

我的问题有两个,

  1. 首先,是否可以在mysql中从引用表到引用表中不是候选键的列创建外键?我使用 SQLYOG 模式设计器尝试了它,它创建的结果很奇怪。只是想与其他人确认一下,然后再假设这是一个错误,也许 sqlyog 或 mysql 实际上允许它。

示例 :

table1 columns :

ssn : 主键

名称 : 非候选键(名称可以重复)

table2 columns

id2 : 主键

name_referencing : table1 中名称的外键(是这个外键可能吗??)

2.如果上述情况可能,那么当“删除级联”发生时会发生什么。也就是说,如果引用列有相同的值(在各个行中),是否仅在删除引用表中的最后一个值(重复值)时才会删除子项(在引用中)?

My question is two fold,

  1. firstly, is it possible to create a foreign key,in mysql, from a referencing table to a column in the referenced table that is not a candidate key ? I tried it using SQLYOG schema designer and it created it strangely. Just want to confirm with others out there, before assuming it is a bug maybe with sqlyog or mysql actually allows it.

example :

table1 columns :

ssn : primary key

name : non-candidate key (names can repeat)

table2 columns

id2 : primary key

name_referencing : foreign key to name in table1 (is this foreign key possible??)

2.If the above case is possible, what happens when 'on delete cascade' happens. That is, if there are same values (in various rows) of the referenced column, does the deletion of the child(in referencing) happen only on the deletion of the last value(of the repeated values) in the referenced table ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-11-21 10:13:08
-- Create the tables
(anthony@localhost) [test]> create table foo (a int primary key, b int not null, index(b)) engine=innodb;
Query OK, 0 rows affected (0.33 sec)

create table bar (b int not null, constraint b_exists foreign key (b) references foo(b) on delete cascade) engine=innodb;
Query OK, 0 rows affected (0.40 sec)

所以,MySQL实际上允许这种情况。诡异的。 Oracle 和 PostgreSQL 不会(都会引发错误),而且我不相信 SQL 标准允许这样做(但没有检查过,所以可能是错误的)。让我们看看它是如何处理的:

-- Fill foo
(anthony@localhost) [test]> insert into foo values (1,1);
Query OK, 1 row affected (0.11 sec)

(anthony@localhost) [test]> insert into foo values (2,1);
Query OK, 1 row affected (0.07 sec)

-- Check foreign key works:
(anthony@localhost) [test]> insert into bar values (1);
Query OK, 1 row affected (0.13 sec)

(anthony@localhost) [test]> insert into bar values (2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`bar`, CONSTRAINT `b_exists` FOREIGN KEY (`b`) REFERENCES `foo` (`b`) ON DELETE CASCADE)

-- Delete

(anthony@localhost) [test]> delete from foo  where a = 1;
Query OK, 1 row affected (0.09 sec)

(anthony@localhost) [test]> select * from bar;
Empty set (0.00 sec)

因此,MySQL 从引用表中删除该行。至少在 5.1.49 中是这样。

-- Create the tables
(anthony@localhost) [test]> create table foo (a int primary key, b int not null, index(b)) engine=innodb;
Query OK, 0 rows affected (0.33 sec)

create table bar (b int not null, constraint b_exists foreign key (b) references foo(b) on delete cascade) engine=innodb;
Query OK, 0 rows affected (0.40 sec)

So, MySQL actually allows this situation. Weird. Oracle and PostgreSQL will not (both raise errors), and I don't believe the SQL standard allows it (but haven't checked, so could be wrong there). Let's see how it handles it:

-- Fill foo
(anthony@localhost) [test]> insert into foo values (1,1);
Query OK, 1 row affected (0.11 sec)

(anthony@localhost) [test]> insert into foo values (2,1);
Query OK, 1 row affected (0.07 sec)

-- Check foreign key works:
(anthony@localhost) [test]> insert into bar values (1);
Query OK, 1 row affected (0.13 sec)

(anthony@localhost) [test]> insert into bar values (2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`bar`, CONSTRAINT `b_exists` FOREIGN KEY (`b`) REFERENCES `foo` (`b`) ON DELETE CASCADE)

-- Delete

(anthony@localhost) [test]> delete from foo  where a = 1;
Query OK, 1 row affected (0.09 sec)

(anthony@localhost) [test]> select * from bar;
Empty set (0.00 sec)

So, MySQL deletes the row from the referencing table. At least in 5.1.49.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文