Home » Performance » MySQL » PRIMARY KEY or FOREIGN KEY?
PRIMARY KEY or FOREIGN KEY? [message #1456] Thu, 21 June 2007 20:29 Go to next message
omakase  is currently offline omakase
Messages: 3
Registered: June 2007
Junior Member
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?
Re: PRIMARY KEY or FOREIGN KEY? [message #1457 is a reply to message #1456 ] Fri, 22 June 2007 06:20 Go to previous messageGo to next message
babuno5  is currently offline babuno5
Messages: 7
Registered: June 2007
Location: India
Junior Member

uid in the profiles table will be the foreign key referencing to the users table
Re: PRIMARY KEY or FOREIGN KEY? [message #1463 is a reply to message #1456 ] Fri, 22 June 2007 09:27 Go to previous messageGo to next message
omakase  is currently offline omakase
Messages: 3
Registered: June 2007
Junior Member
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?
Re: PRIMARY KEY or FOREIGN KEY? [message #1466 is a reply to message #1463 ] Fri, 22 June 2007 15:29 Go to previous messageGo to next message
babuno5  is currently offline babuno5
Messages: 7
Registered: June 2007
Location: India
Junior Member

uid in the profiles tables will be forced as a FOREIGN KEY which will be non unique
Re: PRIMARY KEY or FOREIGN KEY? [message #1470 is a reply to message #1463 ] Sun, 24 June 2007 15:27 Go to previous message
carpii  is currently offline carpii
Messages: 48
Registered: November 2006
Member
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)
Previous Topic:Mysql Server tuning
Next Topic:InnoDB Backup
Goto Forum:
  


Current Time: Mon Jul 6 19:50:40 EDT 2009

Total time taken to generate the page: 0.01291 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software

MySQL is a trademark of Sun Microsystems.
InnoDB is a trademark of Oracle Corp.

Percona Performance Forums are a service of Percona, Inc.
Not affiliated with Sun Microsystems or Oracle Corp.