0% found this document useful (0 votes)
3 views

Lab10 SQL Statements

The document outlines SQL statements for ensuring database integrity and security through various constraints, triggers, and privilege grants. It includes examples of creating tables with constraints on employee salaries and manager ages, as well as granting specific privileges to users for manipulating data in the Employee and Department tables. Additionally, it demonstrates how to create views for limited data access and privilege propagation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab10 SQL Statements

The document outlines SQL statements for ensuring database integrity and security through various constraints, triggers, and privilege grants. It includes examples of creating tables with constraints on employee salaries and manager ages, as well as granting specific privileges to users for manipulating data in the Employee and Department tables. Additionally, it demonstrates how to create views for limited data access and privilege propagation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Statements for Lab 10 - Database

Integrity and Security


1. Relational Schema Questions:

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;

GRANT SELECT ON EvaEmployeeView TO Eva WITH GRANT OPTION;

You might also like