GET 24/7 LIVE HELP NOW

Announcement

Announcement Module
Collapse
No announcement yet.

PRIMARY KEY or FOREIGN KEY?

Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PRIMARY KEY or FOREIGN KEY?

    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?

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

    Comment


    • #3
      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?

      Comment


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

        Comment


        • #5
          omakase wrote on Fri, 22 June 2007 09:27

          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)

          Comment

          Working...
          X