문제

i have a question about the 1 normal form and will explain it by an example.

lets imagine that we have a set of students that are working on a set of projects, but not necessarily only one, but more than one (many to many relation). we have a table where the information's of the students are recorded, and one for the projects. but we need to link them together. but since the 1NF says redundancy and only value per tuple, how would you do it? both fields are primary keys here

illustation 1:

student_ID         project_ID
   1                   7
   2                   7,1
   3                   4,1,9
   4                   1,3
   5                   1

illustration 2:

student_ID         project_ID
   1                   7
   2                   1
   2                   7
   3                   4
   3                   1
   3                   9
   4                   1
   4                   3
   5                   1

Illustration 1: I know that if this would be a result of a table, this would violate the 1NF becuase one than one value per tuple.

Ilustration 2: since they are primary keys they are not allowed to be duplicated, even if i remove the primary key from the student_ID i still would be redundant.

How can i fix this issue?

thanks in advance :)

도움이 되었습니까?

해결책 2

What you have here is basically a junction table, and your second illustration shows the correct way to normalize it.

Note that, as is typical for junction tables, the primary key for your table will consist of both of the columns together. Together, each unique combination of values in these columns specifies a distinct student–project pairing.

Edit: In MySQL, you would define this table e.g. as:

CREATE TABLE student_projects (
  student_id INTEGER NOT NULL,
  project_id INTEGER NOT NULL,
  PRIMARY KEY (student_id, project_id)
)

To enforce relational consistency, you may also want to add explicit foreign key constraints to each of the columns.

다른 팁

The primary key of this table will be a composite of the two fields. They must both together be unique. Both fields are foreign keys to their respective tables and they will be unique in their respective tables.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top