views:

72

answers:

4

Here's my database design:

USER                  SKILLS
id                    idUser
email     1-------*   description
name                  selfAssesedLevel
lastname              yearsExperience
?skills?              reference

How could I handle this? I'm kind of lost on what I have to write down in ?skills? field of the USER table.

My purpose is to have a user be able to add n number of skills to his profile.

Thank you.

+6  A: 

You don't need the skills field in the USER table, as you already have a link between user and skills: the SKILLS.idUser field.

So when you want to add a skill for a user with id 666, you should just add a new entry in the SKILLS table, having idUser = 666

Extra: I would recommend to add an extra field to the SKILLS table: 'id' or 'skillsid', which can be the primary key for this table.

Fortega
@Justing: I thought a third table is only necessary on a many-many relationship? I'm implementing a 1 to many relationship. ONE user can have MANY skills.
Serg
@Justin: Why would he? He can have multiple items in the skills table with the same 'idUser'. As long as that 'idUser' is not a unique key of course.@Sergio: that's correct.
Fortega
@Sergio Can multiple users have the same skill? If so, then you'll want to use Justin's solution to avoid having duplicate skills in the SKILLS table. Otherwise, Fortega's solution is correct.
JohnK813
I think @Sergio's point was that if a two users have the 'Data Entry' skill, there will be two 'Data Entry' records in the SKILLS table, if you add a third table of "UserSkills" you can reduce duplicate data.
Nate Bross
Go with the answer from @Chad - then you won't be duplicating skills.
Nathan DeWitt
Fortega
+1  A: 

You don't need a ?skills? column at all in the USER table. The existence of a row in the SKILLS table with a particular idUser is enough.

For example, to select all the skills for user id 4, you would write:

select description, selfAssesedLevel, yearsExperience, reference from SKILLS where idUser = 4
Paul Stephenson
+7  A: 

Use three tables, while you don't NEED three for a one to many relationship, things change, make the db support it, even if the app won't ...yet.

And you do have a many to many, as your skills should be their own table, as two people will share the same skill, what's different is their SelfAssesedLevel, YearsExperience and Reference

Users
----
UserId
Email
Name
LastName


Skills
------
SkillId
Description


UserSkills
----------
UserId
SkillId
SelfAssesedLevel
YearsExperience
Reference

Then your SQL to get the users and their skills would be something along the lines of

SELECT * FROM Users 
    JOIN UserSkills ON Users.UserId = UserSkills.UserId
    JOIN Skills ON UserSkills.SkillId = Skills.SkillId
Chad
Jay
This is clearly the best implementation for such a problem as it prevents duplicate data.
Nate Bross
There's not much point in a unique constraint on (UserId,SkillId), since that's what you should be using as the primary key. There's no point in a surrogate key for that table.
Michael Madsen
+1  A: 

Since a User can have many Skills, and a Skill can be common across many Users, you need a many-to-many JOIN table UserSkills. Users and Skills will have a foreign key relationship with UserSkills.

duffymo