Lab10 SQL Statements
Lab10 SQL Statements
a) Define a table constraint on Emp that ensures every employee makes at least P10,000.
CREATE TABLE Emp (
IDNo INTEGER,
Name VARCHAR(100),
Age INTEGER,
Salary REAL CHECK (Salary >= 10000)
);
b) Define a table constraint on Dept that ensures all managers are older than 30 years.
CREATE TABLE Dept (
Dno INTEGER,
Budget REAL,
ManagerIDNo INTEGER,
CHECK (ManagerIDNo IN (SELECT IDNo FROM Emp WHERE Age > 30))
);
c) Define an assertion/named constraint on Dept to ensure all managers are older than 30
years.
CREATE ASSERTION ManagerAgeCheck
CHECK (NOT EXISTS (
SELECT * FROM Dept D, Emp E
WHERE D.ManagerIDNo = E.IDNo AND E.Age <= 30
));
d) Write a trigger to prevent hiring employees whose salaries exceed that of the managers
of their departments.
CREATE TRIGGER SalaryCheck
BEFORE INSERT OR UPDATE ON Emp
FOR EACH ROW
WHEN (NEW.Salary > (SELECT Salary FROM Emp E, Dept D WHERE D.ManagerIDNo =
E.IDNo AND D.Dno = NEW.Dno))
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Employee salary cannot exceed
manager salary';
END;
2. Granting Privileges:
e) Grant user Alex privileges to insert and delete tuples in both Employee and Department
tables.
GRANT INSERT, DELETE ON Employee TO Alex;
GRANT INSERT, DELETE ON Department TO Alex;
f) Grant user Elisa retrieval privileges for both tables and the ability to propagate these
privileges.
GRANT SELECT ON Employee TO Elisa WITH GRANT OPTION;
GRANT SELECT ON Department TO Elisa WITH GRANT OPTION;
g) Grant user John the privilege to update only the Salary attribute of the Employee table.
GRANT UPDATE (Salary) ON Employee TO John;
h) Grant user Eva limited retrieval privileges on the Employee table (Name, Bdate, Address
for Dno=5), with propagation capability.
CREATE VIEW EvaEmployeeView AS
SELECT Name, Bdate, Address
FROM Employee
WHERE Dno = 5;