Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用复合键时添加外键约束会导致错误_Mysql_Foreign Keys_Foreign Key Relationship_Composite Primary Key - Fatal编程技术网

Mysql 使用复合键时添加外键约束会导致错误

Mysql 使用复合键时添加外键约束会导致错误,mysql,foreign-keys,foreign-key-relationship,composite-primary-key,Mysql,Foreign Keys,Foreign Key Relationship,Composite Primary Key,我试图创建三个表,但我得到的错误如下 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) )' 上述错误适用于第三个表,即表cabiods\u person 我正在创建如下表 CREATE TABLE p

我试图创建三个表,但我得到的错误如下

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)
)'
上述错误适用于第三个表,即表
cabiods\u 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)
);
但还是同样的错误

你知道怎么解决这个问题吗


在表
cabiods\u person
中,我使用了复合主键作为
主键(personID,hobbID),
您需要将
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)
);
您忘记了外键周围的
()

FOREIGN KEY (personID) REFERENCES personal_details(personID)

如果hbbID是主键的一部分,为什么将其定义为唯一的?这并不意味着sense@a_horse_with_no_name:谢谢你赶上了。。。我去掉了那个。。还是同样的错误…删除“hobbID INT UNIQUE KEY”,将其改为hobbID INT,然后将其与person_id一起声明为主键