CREATE TABLE IF NOT EXISTS `questions` (
`question_id` int(11) NOT NULL AUTO_INCREMENT,
`createddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateddate` timestamp NULL DEFAULT NULL,
`active_flag` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`question_id`),
UNIQUE KEY `id_UNIQUE` (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE `alarts` (
`alart_id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
`alart_name` varchar(45) NOT NULL,
`interval` int(10) unsigned NOT NULL,
`alart_sent_counter` int(10) unsigned NOT NULL,
`alart_types_id` BIGINT(20) unsigned NOT NULL,
`contact_group_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`alart_id`),
FOREIGN KEY (`alart_types_id`) REFERENCES alart_types(`alart_types_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
我想创建一个包含两个FOREIGN KEY的新表:
CREATE TABLE `alart_question_mapping` (
`alart_question_mapping_id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`alart_id` BIGINT(20) unsigned NOT NULL,
PRIMARY KEY (`alart_question_mapping_id`),
FOREIGN KEY (`question_id`) REFERENCES questions(`question_id`),
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
但我收到错误: 错误代码:1005。无法创建表' alart_question_mapping' (错误:150)
如何创建此表?
感谢' S
答案 0 :(得分:2)
我唯一能看到的是您在CREATE TABLE
语句中引用了一个表,该表中没有您提供的内容:
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
如果删除此引用,表将创建。见SQL Fiddle with Demo
编辑#1,根据您的更新,问题是您引用了上一个表中的错误字段:
改变这个:
CREATE TABLE `alart_question_mapping` (
`alart_question_mapping_id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`alart_id` BIGINT(20) unsigned NOT NULL,
PRIMARY KEY (`alart_question_mapping_id`),
FOREIGN KEY (`question_id`) REFERENCES questions(`question_id`),
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
)
对此:
CREATE TABLE `alart_question_mapping` (
`alart_question_mapping_id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`alart_id` BIGINT(20) unsigned NOT NULL,
PRIMARY KEY (`alart_question_mapping_id`),
FOREIGN KEY (`question_id`) REFERENCES questions(`question_id`),
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_types_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
所以你要改变这一行:
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
到此:
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_types_id`)
如果您引用alart_types
表,则需要引用alart_types_id
而不是alart_id
答案 1 :(得分:2)
Chane声明:
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
到
FOREIGN KEY (`alart_id`) REFERENCES alarts(`alart_id`)
答案 2 :(得分:1)
找不到表alart_types
。
<强> From MySQL Foreign Key Constraints 强>
如果重新创建已删除的表,则必须具有定义 符合引用它的外键约束。 必须 拥有正确的列名和类型,并且必须具有索引 如前所述,引用的密钥。如果不满意,MySQL 返回错误号1005并在错误中引用错误150 消息。
我认为你的意思是
FOREIGN KEY (`alart_id`) REFERENCES alart(`alart_id`)
而不是
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
希望这是有道理的。
答案 3 :(得分:1)
在此表中
CREATE TABLE alart_types (
alart_types_id BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
alarts_types_name varchar(45) NOT NULL,
PRIMARY KEY (alart_types_id)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
在没有对alarts_types_name具有唯一约束的情况下,拥有自动增量ID号并没有多大意义。如果没有这种独特的约束,你几乎肯定会得到一个数据看起来像这样的表。
1 Warning
2 Critical
3 Warning
4 Warning
此约束引用不存在的列。
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_id`)
应该是
FOREIGN KEY (`alart_id`) REFERENCES alart_types(`alart_types_id`)