SQL上的im newbee,创建引用(产品)表的外键(orderdetails)时出现错误
drop database My_Classicmodels ;
create database My_Classicmodels ;
use My_Classicmodels;
// creat table orders
create table orders (
orderNumber INT(11) auto_increment primary key ,
orderDate datetime not null ,
requiredDate datetime not null ,
shippedDate datetime ,
statuss varchar(15) not null ,
comments text ,
customerNumber int(11) not null
) Engine = InnoDb ;
// creat table productlines
create table productlines (
productLine Varchar(50) primary key ,
textDescription varchar(4000) ,
htmlDescription mediumtext ,
image mediumblob
) ENgine =InnoDB ;
// creat tabale orderdetails
create table orderdetails (
orderNumber Int(11) not null ,
productCode varchar(15) not null ,
quantityOrdered Int(11) not null ,
priceEach Double not null ,
orderLineNumber smallint(6) not null ,
constraint fk_order_number foreign key (orderNumber) references
orders ( orderNumber) on delete restrict on update cascade ,
constraint fk_productCode foreign key ( productCode) references
products (productCode) ,
constraint fk_key primary key( orderNumber, productCode )
) Engine = InnoDb ;
create table products (
productCode varchar(15) primary key ,
productName varchar(70) not null ,
productLine Varchar(50) not null,
productScale varchar(10) not null,
productVendor varchar(50) not null,
productDescription text not null ,
quantityInStock smallint not null ,
buyPrice double not null ,
constraint fk_productLine foreign key (productLine) references
productlines ( productLine) on delete restrict on update cascade
) engine = InnoDB ;
describe orderdetails ;
答案 0 :(得分:0)
由于代码中表格的创建顺序,是指尚未创建的表格产品
以这种方式尝试更改创建顺序 您指的是t
create table products (
productCode varchar(15) primary key ,
productName varchar(70) not null ,
productLine Varchar(50) not null,
productScale varchar(10) not null,
productVendor varchar(50) not null,
productDescription text not null ,
quantityInStock smallint not null ,
buyPrice double not null ,
constraint fk_productLine foreign key (productLine) references
productlines ( productLine) on delete restrict on update cascade
) engine = InnoDB ;
// creat tabale orderdetails
create table orderdetails (
orderNumber Int(11) not null ,
productCode varchar(15) not null ,
quantityOrdered Int(11) not null ,
priceEach Double not null ,
orderLineNumber smallint(6) not null ,
constraint fk_order_number foreign key (orderNumber) references
orders ( orderNumber) on delete restrict on update cascade ,
constraint fk_productCode foreign key ( productCode) references
products (productCode) ,
constraint fk_key primary key( orderNumber, productCode )