2

我正在尝试创建三个表,但是出现错误

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

4

2 回答 2

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)
);
于 2012-06-19T09:23:40.810 回答
3

你忘记了()你的外键

FOREIGN KEY (personID) REFERENCES personal_details(personID)
于 2012-06-19T09:24:12.780 回答