我正在尝试创建三个表但是我收到错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCES personal_details(personID)
)'
以上错误适用于第三个表,即表hobbies_person
。
我正在创建表格如下。
CREATE TABLE personal_details (
personID INT PRIMARY KEY,
firstName varchar(30),
middleName varchar(30),
lastName varchar(30),
age INT,
aboutMe varchar(500)
);
CREATE TABLE hobbies (
hobbID INT PRIMARY KEY,
hobbName varchar(30)
);
CREATE TABLE hobbies_person (
personID INT,
hobbID INT,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY personID REFERENCES personal_details(personID)
);
我也试过
CREATE TABLE hobbies_person (
personID INT,
hobbID INT,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY personID REFERENCES personal_details(personID),
FOREIGN KEY hobbID REFERENCES hobbies(hobbID)
);
但仍然是同样的错误。
知道如何解决这个问题吗?
在表格hobbies_person
中,我使用复合主键作为PRIMARY KEY (personID, hobbID),
答案 0 :(得分:4)
您需要将列personID
括在FK定义的括号中:
CREATE TABLE hobbies_person (
personID INT NOT NULL,
hobbID INT NOT NULL,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY (personID) REFERENCES personal_details(personID)
);
答案 1 :(得分:3)
您忘记了外键附近的()
FOREIGN KEY (personID) REFERENCES personal_details(personID)