Creating Schemas and Queries
Creating Schemas and Queries
Creating Schemas and Queries
Q1
1. Print the names and ages of each employee who works in both the Hardware department and
the Software department.
SELECT E.ENAME, E.AGE FROM EMP E, DEPT D1, DEPT D2, WORKS W1, WORKS W2
WHERE
E.EID = W1.EID AND W1.DID = D1.DID AND D1.DNAME ='HARDWARE' AND
E.EID = W2.EID AND W2.DID = D2.DID AND D2.DNAME ='SOFTWARE'
OR
2. For each department with more than 20 full-time-equivalent employees (i.e., where the part-
time and full-time employees add up to at least that many full-time employees), print the did
together with the number of employees that work in that department.
3. Find the managerids of managers who manage only departments with budgets greater than
$1 million.
4. Find the enames of managers who manage the departments with the largest budgets.
OR
5. Find the enames of managers who manage only departments with budgets larger than $1
million, but at least one department with budget less than $5 million.
Q2
Given Schemas
Student (snum: integer, sname: string, major: string, level: string, age: integer)
name char(20),
meets_at time,
room char (10),
fid integer,
PRIMARY KEY (name),
FOREIGN KEY (fid) REFEFENCES Faculty(fid)
ON DELETE SET TO NULL //do not want to delete course another faculty may be assigned later
ON UPDATE CASCADE
)
snum integer,
cname char(20),
PRIMARY KEY (snum,cname),
FOREIGN KEY (snum) REFEFENCES Student(snum)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (cname) REFERENCES Class(name)
ON DELETE CASCADE
ON UPDATE CASCADE
)
fid integer,
fname char(30),
deptid integer,
PRIMARY KEY (fid)
)
(a) Every class has a minimum enrollment of 5 students and a maximum enrollment of 30
students.
snum integer,
cname char(20),
PRIMARY KEY (snum,cname),
FOREIGN KEY (snum) REFEFENCES Student(snum),
FOREIGN KEY (cname) REFERENCES Class(name)
CHECK (( SELECT COUNT (E.snum) FROM ENROLLED E GROUP BY E.cname) >=5 ),
CHECK (( SELECT COUNT (E.snum) FROM ENROLLED E GROUP BY E.cname) <=30)
)
Operations to be monitored :
Insert and delete on STUDENTS needs to be monitored so that the no of students enrolled for a
course remain between 5 and 30. Also Insert and Delete on ENROLLMENT needs to be
monitored
Operations to be monitored :
Operations to be monitored :
Delete on Enrollment needs to be monitored, so as the enrollment for Math101 is not removed
fid integer,
fname char(30),
deptid integer,
PRIMARY KEY (fid),
CHECK ( (SELECT COUNT(*) FROM FACULTY F
GROUP BY deptid HAVING COUNT (*) > 10) =0)
)
Operations to be monitored :
Insert on faculty needs to be monitored, if there are 10 faculty members in a deptid, addition of
another faculty in the same deptid will violate this constraint.
(f) The number of CS majors must be more than the number of Math majors.
snum integer,
sname char (30),
major char (10),
level char (10),
age integer,
PRIMARY KEY (snum),
CHECK ( ( SELECT COUNT(*) FROM STUDENTS S WHERE MAJOR = ‘CS’) >
(SELECT COUNT(*) FROM STUDENTS WHERE MAJOR =’Math’))
)
Operations to be monitored :
Insert and delete on ENROLLMENT needs to be to maintain the above constraint on majors