我应该在哪个表上建立FK关系?

时间:2017-08-13 17:46:03

标签: mysql sql foreign-keys

我有两张表,如:

-- users
+----+--------+---------------------+----------+--------------------+
| id |  name  |       email         | password |      cookie        |
+----+--------+---------------------+----------+--------------------+

-- user_detail
+---------+-----+-------------------+----------------+
| user_id | age |       about       |    birthday    |
+---------+-----+-------------------+----------------+

我需要在users(id)user_detail(user_id)列之间建立关系。现在我想知道,我应该打开user_detail表并在其user_id列上创建关系(指users(id),反之亦然?

1 个答案:

答案 0 :(得分:1)

您正在寻找这样的语法:

alter table user_details add constraint fk_user_details_user_id
    foreign key (user_id) references users(id);