简体   繁体   中英

foreign key reference syntax error

I am having trouble creating members table with the following code. 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

What is wrong with the syntax?

Thanks!

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
);
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)
);

you can also do it this way,

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)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM