Datasheet

(
CONSTRAINT PK_TimeSheets PRIMARY KEY (TimeSheetID)
);
ALTER TABLE TimeSheetItems ADD
(
CONSTRAINT PK_TimeSheetItems PRIMARY KEY (TimeSheetItemID)
);
4. Finally, you need to create the foreign key relationships between your tables, so enter and exe-
cute the following script:
---------------------------------------------------------------------
-- Foreign Key Relationships
---------------------------------------------------------------------
ALTER TABLE GroupProjects ADD
(
FOREIGN KEY (GroupID) REFERENCES Groups (GroupID),
FOREIGN KEY (ProjectID) REFERENCES Projects (ProjectID)
);
ALTER TABLE Users ADD
(
FOREIGN KEY (ManagerID) REFERENCES Users (UserID),
FOREIGN KEY (GroupID) REFERENCES Groups (GroupID),
FOREIGN KEY (RoleID) REFERENCES Roles (RoleID)
);
ALTER TABLE TimeSheets ADD
(
FOREIGN KEY (UserID) REFERENCES Users (UserID),
FOREIGN KEY (ManagerID) REFERENCES Users (UserID)
);
ALTER TABLE TimeSheetItems ADD
(
FOREIGN KEY (TimeSheetID) REFERENCES TimeSheets (TimeSheetID),
FOREIGN KEY (ProjectID) REFERENCES Projects (ProjectID)
);
How It Works
The first script that you run creates all the tables in the Oracle schema shown in Figure 1-11. At this
point, the primary keys are not defined so you run the second script to alter the tables to add the
constraints that set the primary key in each table. Finally, you run the last script to create the foreign key
relationships between the tables.
29
Databases
04_58894x ch01.qxd 10/13/05 5:54 PM Page 29