我设计了一个包含8个表的MySQL数据库。我编写了SQL来生成每个表及其键和约束。但是一个表总是在创建时失败,并且没有有用的错误消息可以提示问题是什么。我在GitHub上描述了我的数据库。放大图表,了解事物是如何连接的:
https://github.com/gustafl/Lexeme/wiki/Lexeme-data-model
这是生成前7个表的代码:
CREATE TABLE `grammatical_category` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `grammeme` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`grammatical_category` tinyint(3) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `grammatical_category_fk_idx` (`grammatical_category`),
CONSTRAINT `grammeme_grammatical_category_fk` FOREIGN KEY (`grammatical_category`) REFERENCES `grammatical_category` (`id`)
);
CREATE TABLE `lexical_category` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `language` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`code` varchar(5) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `code_idx` (`code`)
);
CREATE TABLE `lexeme` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`language` tinyint(3) unsigned NOT NULL,
`lexical_category` tinyint(3) unsigned NOT NULL,
`spelling` varchar(100) NOT NULL,
`pronounciation` varchar(100),
PRIMARY KEY (`id`),
KEY `language_fk_idx` (`language`),
KEY `lexical_category_fk_idx` (`lexical_category`),
CONSTRAINT `lexeme_language_fk` FOREIGN KEY (`language`) REFERENCES `language` (`id`),
CONSTRAINT `lexeme_lexical_category_fk` FOREIGN KEY (`lexical_category`) REFERENCES `lexical_category` (`id`)
);
CREATE TABLE `inflection` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lexeme` int(10) unsigned NOT NULL,
`spelling` varchar(100) NOT NULL,
`pronounciation` varchar(100),
PRIMARY KEY (`id`),
KEY `lexeme_fk_idx` (`lexeme`),
CONSTRAINT `inflection_lexeme_fk` FOREIGN KEY (`lexeme`) REFERENCES `lexeme` (`id`)
);
CREATE TABLE `inflection_grammatical_category` (
`inflection` int(10) unsigned NOT NULL,
`grammatical_category` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`inflection`,`grammatical_category`),
KEY `inflection_fk_idx` (`inflection`),
KEY `grammatical_category_fk_idx` (`grammatical_category`),
CONSTRAINT `inflection_grammatical_category_grammatical_category_fk` FOREIGN KEY (`grammatical_category`) REFERENCES `grammatical_category` (`id`),
CONSTRAINT `inflection_grammatical_category_inflection_fk` FOREIGN KEY (`inflection`) REFERENCES `inflection` (`id`)
);
这是最后一个失败的表:
CREATE TABLE `lexeme_grammatical_category` (
`lexeme` int(10) unsigned NOT NULL,
`grammatical_category` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`lexeme`,`grammatical_category`),
KEY `lexeme_fk_idx` (`lexeme`),
KEY `grammatical_category_fk_idx` (`grammatical_category`),
CONSTRAINT `lexeme_grammatical_category_grammatical_category_fk` FOREIGN KEY (`grammatical_category`) REFERENCES `grammatical_category` (`id`),
CONSTRAINT `lexeme_grammatical_category_lexeme_fk` FOREIGN KEY (`lexeme`) REFERENCES `lexeme` (`id`)
);
lexeme_grammatical_category 表与 inflection_grammatical_category 相同,只是“inflection”在任何地方都被“lexeme”替换。我做了搜索并更换以确定。 我也尝试在 inflection_grammatical_category 之前创建 lexeme_grammatical_category ,但它仍然失败。
我只是看不出有什么不对。我想知道它可能是一个循环引用问题,因为 lexeme 和 inflection 也是连接的。但是 lexeme_grammatical_category 在我创建 inflection_grammatical_category 之前就失败了,所以我看不清楚。