Wednesday 15 June 2011

database - Should i use foreign key or not -


Suppose that I want to create a database to store a user's login and personal details, > Create a table

  only one table table user username (varchar255) | User_id (int) | Name (varchar255) | Gender (0,1,2) | DOB (date) | Password (char64) |   

or Two tables with a foreign key

  table User_ login User_id (int) | Username (varchar255) | Password (char64) | And Table User_Detail User_id (int) | Name (varchar255) | Gender (0,1,2) | DOB (date) |    

Ask yourself these questions.

Does the user have one or more than one user name?

Will a user have one or more passwords?

Does a user have one or more names?

Does a user have one or more than one gender?

Does a user have one or more birthdate?

When the answer to a question is "one", that column is in the user table.

When the answer to a question is "more than one", that column is in a separate table.

This process is called the first normal form.

No comments:

Post a Comment