在MySQL中创建表时获取错误

时间:2011-11-09 14:16:15

标签: mysql sql

我正在尝试运行以下SQL脚本:

CREATE TABLE part
(Partno    CHAR(4) PRIMARY KEY,
 Partdesc  VARCHAR(20),
 Onhand    INTEGER,
 Partclass CHAR(2) check (Partclass IN ('AP','HW','KI','SP')),
 Unitprice DECIMAL(6,2)
);


CREATE TABLE salesrep
(Srepno     CHAR(3),
 Srepname   VARCHAR(25),
 Srepstreet VARCHAR(30),
 Srepcity   VARCHAR(15) NOT NULL,
 Srepprov   VARCHAR(3) NOT NULL,
 Sreppcode  VARCHAR(6) NOT NULL,
 Totcomm    DECIMAL(8,2),
 Commrate   DECIMAL(3,2),
 CONSTRAINT pkslsrep PRIMARY KEY (Srepno)
);


CREATE TABLE customer
(Custno     CHAR(3),
 Custname   VARCHAR(25) NOT NULL,
 Custstreet VARCHAR(30) NOT NULL,
 Custcity   VARCHAR(15) NOT NULL,
 Custprov   VARCHAR(3) NOT NULL,
 Custpcode  VARCHAR(6) NOT NULL,
 Disc       DECIMAL(3,1),
 Balance    DECIMAL(7,2),
 Credlimit  DECIMAL(5),
 Srepno     CHAR(3),
 CONSTRAINT pkcustno PRIMARY KEY (Custno),
 CONSTRAINT fksrepno FOREIGN KEY (Srepno) REFERENCES salesrep(Srepno)
);


CREATE TABLE orders
(Orderno   CHAR(5) UNIQUE NOT NULL,
 Orderdate DATE,
 Custno    CHAR(3) NOT NULL,
 CONSTRAINT fkordercust FOREIGN KEY (Custno) REFERENCES customer (Custno)
);


CREATE TABLE orderprod
(Orderno    CHAR(5),
 Partno     CHAR(4),
 Orderqty   INTEGER CHECK (Orderqty>0),
 Orderprice DECIMAL(7,2),
 CONSTRAINT pkorderprod PRIMARY KEY (Orderno, Partno),
 CONSTRAINT fkordprdord FOREIGN KEY (Orderno) REFERENCES orders(Orderno),
 CONSTRAINT fkordprdpar FOREIGN KEY (Partno) REFERENCES part(Partno)
);


CREATE TABLE invoice
( Invno     CHAR(6),
  Invdate   DATE,
  Orderno    CHAR(5),
  CONSTRAINT fkinvoice FOREIGN KEY (Orderno) REFERENCES orders(Orderno),
  CONSTRAINT pkinvoice PRIMARY KEY (Invno)
);


CREATE TABLE invprod
( Invno     CHAR(6),
  Partno     CHAR(4),
  Shipqty    INTEGER CHECK (Shipqty>0),
  CONSTRAINT fkinvoice FOREIGN KEY (Invno) REFERENCES invoice(Invno),
  CONSTRAINT fkpart FOREIGN KEY (Partno) REFERENCES part(Partno)
);

DROP TABLE invprod;
DROP TABLE invoice;
DROP TABLE orderprod;
DROP TABLE orders;
DROP TABLE customer;
DROP TABLE salesrep;
DROP TABLE part;*

我无法创建表invprod。我收到错误:69无法创建表'sh.invprod'(错误号:121)。我在这个剧本中犯了什么错误?

1 个答案:

答案 0 :(得分:2)

CONSTRAINT fkinvoice已被定义为CREATE TABLE invoice的一部分。约束名称应该像表或索引一样唯一。