我在mySQL中的外键有什么问题?

时间:2013-11-17 21:41:34

标签: mysql foreign-keys

每当我尝试在mySQL中运行此脚本时,我会收到一条错误,内容如下: 错误1064(42000):您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法 CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number), CONSTRAINT ITEM_CATEGORY_fk FOREIG'第7行

它引用的表的代码位于顶部,具有失败的外键约束的表的代码如下:

CREATE TABLE CATEGORY(
Category_Name varchar(35) NOT NULL,
ShippingPerPound DECIMAL(4,2),
OffersAllowed ENUM('y', 'n'),
CONSTRAINT CATEGORY_pk PRIMARY KEY(Category_Name)
);

CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);

1 个答案:

答案 0 :(得分:1)

您的Category_Name declaration in your项目表格不完整:

尝试:

CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name varchar(35) NOT NULL,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);