An Example of A Foreign Key Constraint Involving The Dept Relation Is The Managerid Attribute in The Dept Relation
An Example of A Foreign Key Constraint Involving The Dept Relation Is The Managerid Attribute in The Dept Relation
Dept relation, which references the eid attribute in the professor relation. When a user attempts to
delete a Dept tuple, the options for enforcing this constraint include: preventing the deletion of the
Dept tuple if there are still professors with the same managerid, cascading the deletion of all
professors with the same managerid as the Dept tuple, setting the managerid attribute to null for all
professors with the same managerid as the Dept tuple, or setting the managerid attribute to a
default value for all professors with the same managerid as the Dept tuple.
SQL statements to create the above relations with appropriate primary and foreign key constraints:
sql
Copy code
age INTEGER,
salary REAL
);
budget REAL,
managerid INTEGER,
);
eid INTEGER,
did INTEGER,
pct_time INTEGER,
);
To define the Dept relation in SQL so that every department is guaranteed to have a manager, we
can modify the definition of the Dept table to include a NOT NULL constraint on the managerid
attribute:
sql
Copy code
budget REAL,
);
SQL statement to add 'Emmanuel Kuseamo' as an employee with eid = 101, age = 32, and salary =
20000:
sql
Copy code
sql
Copy code
sql
Copy code
If the referential integrity constraint was set to prevent deletion of a Dept tuple if there are still
professors with the same managerid, the deletion of the 'Toy' department will fail if there are still
professors with managerid referencing the 'Toy' department. If the constraint was set to cascade the
deletion of all professors with the same managerid as the Dept tuple, then all professors with
managerid referencing the 'Toy' department will also be deleted. If the constraint was set to set the
managerid attribute to null or a default value for all professors with the same managerid as the Dept
tuple, then the affected professors will have their managerid attribute updated accordingly.
S2
An example of a foreign key constraint that involves the Dept relation is the managerid attribute in
the Dept relation, which references the eid attribute in the professor relation. When a user attempts
to delete a Dept tuple, the options for enforcing this constraint are either to disallow the deletion if
there are professor tuples referencing the department or to cascade the deletion by deleting all
professor tuples referencing the department.
Here are the SQL statements required to create the above relations with appropriate versions of all
primary and foreign key integrity constraints:
ename VARCHAR(50),
age INTEGER,
salary REAL
);
dname VARCHAR(50),
budget REAL,
managerid INTEGER,
);
pct_time INTEGER,
PRIMARY KEY (eid, did)
);
To define the Dept relation in SQL so that every department is guaranteed to have a manager, you
can add a NOT NULL constraint to the managerid attribute:
dname VARCHAR(50),
budget REAL,
);
The SQL statement to add ‘Emmanueal Kuseamo’ as an employee with eid = 101, age = 32 and salary
= 20,000 would be:
INSERT INTO professor (eid, ename, age, salary) VALUES (101, 'Emmanueal Kuseamo', 32, 20000);
The SQL statement to give every employee a 40% raise would be:
Given the referential integrity constraints I chose for this schema, when this statement is executed,
it will not be allowed if there are any Course tuples referencing the 'Toy' department. If there are no
such tuples, the deletion will be allowed and any professor tuples with a managerid of the deleted
department will have their managerid set to NULL.