I’ve looked through the forums, but I can’t seem to find exactly what I’m looking for. I have a supertype, employee, and three subtypes that reference employee’s primary key, ID. The subtypes of employee must be disjoint. My problem arises in that I don’t understand where to place a constraint to make this happen.
CREATE TABLE Employee(
ID INT,
PRIMARY KEY(ID));
CREATE TABLE Manager(
ID INT,
Salary INT NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(ID) REFERENCES Employee(ID));
CREATE TABLE Server(
ID INT,
Tips INT NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(ID) REFERENCES Employee(ID));
CREATE TABLE Hostess(
ID INT,
hourly_sal INT NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(ID) REFERENCES Employee(ID));
I thought to create the constraint via a view of intersecting values, then a constraint limiting the view’s entries to null only, as shown below:
CREATE VIEW EMPLOYEE_DISJOINT AS
((SELECT ID FROM Server)INTERSECT (SELECT ID FROM Hostess))
UNION
((SELECT ID FROM Hostess) INTERSECT (SELECT ID FROM Manager))
UNION
((SELECT ID FROM Server) INTERSECT (SELECT ID FROM Manager));
ALTER VIEW EMPLOYEE_DISJOINT
ADD CONSTRAINT disjoint CHECK(ID = NULL);
Seeking to create a constraint on the view requiring all of the primary keys in Employee to be unique to one and only one subtype of employee. Is there a better way to do this? While this method seems like it ought to work, I get the following error:
ADD CONSTRAINT disjoint CHECK(ID = NULL)
*
ERROR at line 2:
ORA-00922: missing or invalid option
Please help or point me in the direction of somewhere I may find it! Thanks a ton!
You could use a materialized view and add the
CHECK (ID=NULL)constraint, but a simpler method might be just to have a discriminator column on the Employee table, e.g.employee_typewith valid values('Manager', 'Server', 'Hostess').