0

I am to create an enrollment system and I have the following tables in my database:

Subject //Primary key: Subj_no
Student //Primary key: Stdnt_no
Subject_Student //Primary key: Subj_Stdnt_no

Where Subject_Student is related(indexed) to Subject and Student with their primary keys.

Now my question is: How can I prevent the database from making the same relationship again? Example> For say I have the following rows inside Subject_Student:

Subj_Stdnt_no | Subject |  Student 
      1       |    1    |     2
      2       |    1    |     6
      3       |    1    |     2       <------How can I prevent that? It's redundant.
Jomar Sevillejo
  • 1,648
  • 2
  • 21
  • 33

2 Answers2

2

PhpMyAdmin is a query tool for mysql, not a database.

You'll want to make the combination of (Student, Subject) UNIQUE.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • Thank you so much sir for taking the time to reply, but i'm still a student and currently new to database. I've heard about composite keys, but am not sure. Sir if it isn't too much, can you explain further how I can do that? by "You'll want to make the combination of (Student, Subject) UNIQUE." do you mean making both of them primary keys? and thanks for clearing out what PhpMyAdmin is :) – Jomar Sevillejo Mar 09 '13 at 12:33
  • 1
    A composite key is making a combination of two fields. Together with the unique condition, you won't be able to insert the same combination twice. You can have (1, 1), (1, 2), (2, 1), (2, 2), but you won't be able to have (1, 1), (1, 1). – Femaref Mar 09 '13 at 14:08
0

I've managed to answer my own question through thew help of @Femaref I used: ALTER TABLE Subject_Student ADD UNIQUE (Subject,Student);

Jomar Sevillejo
  • 1,648
  • 2
  • 21
  • 33