gpt4 book ai didi

mysql - 向外键约束表添加外键

转载 作者:行者123 更新时间:2023-11-29 18:28:05 26 4
gpt4 key购买 nike

我有这个表:

引用

+---------------------+-----------------------+------+-----+---------+---+
| Field | Type | Null | Key | Default
+---------------------+-----------------------+------+-----+---------+---+
| id | int(11) unsigned | NO | PRI | NULL
... (More Columns)
+---------------------+-----------------------+------+-----+-------------+

引用包信息

+---------------+---------------------+------+-----+---------+------------+
| Field | Type | Null | Key | Default
+---------------+---------------------+------+-----+---------+------------+
| id | int(11) unsigned | NO | PRI | NULL
| quoteId | int(11) unsigned | NO | | NULL
... (More Columns)
+---------------+---------------------+------+-----+---------+------------+

报价航类信息

+---------------------------+---------------------+------+-----+----------+
| Field | Type | Null | Key | Default
+---------------------------+---------------------+------+-----+----------+
| id | int(11) unsigned | NO | PRI | NULL
| quoteId | int(11) unsigned | NO | MUL | NULL
| packageId | int(11) unsigned | YES | MUL | NULL ... (More Columns)
+---------------------------+---------------------+------+-----+----------+

所以基本上有报价是主键,在报价中有包裹之后,包裹可以包含航类,所以我需要当包裹被删除时,所有相关的航类都被删除,所以我添加了航类的外键如下:

ALTER TABLE quotesFlightsInfo
ADD CONSTRAINT fk_quotesFlightsInfo_packageId
FOREIGN KEY (packageId) REFERENCES quotesPackagesInfo(id)
ON DELETE CASCADE

现在我正在尝试向包裹添加外键,因此当删除报价时,包裹也会删除,因此航类但不起作用:(,这里是查询:

ALTER TABLE quotesPackagesInfo 
ADD CONSTRAINT fk_quotesPackagesInfo_quoteId
FOREIGN KEY (quoteId) REFERENCES quotes(id)
ON DELETE CASCADE

错误:无法添加或更新子行:外键约束失败 (????????????.#sql-312_2, CONSTRAINT fk_quotesPackagesInfo_quoteId 外键 (quoteId) 引用 quotes (id) ON DELETE CASCADE)

最佳答案

我删除了quotesPackagesInfo表并再次创建它,从一开始就添加了外键并且它起作用了,但仍然不知道之前出了什么问题

关于mysql - 向外键约束表添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45986218/

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