Error Code: 1215. Cannot add foreign key constraint
来源:2-3 实体类设计与表创建B
慕仰8442947
2019-03-28
create table o2o
.tb_shop_category
(shop_category_id
int(11) not null auto_increment,shop_category_name
varchar(100) not null default ‘’,shop_category_desc
varchar(1000) default ‘’,shop_category_img
varchar(2000) DEFAULT NULL,priority
int(2) NOT NULL DEFAULT ‘0’,create_time
datetime default null,parent_edit_time
datetime default null,
parent_id
int(11) default null,//parent_idint(11)//parent_id
int(11) not null//试过这几个都不行
last_edit_time
datetime default null,
constraint fk_shop_category_self
foreign key(parent_id
)referencestb_shop_category
(shop_category_id
),
primary key(shop_category_id
)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET= utf8;
1回答
-
同学好,要不直接试试我的,如果成功,再和你的对比一下有什么问题。
CREATE TABLE `tb_shop_category` ( `shop_category_id` int(11) NOT NULL AUTO_INCREMENT, `shop_category_name` varchar(100) NOT NULL DEFAULT '', `shop_category_desc` varchar(1000) DEFAULT '', `shop_category_img` varchar(2000) DEFAULT NULL, `priority` int(2) NOT NULL DEFAULT '0', `create_time` datetime DEFAULT NULL, `last_edit_time` datetime DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, PRIMARY KEY (`shop_category_id`), KEY `fk_shop_category_self` (`parent_id`), CONSTRAINT `fk_shop_category_self` FOREIGN KEY (`parent_id`) REFERENCES `tb_shop_category` (`shop_category_id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
052019-04-28
相似问题