8/2/2019 Report of Assignment
1/28
DWH Assignment 3Faiza Tariq
08-0161
Section-A
8/2/2019 Report of Assignment
2/28
Table of ContentsSoft Referential Integrity ......................................................................................................................................................... 3
Constraints .............................................................................................................................................................................. 3
CREATE DATABASE: ................................................................................................................................................................. 4
CREATE TABLES: ...................................................................................................................................................................... 6
Department Table ............................................................................................................................................................... 7
Employee Table ................................................................................................................................................................... 8
HR Table .............................................................................................................................................................................. 9
Security Table .................................................................................................................................................................... 10
Projects Table .................................................................................................................................................................... 11
Tables Created: ..................................................................................................................................................................... 12
INSERTION QUERIES: ............................................................................................................................................................. 13
Department Table Insertion .............................................................................................................................................. 14
Employee Table Insertion ................................................................................................................................................. 15
Projects Table Insertion .................................................................................................................................................... 16
Security Table Insertion .................................................................................................................................................... 17
HR Table Insertion ............................................................................................................................................................. 18
QUERIES: ............................................................................................................................................................................... 19
1.1 Which employees have not been assigned a project ........................................................................................... 20
1.2 Which employees have a Level of 12 and have a salary of 150K .......................................................................... 21
1.3 Who are the five level manager of all employees ................................................................................................ 22
1.4 Which employees earn less than average ............................................................................................................ 23
1.5 Which employees earn greater than average ....................................................................................................... 24
1.6 Who are the three employees who earn the most .............................................................................................. 25
1.7 Who are the thee employees who earn the least ................................................................................................ 26
1.8 Who are the managers of the employees, whose project have passed the deadline.......................................... 27
8/2/2019 Report of Assignment
3/28
Soft Referential Integrity
"Hard" referential integrity is the "normal" referential integrity that enforces any RI constraints and ensures that any
data loaded into the tables meets the RI rules.
"Soft" referential integrity is a feature that is more about accessing the data than about loading it. Soft referential
integrity does not enforce any RI constraints. However, when we specify soft RI, we are telling the optimizer that the
foreign key references do exist. Therefore, it is your job to make sure that is true.
For example, if I have a sales table that references a store number, I could set up a soft referential integrity constraint
between the "sales" table and the "store" table. That would tell the optimizer that all store numbers that exist in the
sales table also exist in the store table (even if they don't actually exist). The optimizer can then use this information to
avoid joining to the store table. For example, if I coded an inner join betwen the sales table and the store table, and I did
not reference any other columns from the store table except for the store number, the optimizer could avoid actually
joining to the store table at all.
This is useful since we sometimes set up views to join all of the "dimension" tables to the "fact" table in a view to make
things easier. By using soft RI, the optimizer can join only to the dimension tables that it needs to and can avoid joins tothe rest.
When we set up soft referential integrity, we have to ensure that the foreign key does in fact exist if we want to get the
"correct" answer. Otherwise, the optimizer may avoid joining to the foriegn key table and will produce an incorrect
answer.
ConstraintsImplemented the following constraints while implementing the above mentioned tables.
Emp_ID cant be null EMp_Name cant be null CNIC cant be null DEPT_ID should only be between 01 and 10 A department must have a manager Basic_Pay should not be less than 30000 Level should not be less than 5 or greater than 13 Projects mush have a start date and a deadline Project_id cant be null If any employee has a vehicle then he should be a enrolled employee
8/2/2019 Report of Assignment
4/28
CREATE DATABASE:CREATEDATABASECompany_Employees_080161FROMdbcASPERMANENT=1073741824,SPOOL=419430400,TEMPORARY=419430400;
8/2/2019 Report of Assignment
5/28
8/2/2019 Report of Assignment
6/28
CREATE TABLES:
8/2/2019 Report of Assignment
7/28
Department TableCREATETABLECompany_Employees_080161.Department(Dept_IDintBETWEEN1AND10NOTNULLPRIMARYKEY,Dept_Namevarchar(25)NOTNULL,Manager_IDint)
8/2/2019 Report of Assignment
8/28
Employee TableCREATETABLECompany_Employees_080161.Employees(Emp_IDintNOTNULL,Emp_Namevarchar(50)NOTNULL,PrimaryKey(Emp_Id),CNICintNOTNULL,Phone_Noint,Cell_NOint,
Date_of_Birthdate,Dept_IDint,Locationvarchar(30),CONSTRAINTfk_Dept_IDFOREIGNKEY(Dept_ID)REFERENCESCompany_Employees_080161.Department(Dept_ID))
8/2/2019 Report of Assignment
9/28
HR TableCREATETABLECompany_Employees_080161.HR(Emp_IDintNOTNULL,CONSTRAINTFK_Emp_IDFOREIGNKEY(Emp_ID)REFERENCESCompany_Employees_080161.Employees(Emp_ID),Emp_Namevarchar(30),LevelintCHECK(Level>=5ANDLevel=30000))
8/2/2019 Report of Assignment
10/28
Security TableCREATETABLECompany_Employees_080161.Security(Emp_IDintNOTNULL,Dept_IDintNOTNULL,Vehicle_Noint,Vehicle_Descvarchar(100),
CONSTRAINTFK_Emp_IDFOREIGNKEY(Emp_ID)REFERENCESCompany_Employees_080161.Employees(Emp_ID),CONSTRAINTFK_Dept_IDFOREIGNKEY(Dept_ID)REFERENCESCompany_Employees_080161.Department(Dept_ID),PRIMARYKEY(Emp_ID,Dept_ID))
8/2/2019 Report of Assignment
11/28
Projects TableCREATETABLECompany_Employees_080161.Projects(Project_IDintNOTNULL,PRIMARYKEY(Project_ID),Project_Namevarchar(30)NOTNULL,Emp_IDint,Deadlinedate,
Start_Datedate,CONSTRAINTfk_Emp_IDFOREIGNKEY(Emp_ID)REFERENCESCompany_Employees_080161.Employees(Emp_ID))
8/2/2019 Report of Assignment
12/28
Tables Created:
8/2/2019 Report of Assignment
13/28
INSERTION QUERIES:
8/2/2019 Report of Assignment
14/28
Department Table InsertionINSERTINTOcompany_employees_080161.Department(Dept_ID,Dept_Name,Manager_ID)Values(1,'BBA',1);INSERTINTOcompany_employees_080161.Department(Dept_ID,Dept_Name,Manager_ID)Values(2,'BSCS',2);INSERTINTOcompany_employees_080161.Department(Dept_ID,Dept_Name,Manager_ID)Values(3,'BSTE',3);INSERTINTOcompany_employees_080161.Department(Dept_ID,Dept_Name,Manager_ID)Values(4,'BSCE',4);INSERTINTOcompany_employees_080161.Department(Dept_ID,Dept_Name,Manager_ID)Values(5,'BSIT',5);
8/2/2019 Report of Assignment
15/28
Employee Table InsertionINSERTINTOCompany_Employees_080161.Employees(Emp_Id,Emp_Name,CNIC,Phone_No,Cell_No,Date_of_Birth,Dept_Id,Location)Values(1,'Faiza',678,819026,7819273,'1987-08-10',1,'Islamabad');INSERTINTOCompany_Employees_080161.Employees(Emp_Id,Emp_Name,CNIC,Phone_No,Cell_No,Date_of_Birth,Dept
_Id,Location)Values(2,'Saima',009,991726,9156207,'1989-07-11',2,'Lahore');INSERTINTOCompany_Employees_080161.employees(Emp_Id,Emp_Name,CNIC,Phone_No,Cell_No,Date_of_Birth,Dept_Id,Location)Values(3,'Sara',899,218020,3620174,'1984-07-18',3,'Rawalpindi');INSERTINTOCompany_Employees_080161.employees(Emp_Id,Emp_Name,CNIC,Phone_No,Cell_No,Date_of_Birth,Dept_Id,Location)Values(4,'Sidrah',120,780162,0183746,'1980-02-19',4,'Karachi');INSERTINTOCompany_Employees_080161.employees(Emp_Id,Emp_Name,CNIC,Phone_No,Cell_No,Date_of_Birth,Dept_Id,Location)Values(5,'Tooba',883,601726,1092736,'1984-07-22',5,'Faisalabad');
8/2/2019 Report of Assignment
16/28
Projects Table InsertionINSERTINTOcompany_employees_080161.Projects(Project_ID,Project_Name,Emp_ID,Deadline,Start_Date)Values(1,'Game',1,'2011-06-12','2011-05-11');INSERTINTOcompany_employees_080161.Projects(Project_ID,Project_Name,Emp_ID,Deadline,Start_Date)Values(2,'Anroidproject',2,'2011-04-12','2011-05-20');INSERTINTOcompany_employees_080161.Projects(Project_ID,Project_Name,Emp_ID,Deadline,Start_Date)Values(3,'Testingsoftware',3,'2011-06-10','2011-06-08');INSERTINTOcompany_employees_080161.Projects(Project_ID,Project_Name,Emp_ID,Deadline,Start_Date)Values(4,'Networking',4,'2011-09-11','2011-01-09');
INSERTINTOcompany_employees_080161.Projects(Project_ID,Project_Name,Emp_ID,Deadline,Start_Date)Values(5,'AeroWIFI',5,'2011-04-12','2011-05-05');
8/2/2019 Report of Assignment
17/28
Security Table InsertionINSERTINTOcompany_employees_080161.Security(Emp_Id,Dept_Id,Vehicle_No,Vehicle_Desc)Values(1,4,1,'CAR');INSERTINTOcompany_employees_080161.Security(Emp_Id,Dept_Id,Vehicle_No,Vehicle_Desc)Values(2,3,2,'BUS');INSERTINTOcompany_employees_080161.Security(Emp_Id,Dept_Id,Vehicle_No,Vehicle_Desc)Values(3,5,3,'CAR');INSERTINTOcompany_employees_080161.Security(Emp_Id,Dept_Id,Vehicle_No,Vehicle_Desc)Values(4,2,4,'VAN');INSERTINTOcompany_employees_080161.Security(Emp_Id,Dept_Id,Vehicle_No,Vehicle_Desc)Values(5,1,5,'BICYCLE');
8/2/2019 Report of Assignment
18/28
HR Table InsertionINSERTINTOcompany_employees_080161.HR(Emp_Id,EMP_Name,Level,Basic_Pay)Values(1,'Saima',6,55000);INSERTINTOcompany_employees_080161.HR(Emp_Id,EMP_Name,Level,Basic_Pay)Values(2,'Faiza',7,59000);INSERTINTOcompany_employees_080161.HR(Emp_Id,EMP_Name,Level,Basic_Pay)Values(3,'Tooba',5,85000);INSERTINTOcompany_employees_080161.HR(Emp_Id,EMP_Name,Level,Basic_Pay)Values(4,'Hina',9,89000);INSERTINTOcompany_employees_080161.HR(Emp_Id,EMP_Name,Level,Basic_Pay)Values(5,'Huma',7,99000);INSERTINTOcompany_employees_080161.HR(Emp_Id,EMP_Name,Level,Basic_Pay)Values(4,'Haniya',12,900000);
8/2/2019 Report of Assignment
19/28
QUERIES:
8/2/2019 Report of Assignment
20/28
1.1Which employees have not been assigned a projectSelect*fromcompany_employees_080161.EmployeeswhereEmp_IDnotin(selectEmployees.Emp_IDfromcompany_employees_080161.Employees,company_employees_080161.ProjectswhereEmployees.Emp_ID=Projects.Emp_ID)
8/2/2019 Report of Assignment
21/28
1.2Which employees have a Level of 12 and have a salary of 150KSELECTcompany_employees_080161.employees.Emp_ID,company_employees_080161.employees.Emp_Namefromcompany_employees_080161.employeesjoincompany_employees_080161.HRONcompany_employees_080161.HR.Emp_ID=company_employees_080161.employees.Emp_IDANDcompany_employees_080161.HR.Level=11ANDcompany_employees_080161.HR.Basic_Pay=1500000
8/2/2019 Report of Assignment
22/28
1.3Who are the five level manager of all employeesSELECTcompany_employees_080161.employees.Emp_ID,company_employees_080161.employees.Emp_Namefromcompany_employees_080161.employeesjoincompany_employees_080161.HRONcompany_employees_080161.HR.Emp_ID=company_employees_080161.employees.Emp_IDANDcompany_employees_080161.HR.Level=5
8/2/2019 Report of Assignment
23/28
1.4Which employees earn less than averageSELECTcompany_employees_080161.HR.Emp_ID,company_employees_080161.HR.Emp_Namefromcompany_employees_080161.HRWHEREcompany_employees_080161.HR.Basic_Pay
8/2/2019 Report of Assignment
24/28
1.5Which employees earn greater than averageSELECTcompany_employees_080161.HR.Emp_ID,company_employees_080161.HR.Emp_Namefromcompany_employees_080161.HRWHEREcompany_employees_080161.HR.Basic_Pay>(SELECTAVG(company_employees_080161.HR.Basic_Pay)FROMcompany_employees_080161.HR)
8/2/2019 Report of Assignment
25/28
1.6Who are the three employees who earn the mostSELECTTOP3*fromcompany_employees_080161.HRORDERBYcompany_employees_080161.HR.Basic_PayDESC
8/2/2019 Report of Assignment
26/28
1.7Who are the thee employees who earn the leastSELECTTOP3*FROMcompany_employees_080161.HRORDERBYcompany_employees_080161.HR.Basic_PayASC
8/2/2019 Report of Assignment
27/28
1.8Who are the managers of the employees, whose project have passedthe deadline
SELECTCompany_Employees_080161.DEPARTMENT.MANAGER_IDFROMCompany_Employees_080161.DEPARTMENTWHERECompany_Employees_080161.DEPARTMENT.DEPT_IDIN(SELECTCompany_Employees_080161.EMPLOYEES.DEPT_IDFROMCompany_Employees_080161.EMPLOYEESJOINCompany_Employees_080161.PROJECTSONCompany_Employees_080161.EMPLOYEES.EMP_ID=Company_Employees_080161.PROJECTS.EMP_IDWHERECURRENT_DATE>Company_Employees_080161.PROJECTS.DEADLINE);
8/2/2019 Report of Assignment
28/28