2

正确的代码是这样的:

CREATE TABLE Seller (
sID INT AUTO_INCREMENT
    PRIMARY KEY,
sName CHAR (10), 
sAddress CHAR (30)
)ENGINE = INNODB;
CREATE TABLE Sale (
sID INT AUTO_INCREMENT,
iName CHAR(10),
CONSTRAINT sa_pk
    PRIMARY KEY (sID, iName),
CONSTRAINT sa_fk1
    FOREIGN KEY (sID)
    REFERENCES Seller (sID)
    ON UPDATE CASCADE,
CONSTRAINT sa_fk2
    FOREIGN KEY (iName)
    REFERENCES Item (iName)
    ON UPDATE CASCADE
)ENGINE = INNODB;
CREATE TABLE Item (
iPrice REAL,
iStock INT,
iName CHAR(10)
    PRIMARY KEY,
iDescription CHAR (30)
)ENGINE = INNODB;
CREATE TABLE Copy (
oQuantity INT,
iName CHAR(10),
CONSTRAINT co_pk
    PRIMARY KEY (oQuantity, iName),
CONSTRAINT co_fk1
    FOREIGN KEY (oQuantity)
    REFERENCES Orders (oQuantity)
    ON UPDATE CASCADE,
CONSTRAINT co_fk2
    FOREIGN KEY (iName)
    REFERENCES Item (iName)
    ON UPDATE CASCADE
)ENGINE = INNODB;
CREATE TABLE Orders (
oSeller CHAR (10),
oTypes CHAR (10),
oQuantity INT,
oPrice REAL,
oID INT AUTO_INCREMENT
    PRIMARY KEY
)ENGINE = INNODB;
CREATE TABLE Customer (
cID INT AUTO_INCREMENT
    PRIMARY KEY,
cName CHAR (10),
cAddress CHAR (30)
)ENGINE = INNODB;

我一开始就有删除命令,所以每次我运行这个脚本时,它们都被删除了,至少除了复制和销售之外,它们都被删除了。每当我尝试删除它们时它都会失败,因为它们从未被创建我不断收到错误 1005 (HY000) 废话,然后是无法创建表 x。请帮我解决这个烦人的问题。柏油!

4

1 回答 1

1

在你的sale桌子上,你有

CONSTRAINT sa_fk2 FOREIGN KEY (iName) REFERENCES Item (iName)
    ON UPDATE CASCADE

同时,您Item随后将创建表。


要创建Copy表,您需要oQuantity在表中的字段上建立索引Order

INDEX `oQty` (`oQuantity`)

将上述内容添加到 Order 表可以让您创建表。

这是sqlfiddle

于 2013-03-14T11:01:10.277 回答