mysqli:无法创建MANY到MANY表吗?

时间:2017-01-19 21:30:00

标签: mysql foreign-keys

我已经设置了3个表。一个是成分。另一个是用餐,最后一个用于两者之间的连接参考。但是,每次我尝试为" meal_fk"设置外键时,我都没有定义索引错误,即使据我所知,它已被索引,或者我将其属性设置为索引。那么我做错了什么?

enter image description here

enter image description here

enter image description here

编辑:膳食表

enter image description here

成分<​​/ P>

Table   Create Table    
ingredients     CREATE TABLE `ingredients` (
 `ingredient_id` int(6) unsigned NOT NULL AUTO_INCREMENT,
 `quanity` int(6) NOT NULL,
 `package` int(6) NOT NULL,
 `item` varchar(50) DEFAULT NULL,
 `cost` decimal(18,2) DEFAULT NULL,
 `store` varchar(50) DEFAULT NULL,
 `reg_date` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`ingredient_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4

MEALS

Table   Create Table    
meals   CREATE TABLE `meals` (
 `meal_id` int(6) unsigned NOT NULL AUTO_INCREMENT,
 `ingredients` int(6) NOT NULL,
 `meal` varchar(50) NOT NULL,
 `cost` decimal(18,2) DEFAULT NULL,
 `reg_date` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`meal_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

JUNCTION成分膳食

Table   Create Table    
junction_ingredients_meals  CREATE TABLE `junction_ingredients_meals` (
 `junction_id` int(6) NOT NULL,
 `ingredient_fk` int(6) NOT NULL,
 `meal_fk` int(6) NOT NULL,
 PRIMARY KEY (`junction_id`),
 KEY `ingredient_fk` (`ingredient_fk`,`meal_fk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

1 个答案:

答案 0 :(得分:0)

啊,我知道,引用字段需要与引用的字段完全相同(除非它们可以为空,即使引用的字段不是)。您的问题是引用的字段为unsigned int,您的引用字段为int