Relations in ER diagram

106 Views Asked by At

I am trying to create a database for a software development company (as a free time project)

I've been struggling with this for quite a while now, it's time to ask you guys for help.

I have 3 entities, PROGRAMMER, PROGRAM and PLATFORM. A programmer can work on many platforms, a program can be worked on many platforms and a programmer can work on many programs. BUT for a specific program the programmer can only work with one platform. So I created another table called WORKS witch has the following attributes: Programmer_ID, Program_ID and Platform_ID. The first two are Primary Keys so that it meets the specifications above.

Now, is where the problem starts: Programmers can supervise a program (it's not mandatory). The supervising team meets and the database has to store a team_ID for every program and a DATE for the meeting, also a Meeting_ID but that's irrelevant.

So, I have to make a table that doesn't allow duplicates on the pair Programmer_ID - Program_ID so that the same person wont be on the same group a second time, also it has to have a Team_ID witch will be the same number as the Program_ID and it has to reference the Team_ID to the MEETING table so I can store it. How can I do such a thing?

Thank you for your time.

PS: Here is how my ER looks: ER Diagram

And this is the exported DLL in MS SQL 2012:

CREATE
  TABLE CUSTOMER
  (
    Customer_ID NUMERIC (5) NOT NULL ,
    Name        CHAR (30) NOT NULL ,
    PR_Guy      CHAR (30) NOT NULL ,
    Phone       CHAR (20) NOT NULL
  )
  ON "default"
GO
ALTER TABLE CUSTOMER ADD CONSTRAINT CUSTOMER_PK PRIMARY KEY CLUSTERED (
Customer_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE KNOWS
  (
    Programmer_ID NUMERIC (5) NOT NULL ,
    Prog_Lang_ID  NUMERIC (3) NOT NULL
  )
  ON "default"
GO
ALTER TABLE KNOWS ADD CONSTRAINT KNOWS_PK PRIMARY KEY CLUSTERED (Programmer_ID,
Prog_Lang_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE MEETING_DATE
  (
    Meeting_ID    NUMERIC (3) NOT NULL ,
                  DATE DATE ,
    Program_ID    NUMERIC (5) ,
    Programmer_ID NUMERIC (5)
  )
  ON "default"
GO
ALTER TABLE MEETING_DATE ADD CONSTRAINT MEETING_DATE_PK PRIMARY KEY CLUSTERED (
Meeting_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE PLATFORM
  (
    Platform_ID   NUMERIC (5) NOT NULL ,
    Name          CHAR (30) NOT NULL ,
    OS            CHAR (30) ,
    Latest_Update DATE NOT NULL ,
    Dev_Name      CHAR (30) NOT NULL
  )
  ON "default"
GO
ALTER TABLE PLATFORM ADD CONSTRAINT PLATFORM_PK PRIMARY KEY CLUSTERED (
Platform_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE PROGRAM
  (
    Program_ID  NUMERIC (5) NOT NULL ,
    Name        CHAR (30) NOT NULL ,
    Deadline    DATE NOT NULL ,
    Customer_ID NUMERIC (5) NOT NULL
  )
  ON "default"
GO
ALTER TABLE PROGRAM ADD CONSTRAINT PROGRAM_PK PRIMARY KEY CLUSTERED (Program_ID
)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE PROGRAMMER
  (
    Programmer_ID NUMERIC (5) NOT NULL ,
    First_Name    CHAR (30) NOT NULL ,
    Last_Name     CHAR (30) NOT NULL ,
    Money_Status  NUMERIC (1) NOT NULL
  )
  ON "default"
GO
ALTER TABLE PROGRAMMER ADD CONSTRAINT PROGRAMMER_PK PRIMARY KEY CLUSTERED (
Programmer_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE PROG_LANG
  (
    Prog_Lang_ID NUMERIC (3) NOT NULL ,
    Name         CHAR (30) NOT NULL
  )
  ON "default"
GO
ALTER TABLE PROG_LANG ADD CONSTRAINT PROG_LANG_PK PRIMARY KEY CLUSTERED (
Prog_Lang_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE SUPERVISE
  (
    Programmer_ID NUMERIC (5) NOT NULL ,
    Program_ID    NUMERIC (5) NOT NULL ,
    Group_ID      NUMERIC (5) NOT NULL
  )
  ON "default"
GO
ALTER TABLE SUPERVISE ADD CONSTRAINT SUPERVISE_PK PRIMARY KEY CLUSTERED (
Program_ID, Programmer_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

CREATE
  TABLE WORKS
  (
    Programmer_ID NUMERIC (5) NOT NULL ,
    Program_ID    NUMERIC (5) NOT NULL ,
    Platform_ID   NUMERIC (5) NOT NULL
  )
  ON "default"
GO
ALTER TABLE WORKS ADD CONSTRAINT WORKS_PK PRIMARY KEY CLUSTERED (Programmer_ID,
Program_ID)
WITH
  (
    ALLOW_PAGE_LOCKS = ON ,
    ALLOW_ROW_LOCKS  = ON
  )
  ON "default"
GO

ALTER TABLE KNOWS
ADD CONSTRAINT FK_ASS_2 FOREIGN KEY
(
Programmer_ID
)
REFERENCES PROGRAMMER
(
Programmer_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE KNOWS
ADD CONSTRAINT FK_ASS_3 FOREIGN KEY
(
Prog_Lang_ID
)
REFERENCES PROG_LANG
(
Prog_Lang_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE MEETING_DATE
ADD CONSTRAINT MEETING_DATE_SUPERVISE_FK FOREIGN KEY
(
Program_ID,
Programmer_ID
)
REFERENCES SUPERVISE
(
Program_ID ,
Programmer_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE PROGRAM
ADD CONSTRAINT PROGRAM_CUSTOMER_FK FOREIGN KEY
(
Customer_ID
)
REFERENCES CUSTOMER
(
Customer_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE SUPERVISE
ADD CONSTRAINT SUPERVISE_WORKS_FK FOREIGN KEY
(
Programmer_ID,
Program_ID
)
REFERENCES WORKS
(
Programmer_ID ,
Program_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE WORKS
ADD CONSTRAINT WORKS_PLATFORM_FK FOREIGN KEY
(
Platform_ID
)
REFERENCES PLATFORM
(
Platform_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE WORKS
ADD CONSTRAINT WORKS_PROGRAMMER_FK FOREIGN KEY
(
Programmer_ID
)
REFERENCES PROGRAMMER
(
Programmer_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO

ALTER TABLE WORKS
ADD CONSTRAINT WORKS_PROGRAM_FK FOREIGN KEY
(
Program_ID
)
REFERENCES PROGRAM
(
Program_ID
)
ON
DELETE
  NO ACTION ON
UPDATE NO ACTION
GO
1

There are 1 best solutions below

7
vmachan On

Could not put this in a comment since there is an image to be shown.

If your WORKS table has a primary key that has all 3 i.e. PROGRAMMER_ID, PROGRAM_ID and PLATFORM_ID then the MEETING table can be a child of the WORKS table and that I think may address your issue.

See below diagram

enter image description here