gpt4 book ai didi

MySql FOREIGN KEY 约束,错误1005

转载 作者:行者123 更新时间:2023-11-29 02:31:59 25 4
gpt4 key购买 nike

我可以see存在约束问题,但是在这里创建外键的正确方法是什么?

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。

MySql 查询浏览器生成:

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;

这会导致相同的错误。

enter image description here

最佳答案

尝试

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

articles 添加外键到 newsgroups

关于MySql FOREIGN KEY 约束,错误1005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698803/

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