一,sql脚本(红色为修改后正确的语句)
drop table if exists products; create table products( id int not null auto_increment, title varchar(100) not null, description text not null, image_url varchar(200) not null, price decimal(10,2) not null, date_available datetime not null, primary key(id) )type=innodb;
drop table if exists line_items;
create table line_items( id int not null auto_increment, product_id int not null, quantity int not null default 0, unit_price decimal(10,2) not null,
constraint fk_items_product foreign key (product_id) references producets(id),
index(product_id) primary key(id) )type=innodb;
二,执行提示错误:ERROR 1005: Can't create table
三,原因:
1,MySQL支持外键约束,并提供与其它DB相同的功能,但表类型必须为 InnoDB 2、建外键的表的那个列要加上index 四 更改表的type语句 alter table type=innoDB 五.创建索引 create index indexName on article_class(article_class_id); 六 .创建外建 alter table jspmo add constraint FK_Reference_1 foreign key (class_id) references article_class (article_class_id) on delete restrict on update restrict;