create table const_category (
cat_id int(8) not null auto_increment primary key,
cat_label varchar(150)
) ENGINE=InnoDB;
create table const_subcategory (
subcat_id int(11) not null auto_increment primary key,
subcat_label varchar(150),
cat_id int(9) not null,
FOREIGN KEY cat_id REFERENCES const_category(cat_id)
)ENGINE=InnoDB;
答案 0 :(得分:2)
将您的代码与MySQL手册中的代码进行比较:
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
外键声明中的列名周围没有括号。
参考: https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
答案 1 :(得分:0)
将括号中的约束名称设为FOREIGN KEY (cat_id)
create table const_subcategory (
subcat_id int(11) not null auto_increment primary key,
subcat_label varchar(150),
cat_id int(9) not null,
FOREIGN KEY (cat_id) REFERENCES const_category(cat_id)
);
在这里看一个小提琴http://sqlfiddle.com/#!2/9b7cd