尝试更改表时出现错误 150
我有这个 sql 子句:
CREATE TABLE IF NOT EXISTS `culture` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` char(6) NOT NULL DEFAULT 'it',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
CREATE TABLE IF NOT EXISTS `nations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`culture_id` int(11) NOT NULL,
`iso_code_2` char(2) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `nations_FI_1` (`culture_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=245 ;
CREATE TABLE IF NOT EXISTS `sedi` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
CREATE TABLE IF NOT EXISTS `sedi_i18n` (
`id` int(11) NOT NULL,
`culture` char(6) NOT NULL,
`nation` char(2) NOT NULL,
`indirizzo` text NOT NULL,
PRIMARY KEY (`id`,`culture`),
KEY `sedi_i18n_FI_2` (`culture`),
KEY `sedi_i18n_FI_3` (`nation`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `sedi_i18n`
ADD CONSTRAINT `sedi_i18n_FK_1` FOREIGN KEY (`id`) REFERENCES `sedi` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `sedi_i18n_FK_2` FOREIGN KEY (`culture`) REFERENCES `culture` (`code`),
ADD CONSTRAINT `sedi_i18n_FK_3` FOREIGN KEY (`nation`) REFERENCES `nations` (`iso_code_2`);
但我遇到了这个错误:
错误查询SQL:
更改表
sedi_i18n
添加 约束sedi_i18n_FK_1
国外 密钥 (id
) 参考sedi
(id
) 删除级联,添加 约束sedi_i18n_FK_2
国外 关键(文化
)参考文献culture
(code
),添加约束sedi_i18n_FK_3
外键(国家
) 参考国家
(iso_code_2
) ;MySQL 消息:
1005 - 无法创建表“test_javier_4.#sql-528_aed”(错误号:150)
有什么想法吗?
问候
哈维
i have this sql clauses:
CREATE TABLE IF NOT EXISTS `culture` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` char(6) NOT NULL DEFAULT 'it',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
CREATE TABLE IF NOT EXISTS `nations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`culture_id` int(11) NOT NULL,
`iso_code_2` char(2) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `nations_FI_1` (`culture_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=245 ;
CREATE TABLE IF NOT EXISTS `sedi` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
CREATE TABLE IF NOT EXISTS `sedi_i18n` (
`id` int(11) NOT NULL,
`culture` char(6) NOT NULL,
`nation` char(2) NOT NULL,
`indirizzo` text NOT NULL,
PRIMARY KEY (`id`,`culture`),
KEY `sedi_i18n_FI_2` (`culture`),
KEY `sedi_i18n_FI_3` (`nation`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `sedi_i18n`
ADD CONSTRAINT `sedi_i18n_FK_1` FOREIGN KEY (`id`) REFERENCES `sedi` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `sedi_i18n_FK_2` FOREIGN KEY (`culture`) REFERENCES `culture` (`code`),
ADD CONSTRAINT `sedi_i18n_FK_3` FOREIGN KEY (`nation`) REFERENCES `nations` (`iso_code_2`);
But i'm gettin this error:
Errore query SQL:
ALTER TABLE
sedi_i18n
ADD
CONSTRAINTsedi_i18n_FK_1
FOREIGN
KEY (id
) REFERENCESsedi
(id
) ON DELETE CASCADE , ADD
CONSTRAINTsedi_i18n_FK_2
FOREIGN
KEY (culture
) REFERENCESculture
(code
) , ADD CONSTRAINTsedi_i18n_FK_3
FOREIGN KEY (nation
) REFERENCESnations
(iso_code_2
) ;Messaggio di MySQL:
1005 - Can't create table 'test_javier_4.#sql-528_aed' (errno: 150)
Any idea?
Regards
Javi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 3 个外键中的 2 个引用父表中的列,这些列既不是主键,也没有唯一索引。
所有外键列必须引用父表中的主键或唯一键。
这是两个坏的外键:
如果这些列在父表中是唯一的,那么您可以通过在它们上添加唯一索引来解决此问题,如下所示:
The problem is that 2 of your 3 foreign keys refer to columns in the parent table that are neither primary keys nor do they have a unique index on them.
All foreign key columns must reference either the primary key or a unique key in the parent table.
These are the two bad foreign keys:
If those columns are meant to be unique in the parent table, then you may resolve this by adding unique indexes on them like this:
来自 MySQL 文档:
您能否通过一次运行一个外键约束添加来隔离导致错误的三个外键约束添加中的哪一个?喜欢:
From the MySQL documentation:
Can you isolate which of the three foreign key constraint additions is causing the error, by running them one at a time? Like: