How can I reference a composite PK as a foreign key in another table and still have a normalized database?

StackOverflow https://stackoverflow.com/questions/19895805

  •  30-07-2022
  •  | 
  •  

Domanda

First off, here's my code for the three tables I'm having problems with:

    CREATE TABLE ProgramSupervisor
    (    EmpNo                                CHAR (6),
         ProgramSupervisorNo                  CHAR (6),
         TeamNo                               CHAR (3),
         CONSTRAINT PKProgramSupervisor PRIMARY KEY (EmpNo , ProgramSupervisorNo) ,
         CONSTRAINT FKProgSupEmpNo FOREIGN KEY (EmpNo) REFERENCES Employee  )

Another:

    CREATE TABLE ISL
    (    ISLNo                                  CHAR (6) ,
         EmpNo                                  CHAR(6),
         ProgramSupervisorNo                    CHAR (6),
         ISLName                                VARCHAR (30),
         ISLStreet                              VARCHAR (40),
         ISLCity                                VARCHAR (30),
         ISLState                               CHAR (2),
         ISLZip                                 CHAR (5),
    CONSTRAINT PKISL PRIMARY KEY (ISLNo) ,
    CONSTRAINT FKISLProgSupNo FOREIGN KEY (ProgramSupervisorNo, EmpNo) 
               REFERENCES ProgramSupervisor  )

And another:

    CREATE TABLE Hourly
    (    EmpNo                           CHAR (6),
         ISLNo                           CHAR (3),
          NumberOfWriteUpes              SMALLINT,
    CONSTRAINT PKHourly PRIMARY KEY (EmpNo),
    CONSTRAINT FKEmpNo FOREIGN KEY (EmpNo) REFERENCES Employee,
    CONSTRAINT FKISLNo FOREIGN KEY (ISLNo) REFERENCES ISL  )

Here's the ERD and the relationships I'm striving for:

enter image description here

Now that you have the info, let me explain the problem I'm having. First off, the ERD is a little misleading. I have a ProgramSupervisor table that's a child table of the 'Salary' parent table and 'Salary' is a child table of the parent table 'Employee.' With the PK of 'Employee' being EmpNo, the PK of Salary and then ProgramSupervisor are also EmpNo. However, I also want ProgramSupervisorNo to be a PK of the 'ProgramSupervisor' table and have a composite PK for it. When I create the 'ISL' table, in order to reference the composite 'ProgramSupervisor' PK as a FK in the ISL table, Access requires both PK field names. Doesn't this violate normalization standards? Since it requires both EmpNo and ProgramSupervisorNo in the 'ISL' table, doesn't this mean that ProgramSupervisorNo relies on EmpNo and therefore one of the fields shouldn't be included? I'm new to this so please forgive my shoddy explanations.

Also, 'Hourly' employees belong to one ISL and ISL's can have numerous 'Hourly' employees, but the 'Hourly' table is also a child table of Employee and has EmpNo as its primary key. That being said, if I'm required to put EmpNo and ProgramSupervisorNo in my 'ISL' table, how am I to put the ISLNo in my 'Hourly' table?

Clear as mud? Basically, I just to put ProgramSupervisorNo in the 'ISL' table because each ISL has a ProgramSupervisor. Then, I want to put ISLNo in my 'Hourly' table. Please provide some examples and explain to me like I'm five. Any help is much appreciated.

È stato utile?

Soluzione

From academic point of view a Primary key must fit all these points:

  • unique
  • minimal
  • all PK-fields are non-null

Foreign keys are references to other table's primary keys. So they must exactly include the fields that the referenced PK has. And this does not violate normalization since the PK must be minimal (from academic point of view).

When you have a primary key with 2 or more columns and you think it is enough to reference its rows with only one column then your PK is not valid because it is not minimal. When it is valid and you need all these columns to fullfill uniqueness then you also need all these column in the foreign key.

DBMS allow you to define non-minimal PKs but that's then the point where you're leaving the academic concepts.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top