# PRIMARY KEY or FOREIGN KEY?

**URL:** <https://forums.percona.com/t/primary-key-or-foreign-key/365>\
**Category:** Other MySQL® Questions\
**Created:** [June 21, 2007, 6:59pm UTC](https://forums.percona.com/t/primary-key-or-foreign-key/365 "2007-06-21T18:59:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![omakase](https://avatars.discourse-cdn.com/v4/letter/o/e480ec/32.png) [@omakase](https://forums.percona.com/u/omakase)\
**Post date:** [June 21, 2007, 6:59pm UTC](https://forums.percona.com/t/primary-key-or-foreign-key/365/1 "2007-06-21T18:59:17Z")

</div>

Say I have a users table with uid, name, email, and password with a PRIMARY KEY on uid and a UNIQUE KEY on email.

Now I want to create a profiles table with uid, and other extended profile information. Should uid be a PRIMARY KEY or a FOREIGN KEY referencing uid from users in this case?

---

<div class="post-metadata">

**Author:** ![babuno5](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@babuno5](https://forums.percona.com/u/babuno5)\
**Post date:** [June 22, 2007, 4:50am UTC](https://forums.percona.com/t/primary-key-or-foreign-key/365/2 "2007-06-22T04:50:10Z")

</div>

uid in the profiles table will be the foreign key referencing to the users table

---

<div class="post-metadata">

**Author:** ![omakase](https://avatars.discourse-cdn.com/v4/letter/o/e480ec/32.png) [@omakase](https://forums.percona.com/u/omakase)\
**Post date:** [June 22, 2007, 7:57am UTC](https://forums.percona.com/t/primary-key-or-foreign-key/365/3 "2007-06-22T07:57:05Z")

</div>

Ok thank you! Do I not want to force uniqueness of uid in profiles though? Is this worthwhile? Would it then be accomplished through a primary key or a unique key?

---

<div class="post-metadata">

**Author:** ![babuno5](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@babuno5](https://forums.percona.com/u/babuno5)\
**Post date:** [June 22, 2007, 1:59pm UTC](https://forums.percona.com/t/primary-key-or-foreign-key/365/4 "2007-06-22T13:59:26Z")

</div>

uid in the profiles tables will be forced as a FOREIGN KEY which will be non unique

---

<div class="post-metadata">

**Author:** ![carpii](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@carpii](https://forums.percona.com/u/carpii)\
**Post date:** [June 24, 2007, 1:57pm UTC](https://forums.percona.com/t/primary-key-or-foreign-key/365/5 "2007-06-24T13:57:29Z")

</div>

| [B]omakase wrote on Fri, 22 June 2007 09:27[/B] |
| Ok thank you! Do I not want to force uniqueness of uid in profiles though? Is this worthwhile? Would it then be accomplished through a primary key or a unique key? |

Decide if there is a one-to-one mapping between users and profiles table. Im guessing there might be, in that for each row in users, there is only one row in profiles?

If thats the case, then set profiles.userid to be PRIMARY KEY. This obviously forces uniqueness.  
It can also be a FOREIGN KEY into the users table (which normally wouldnt, in itself, enforce uniqueness)
