2016-09-22 38 views
2

我對StackOverflow非常陌生,所以請抱歉,我在做錯事情。對不起我的英語 - 這不是我的母語。好的,長話短說。我使用:外鍵偶爾在MySQL中工作

Server version: 5.6.31 MySQL Community Server (GPL) 

在那裏,我有兩個表是這樣的:

表文本

CREATE TABLE `texts` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `body` text, 
    `source_id` int(11) unsigned DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `texts_sources_key` (`source_id`), 
    CONSTRAINT `texts_sources_key` FOREIGN KEY (`source_id`) REFERENCES `texts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE 
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 

表texts_sources

CREATE TABLE `texts_sources` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `short_desc` varchar(255) DEFAULT NULL, 
    `long_desc` text, 
    `url` varchar(255) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 

正如你可以看到 - 有前路將這些表與關聯的關鍵字DEL刪除設置NULL ON UPDATE CASCADE

這裏是初始數據:

mysql> select * from texts; 
+----+------+-----------+ 
| id | body | source_id | 
+----+------+-----------+ 
| 1 | *** |   1 | 
+----+------+-----------+ 
1 row in set (0.00 sec) 

mysql> select * from texts_sources; 
+----+------------+-----------+----------------+ 
| id | short_desc | long_desc | url   | 
+----+------------+-----------+----------------+ 
| 1 | *   | NULL  | http://url.com | 
+----+------------+-----------+----------------+ 
1 row in set (0.00 sec) 

現在讓我們做一些魔法。

mysql> update texts_sources set id=5; 
Query OK, 1 row affected (0.05 sec) 
Rows matched: 1 Changed: 1 Warnings: 0 

mysql> select * from texts_sources; 
+----+------------+-----------+----------------+ 
| id | short_desc | long_desc | url   | 
+----+------------+-----------+----------------+ 
| 5 | *   | NULL  | http://url.com | 
+----+------------+-----------+----------------+ 
1 row in set (0.01 sec) 

mysql> select * from texts; 
+----+------+-----------+ 
| id | body | source_id | 
+----+------+-----------+ 
| 1 | *** |   1 | 
+----+------+-----------+ 
1 row in set (0.00 sec) 

不工作。一些更多的樂趣:

mysql> delete from texts_sources; 
Query OK, 1 row affected (0.18 sec) 

mysql> select * from texts; 
+----+------+-----------+ 
| id | body | source_id | 
+----+------+-----------+ 
| 1 | *** |   1 | 
+----+------+-----------+ 
1 row in set (0.00 sec) 

不工作。好。也許它根本不起作用。讓我們試試這樣:

mysql> update texts set source_id=5; 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`serj_by`.`texts`, CONSTRAINT `humor_sources_key` FOREIGN KEY (`source_id`) REFERENCES `texts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) 
mysql> insert into texts (body, source_id) values ("***", 7); 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`serj_by`.`texts`, CONSTRAINT `humor_sources_key` FOREIGN KEY (`source_id`) REFERENCES `texts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) 

工作。樂趣。即使更有趣(texts_sources是空的,希望你記得):

mysql> insert into texts (body, source_id) values ("***", 1); 
Query OK, 1 row affected (0.16 sec) 

mysql> select * from texts_sources; 
Empty set (0.00 sec) 

mysql> select * from texts; 
+----+------+-----------+ 
| id | body | source_id | 
+----+------+-----------+ 
| 1 | *** |   1 | 
| 2 | *** |   1 | 
+----+------+-----------+ 
2 rows in set (0.00 sec) 

所以我的問題是顯而易見的。這是什麼?以及如何讓它在所有情況下按預期工作?任何幫助,高度讚賞。先謝謝你!

回答

2

讓我們仔細看看這個:

CONSTRAINT `texts_sources_key` 
    FOREIGN KEY (`source_id`) 
    REFERENCES `texts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE 

你們雖然命名以此爲texts_sources_key外鍵居然在同一個表引用的列。您可能打算將它設爲

CONSTRAINT `texts_sources_key` 
    FOREIGN KEY (`source_id`) 
    REFERENCES `texts_sources` (`id`) ON DELETE SET NULL ON UPDATE CASCADE 

另外,您需要在創建該約束之前先創建text_sources表。

+0

非常感謝!我開始以爲我瘋了。奇蹟般有效!多麼可惜的錯誤...... –

+0

很高興有幫助 – e4c5