私はStackOverflowの新機能ですので、何か問題があった場合はごめんなさい。私の母国語ではありません。外字キーがMySQLで時々動作する
[OK]を長くしてください。私が使用しています:
あり、私はこのような二つのテーブル持って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
あなたが見ることができるように - 先があるこれらのテーブルをとリンクすると、NULLを更新します。カスケードにNULLを設定します。。
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)
OK。たぶんそれは全く働いていないかもしれません。この方法を試してみましょう:
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)
私の質問は明らかです。それは何ですか?そして、どのようにして期待どおりに動作させるのでしょうか?どんな助けも高く評価されます。前もって感謝します!
ありがとうございます!私は私が狂っていると思って始めた。魅力的な作品!どのような同情の間違い... –
助けてうれしい – e4c5