0

Im new to sql, I'm having issues with the foreign key in my table. If any can point out the error, please do, I have no idea what it is.enter image description here

CREATE TABLE donut(
  donutId int(5) NOT NULL, 
  name VARCHAR(20), 
  descriptioin VARCHAR(60), 
  price DECIMAL(8,3), 
  PRIMARY KEY (donutId) 
);

CREATE TABLE customer(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30), 
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),    
zip VARCHAR(8),
homephone  VARCHAR(15),          
cellphone  VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid) );

CREATE TABLE customer_order_donut(
orderid int(10) NOT NULL,
customerid int(10),
donutId int(10), 
orderdate datetime,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid) 
customerid int FOREIGN KEY REFERENCES customer(customerid),
donutId int FOREIGN KEY REFERENCES donut(donutId));
4

2 回答 2

0

检查FOREIGN KEY约束的语法(并在 PRIMARY KEY 后添加逗号):

CREATE TABLE customer_order_donut(
orderid int(10) NOT NULL,
customerid int(10),
donutId int(10), 
orderdate datetime,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutId)
);
于 2017-03-26T21:18:20.930 回答
0

Your FOREIGNKEY syntax is wrong. This should work:

CREATE TABLE donut
(
  donutid      INT(5) NOT NULL,
  name         VARCHAR(20),
  descriptioin VARCHAR(60),
  price        DECIMAL(8,3),
  PRIMARY KEY (donutid)
);
CREATE TABLE customer
(
  customerid INT(5)NOT NULL,
  firstname  VARCHAR(30),
  lastname   VARCHAR(30),
  addres     VARCHAR(100),
  apt        VARCHAR(20),
  city       VARCHAR(30),
  state      VARCHAR(20),
  zip        VARCHAR(8),
  homephone  VARCHAR(15),
  cellphone  VARCHAR(15),
  otherphone VARCHAR(15),
  PRIMARY KEY (customerid)
);
CREATE TABLE customer_order_donut
(
  orderid    INT(10) NOT NULL,
  customerid INT(10),
  donutid    INT(10),
  orderdate  DATETIME,
  donutqty   INT(5),
  specialhandling TEXT,
  PRIMARY KEY (orderid),
  FOREIGN KEY (customerid) REFERENCES customer(customerid),
  FOREIGN KEY (donutid) REFERENCES donut(donutid)
);
于 2017-03-26T21:21:36.507 回答