-2

I have two tables like:

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

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

I need to make a relation between users(id) and user_detail(user_id) columns. Now I want to know, should I open user_detail table and create a relation on the user_id column of it (which refers to users(id)) or vice versa?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • Downvoter please leave a comment and explain what's wrong with my question? – Martin AJ Aug 13 '17 at 17:47
  • And what is a *definition* of FK? And what does a FK constraint say? And when you googled your title you found/learned...? PS Downvote arrrow mouseover text: "This question does not show any research effort; ...". PPS A relation is a table, You mean relationship. – philipxy Aug 13 '17 at 21:58
  • 1
    Possible duplicate of [Basics of Foreign Keys in MySQL?](https://stackoverflow.com/questions/757181/basics-of-foreign-keys-in-mysql) – philipxy Aug 13 '17 at 22:02

1 Answers1

1

You are looking for syntax like this:

alter table user_details add constraint fk_user_details_user_id
    foreign key (user_id) references users(id);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Ok thx. Just why not `alter table users add constraint fk_user_id foreign key (id) references user_detail(user_id);` ? – Martin AJ Aug 13 '17 at 18:03
  • @MartinAJ . . . Because the foreign key is the `user_id` in `user_details`, so that is where the definition goes. `user.id` is the *primary key* of that table. – Gordon Linoff Aug 13 '17 at 18:05
  • @GordonLinoff Why do you answer a question like this that is an obvious duplicate & should be downvoted? – philipxy Aug 13 '17 at 22:05