Harjeet Jadeja
Harjeet Jadeja

Reputation: 1604

how to relate two fields of same table in phpmyadmin?

I have created 1 table which stores the user and uid.

Other table stores the skills of the user:

Basically I want to relate uid and skill fields of skill table so on every unique uid there should be unique skill values, but for different uids skill can be same.

Upvotes: 0

Views: 494

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

Use a composite primary key on the two ids UId and skill. Also, to make it many to many relation between the two tables, so that each user has many skills, and to normalize your tables, better off create it this way:

Users table:

  • UserID Primary key,
  • USername.
  • ...

Skills table:

  • SkillId Primary key,
  • SkillName,
  • ...

The linkage table:

UsersSkills:

  • UserId Foreign key references Users(UserId),
  • SkillId Foreign key references Skills(SkillId).

And then the important part is to make a composite primary key on UserId and SkillId, so that the skill is unique for each userid.

In your example, the skill aaa would be stored in the Skills table with an Id = 1, where the user UserId = 111 is stored in the Users table. Then the UsersSkills will contain something like:

UserId SkillId
 111   1

Upvotes: 1

Related Questions