外键引用语法错误

时间:2013-05-27 05:06:40

标签: mysql sql

我在使用以下代码创建成员表时遇到问题。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'schoolID int NOT NULL FOREIGN KEY REFERENCES schools(schoolID),' at line 1

语法有什么问题?

谢谢!

CREATE TABLE schools (
    schoolID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    parentID int NOT NULL DEFAULT 0,
    schoolname VARCHAR(199) NOT NULL,  
    active int NOT NULL,
    dateENTERED datetime NOT NULL
);
CREATE TABLE members
(
    memberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    schoolID int NOT NULL FOREIGN KEY REFERENCES schools(schoolID),
    active int NOT NULL,
    dateENTERED datetime NOT NULL
);

1 个答案:

答案 0 :(得分:3)

CREATE TABLE members
(
    memberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    schoolID int NOT NULL ,
    active int NOT NULL,
    dateENTERED datetime NOT NULL,
    CONSTRAINT member_FK FOREIGN KEY (schoolID) REFERENCES schools(schoolID)
);

你也可以这样做,

CREATE TABLE members
(
    memberID int NOT NULL AUTO_INCREMENT,
    schoolID int NOT NULL ,
    active int NOT NULL,
    dateENTERED datetime NOT NULL,
    CONSTRAINT member_PK PRIMARY KEY (memberID),
    CONSTRAINT member_FK FOREIGN KEY (schoolID) REFERENCES schools(schoolID)
);