2

制約の問題があることがわかりますが、ここで外部キーを作成する正しい方法は何でしょうか?

mysql> 
mysql> show tables;
+----------------+
| Tables_in_nntp |
+----------------+
| articles       |
| newsgroups     |
+----------------+
2 rows in set (0.01 sec)

mysql> 
mysql> describe newsgroups;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| newsgroup | text    | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> 
mysql> describe articles;
+--------------+---------+------+-----+---------+----------------+
| Field        | Type    | Null | Key | Default | Extra          |
+--------------+---------+------+-----+---------+----------------+
| id           | int(11) | NO   | PRI | NULL    | auto_increment |
| subject      | text    | NO   |     | NULL    |                |
| content      | text    | NO   |     | NULL    |                |
| number       | text    | NO   |     | NULL    |                |
| sent         | date    | NO   |     | NULL    |                |
| header_id    | text    | NO   |     | NULL    |                |
| newsgroup_id | int(11) | NO   |     | NULL    |                |
+--------------+---------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> 
mysql> ALTER TABLE articles ADD FOREIGN KEY (newsgroup_id) REFERENCES newsgroup(id);
ERROR 1005 (HY000): Can't create table 'nntp.#sql-3bf_9a' (errno: 150)
mysql> 

どちらのテーブルもinnodbを使用しています。

MySqlQueryブラウザは以下を生成します。

ALTER TABLE `nntp`.`articles` ADD CONSTRAINT `new_fk_constraint` FOREIGN KEY `new_fk_constraint` (`newsgroup_id`)
    REFERENCES `newsgroups` (`id`)
    ON DELETE SET NULL
    ON UPDATE SET NULL;

同じエラーが発生します。

ここに画像の説明を入力してください

4

1 に答える 1

2

試す

ALTER TABLE articles
ADD FOREIGN KEY
newsgroup_fk (newsgroup_id)
REFERENCES newsgroups (id)

から に外部キーを追加articlesnewsgroupsます。

于 2012-07-28T06:26:13.700 に答える