Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

This is my database structure:

Users {user_id, username}

Profiles {profile_id, user_id, user_views}

My question is: If I do it the way mentioned above, should I to set user_id as primary key or unique key? and what about profile_id?

share|improve this question
    
Primary key is also unique.Profile_ID should also be primary,just use a foreign key for user_id. –  Mihai Mar 22 at 21:50
    
You could also add unique to the username column, so that there cannot be duplicate usernames. –  Tero Kilkanen Mar 22 at 22:00
    
Is not necessary to set user_id as unique? –  Juliano Lima Mar 22 at 22:04

1 Answer 1

up vote 1 down vote accepted

The right way is like the following:

Users {user_id (PK), username (Unique)}

Profiles {profile_id(PK), user_id FK, user_views}

in the given structure, we are making user_id as PK to link between tables, and making the username as Unique to avoid duplication, some people will do the username as PK, but this will make a problem if you want to update the username in any related detailed table linked to that username, in general, you should make sure that your table has only the correct data, without duplication, easy to update, normalized and easy to select with the given PKs or FKs ...

Please read more info from the following reference: http://dev.mysql.com/doc/refman/5.0/en/create-table-foreign-keys.html

share|improve this answer
    
Nice answer! thanks –  Juliano Lima Mar 23 at 13:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.