我有两张表,如:
-- 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)
),反之亦然?
答案 0 :(得分:1)
您正在寻找这样的语法:
alter table user_details add constraint fk_user_details_user_id
foreign key (user_id) references users(id);