0% found this document useful (0 votes)
11 views6 pages

Sheet 5 PRACTICE

Uploaded by

Ramy Elbasha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Sheet 5 PRACTICE

Uploaded by

Ramy Elbasha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL Server Sheet 5

Here is the schema of a company database, please implement it on any RDBMS


you like (only structure do not insert data)

Prepared by: Asmaa Zakria


SQL Server Sheet 5

Prepared by: Asmaa Zakria


SQL Server Sheet 5

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))

CREATE UNIQUE INDEX ux ON DEPARTMENT (MGRSSN);

ALTER TABLE EMPLOYEE ADD FOREIGN KEY (DID) REFERENCES DEPARTMENT(DNO)

ALTER TABLE DEPARTMENT ADD FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN)

Prepared by: Asmaa Zakria


SQL Server Sheet 5
ALTER TABLE EMPLOYEE ADD FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE (SSN) ON
update CASCADE

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 WORKSFOR ADD CONSTRAINT PK PRIMARY KEY (ESSN, PRNO)

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

CREATE INDEX IDX ON EMPLOYEE (salary)

ALTER TABLE ZZ DROP CONSTRAINT PK3

ALTER TABLE DEPENDANT ADD PLACEOFBIRTH TEXT

ALTER TABLE DEPENDANT drop column PLACEOFBIRTH

CREATE TABLE Persons (Personid int IDENTITY(1,1) PRIMARY KEY, LastName


varchar(255) NOT NULL, FirstName varchar(255), Age int)

After creating tables and inserting data of the company schema, please perform
the following commands:

1. Insert your personal data to the employee table as a new employee in


department number 30, SSN = 102672, Superssn = 112233.
INSERT INTO employee VALUES (102672, 'ABdel Wahab', 'Salah', '1/1/1980',
'55 new takseem - helwan', 'M', 1500, 30,112233);

Prepared by: Asmaa Zakria


SQL Server Sheet 5
2. Insert another employee with personal data your friend as new employee in
department number 30 , SSN = 102660, but don’t enter any value for salary
or manager number to him.
INSERT INTO employee ( fname, lname, ssn, Bdate, Address, gender, did
)VALUES ('Mohamed', 'nabeel', 102660, '1/1/1980', '55 new takseem -
helwan', 'M', 30);

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')

4. Do what is required if you know that : Mrs.Noha Mohamed moved to be the


manager of the new department (id = 100), and they give you her position
a. First try to update her record in your database.
UPDATE department SET MgrSSN = 968574 WHERE dno=100;

b. Update your record to be department 20 manager .


UPDATE department SET MgrSSN = 102672 WHERE dno=20;

c. Update your friend data to be in your teamwork (supervised by you)


UPDATE Employee SET Superssn = 102672 WHERE ssn=102660;

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;

6. Her salary has been upgraded by 20 present of its last value.


update employee set salary=salary+(salary*0.2) where ssn=123456;

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'

Prepared by: Asmaa Zakria


SQL Server Sheet 5
9. Display all the employees Data.
Select * from employee

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

12. Display the names and salaries of the female employees


Select fname,salary from employee where gender='f'

Order By, having, and Group By Clause in SQL

- Order By is used to sort the data in ascending or descending order


SELECT Fname, salary FROM employee ORDER BY salary DESC

- 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

Prepared by: Asmaa Zakria

You might also like