Sheet 5 PRACTICE
Sheet 5 PRACTICE
Solution:
CREATE Database company
CREATE TABLE EMPLOYEE (SSN int PRIMARY KEY, FNAME TEXT NOT NULL, LNAME TEXT NOT
NULL, BDATE DATE, ADDRESS TEXT, GENDER CHAR, SALARY money, DID int, SUPERSSN
int)
CREATE TABLE DEPARTMENT (DNO int PRIMARY KEY, DNAME TEXT, MGRSSN int, MGRSTDATE
DATE)
CREATE TABLE WORKSFOR (ESSN int NOT NULL, PRNO int NOT NULL, HOURS int)
CREATE TABLE DEPENDANT (ESSN int NOT NULL, DEPNAME TEXT, GENDER CHAR, BDATE
DATE)
CREATE TABLE PROJECT (PNO int PRIMARY KEY, PNAME TEXT NOT NULL, LOC TEXT, CITY
TEXT, DNO int, FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNO))
ALTER TABLE DEPENDANT ADD FOREIGN KEY (ESSN) REFERENCES EMPLOYEE (SSN) ON update
set null
ALTER TABLE WORKSFOR ADD FOREIGN KEY (ESSN) REFERENCES EMPLOYEE (SSN)
ALTER TABLE WORKSFOR ADD FOREIGN KEY (PRNO) REFERENCES PROJECT (PNO)
ALTER TABLE DEPENDANT ADD CONSTRAINT PK2 PRIMARY KEY (ESSN, DEPNAME)
CREATE TABLE XX (ID int PRIMARY KEY, NAME TEXT, DID int, CONSTRAINT FK FOREIGN
KEY (DID) REFERENCES DEPARTMENT(DNO)ON DELETE CASCADE)
CREATE TABLE ZZ (ID int, NAME TEXT, DID int, CONSTRAINT FF FOREIGN KEY (DID)
REFERENCES DEPARTMENT(DNO)ON DELETE set null, CONSTRAINT PK3 PRIMARY KEY (ID))
DROP TABLE xx
After creating tables and inserting data of the company schema, please perform
the following commands:
3. In the department table insert new department called "DEPT IT" , with id
100, employee with SSN = 112233 as a manager for this department. The
start date for this manager is '1-11-2006'
insert into department(Dname,Dno,Mgrssn ,[mgrstdate]) values('DEPT
IT',100,112233,'1-11-2006')
5. Unfortunately the company ended the contract with Mr.Kamel Mohamed so try
to delete his data from your database, in case you know that Hanan Sobhy
will be temporary in his position.
UPDATE EMPLOYEE SET Superssn = 123456 WHERE superssn=223344;
UPDATE Department SET MGRSSN = 123456 WHERE MGRSSN=223344;
DELETE FROM dependant WHERE ESSN=223344;
DELETE FROM worksfor WHERE ESSN=223344;
DELETE FROM EMPLOYEE WHERE SSN=223344;
7. Display the Projects full data of the projects with a name starts with "a"
letter.
Select * from project where pname like 'a%'
8. Display the Id, name and location of the projects in Cairo or Alex city.
Select PNO,pname,loc from project where city like 'Cairo' or city like
'Alex'
10. If you know that the company policy is to pay an annual commission for
each employee which specific percent equals 10% of his/her annual salary
.Display each employee full name and his annual commission in an ANNUAL
COMM column (alias).
Select convert (varchar,fname)+' '+convert (varchar,lname) as full_name,
(SALARY+(salary*0.1)) as annual_comm from employee
11. Display the employees Id, name who earns between 1000 and 2000 LE monthly.
Select SSN, fname from employee where salary >=1000 and salary <=2000
Or
Select SSN, fname from employee where salary between 1000 and 2000
- The GROUP BY clause is used to create one output row per each group and produces
summary values (aggregate functions: AVG sum min max count) for the selected columns, as
shown below.
select prno, sum(hours) from worksfor group by PRNO
- The HAVING clause behaves like the WHERE clause, but is applicable to groups.
select prno, sum(hours) from worksfor where ESSN != 968574 group by PRNO
having prno !=300