CIS611 Lab Assignment 1 SS Chung 1. Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying Over the database with SQL 2. Automatic Creation and Maintenance of Database from Web Interface 3. Transforming a SQL Query into an Execution Plan in Relational Algebra for Query Processing For those who have never taken CIS430/530: Part 1, Part 2, Part 3, and Part 4 are required as Lab1. For those who have taken CIS430/530: Part 3, Extra Part3, and Part 4 are required as Lab1. 1. Creating the COMPANY database schema using SQL (DDL) statements. The COMPANY database schema is depicted in the Figure 5.7 in the back of this lab. Core data for this database is shown in the Figure in the back as well. Write the corresponding SQL commands (DDL) to create all 6 Tables as shown in Figure 5.7. The typical SQL commands (DDL) for this lab are as follow: Drop table Employee; Create table Employee (...); ALTER TABLE EMPLOYEE ADD COLUMN ALTER TABLE EMPLOYEE ADD CONSTRAINT ALTER TABLE EMPLOYEE DROP COLUMN ; ALTER TABLE EMPLOYEE DROP CONSTRAINT ; ALTER TABLE EMPLOYEE ALTER COLUMN ; Select * From Employee; Tips: Create each table first without PK-FK Constraint, and populate tables with initial data. Then add each PK-FK Constraint with Alter table command. 2. Populating the COMPANY database using SQL (DML) statements. The typical SQL commands (DML) to populate a table and show the rows inserted into the table for this task are like the following example. INSERT INTO EMPLOYEE VALUES('John','B','Smith','123456789','9-Jan-55','731 Fondren, Houston, TX','M',30000,'987654321',5); Select * From Employee;
Show your SQL statements and the result in a Word document. Add screenshots showing your SQLs and the results to show the content of each table after populating it. 3. Write SQL as follow: Part1 on View: 1-1) Create a view named VDept_Budget that reports headcount for each department. The report includes 3 columns as follow: Dept_Name, Dept_Number, No_Emp. Include all the departments. Show the content of the view through SQL (Select * from VDept_Budget;) 1-2) Add yourself to the database (to Employee table). Then Show the content of your view again to see if your view is updated according to the changes in its base table Employee. 1-3) Then Change your view to add two more info Sum_Salary, Ave_Salary for each department. Include all the departments. Your report (view) lists 5 Columns as follow: Dept_Name, Dept_Number, No_Emp, Sum_Salary, Ave_Salary Show the content of your changed view to report the updated info after changes in the view and the database. Show your SQL statements and the result of each step in a Word document. Add screenshots showing your SQLs and the results to show your report is updated before and after the changes in the database. Part 2_1 on Stored Procedure 1: Create Stored Procedure SP_ Dept_Productivity that does the following tasks: 1. It creats a new table Dept_Productivity The table has 5 Columns as follow: Dept_Name, Dept_Number, EmpCount, Sum_Salary, ProjectCount Use Create Table without any constraints. 2. Use Insert Select to populate the table. Write Select query to join with the View VDept_Budget that you created in Part 1-3) to list Employee Headcount, Sum of Salary, The Number of the Projects that each department controls with dnumber and dname for each department.
PopuIate the table with Insert Select. Include all the departments in the list. 3. Show the content of the table with (Select * from Dept_Productivity;) Execute your stored procedure to get the output with Exec SP_Dept_Productivity ; Output (snapshot of screen) each part to show the creation of the View and the updated Views, the contents of the view and the updated view for the Part 1. For Part 2, Screenshot for each step to show Creation of the Stored Procedure, and Execution the SP to show the contents of the new table. Part 2_2 on Stored Procedure 2: Write a Stored Procedure SP_Report_ NEW_Budget using the view you created in Part 1-3). Use Cursor for this task. Your Stored Procedure SP_Report_ NEW_Budget does the following tasks: It creates a new table NEW_Dept_Budget as follow: NEW_Dept_Budget has 5 columns Dept_No (Int) Dept_Name (Char(30)) COUNT_Emp (INT) New_SUM_Salary (INT) New_AVE_Salary (INT) 1) Check if the view VDept_Budget is empty or not (by counting rows from the view). 2) If not empty, For each row of the view VDept_Budget, Calculate New_SUM_Salary, New_AVE_Salary as follow: If Dept = 1, Increase SUM(salary) 10% If Dept = 4, Increase SUM(salary) 20% If Dept = 5, Increase SUM(salary) 30% If Dept = 7, Increase SUM(salary) 40% 3) Insert each column value into the new table NEW_Dept_Budget. Output (snapshot of screen) each Function and Procedure created and Show all the tables contents with Select statement in your output.
For those who have taken CIS430/530, Part 3, Extra Part3, and Part 4 are required as Lab1. Part 3 on Table Function 1. Write a Table Function Total_Salary_By_Dept that returns a table Total_Salary_By_Dept _Table with Five column info as follow: It returns Total_Salary_By_Dept _Table that has the following Five column values. Dept_No (Int) Dept_Name (Char(30)) COUNT_Emp (INT) SUM_Salary (INT) AVE_Salary (INT) From underlying Company database 2. Write a Stored Procedure Report_ NEW_Total_Salary_By_Dept using (by calling) the Table Function Total_Salary_By_Dept, the one that was created in the part 1. Use CURSOR to write the Stored Procedure. Your Stored Procedure Report_ NEW_Total_Salary_By_Dept does the following tasks: It returns a new table NEW_Total_Salary_By_Dept as follow: NEW_Total_Salary_By_Dept has 7 columns Dept_No (Int) Dept_Name (Char(30)) COUNT_Emp (INT) SUM_Salary (INT) AVE_Salary (INT) New_SUM_Salary (INT) New_AVE_Salary (INT) 1) Check if the table Total_Salary_By_Dept _Table is empty or not. 2) If not empty, For each row of the table Total_Salary_By_Dept _Table, Calculate New_SUM_Salary, New_AVE_Salary as follow: If Dept = 1, Increase SUM(salary) 10% If Dept = 4, Increase SUM(salary) 20% If Dept = 5, Increase SUM(salary) 30% If Dept = 7, Increase SUM(salary) 40%
3) Insert each column value into the new table NEW_Total_Salary_By_Dept Output (snapshot of screen) each Function and Procedure created and Show all the tables contents with Select statement in your output. Extra Part 3: Required For those who have taken CIS430/530 Extra Credit For those who have never taken CIS430/530 For this part, you can use any language of your choice with your choice of a Platform: C#/Java with SQL Server or PHP with MYSQL on WAMP. See the example codes on the class webpage for these. 1. Create a company webpage where a new employee can add his/her information into the Employee table by taking a new employee information with 9 Input Boxes for each column Fname, MInit, Lname,, Dno as in Employee table schema With ADD button. 2. Write an Application Program that takes the new employee s input from the webpage and call a Stored Procedure SP_Insert_NewEmployee with the 9 input parameters. 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs to Insert a new employee tuple into the Employee table 3-2. Find all the projects that are controlled by the department that the new employee works for. 3-3. Add all the project numbers with the new employee into Works_On table. Output (snapshot of screen) of Webpage and Stored Procedure created and Show all the tables contents with Select statement in your output.
Part 4: 4. Update the following new changes into the database: 1) Joyce English with Ssn = 453453453 got married with John Anderson. 2) Jenifer Wallace with Ssn = 987654321 just had a new daughter named Erica. 3) Jenifer Wallace with Ssn = 987654321 is just assigned to a new project number 10 and 10 hours to work on. Add these new entries into Dependent, Works_On tables in your database then Select * from Dependent and Select * from Works_On to show the updated table contents. 5. Write SQL Select statements to retrieve data in the followings: Q1: Make a list of all project numbers that Research department employees are working on, either as a worker or as a manager of the department that controls the projects. Write Q1 in Relational Algebra: Q2: Get SSN and the last name of married female employees who work on three or more projects Write Q2 in Relational Algebra: Q3. Write SQL using View instead of Subquery to express the subquery Q2. Q4: Get the Last and the First name of married employees who only have daughters. (Hint: Identify the following three sets in SQL first then write a main SQL using them. Married = Select ESSN From Dependent Where relationship = spouse ; Girls = Select ESSN From Dependent Where relationship = daughter ; Boys = Select ESSN From Dependent Where relationship = son ; Q4 in Relational Algebra: Q4 in Relational Tuple Calculus:
COMPANY DATABASE EMPLOYEE FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO John B Smith 123456789 09-Jan-55 731 Fondren, Houston, TX M 30000 987654321 5 Franklin T Wong 333445555 08-Dec-45 638 Voss, Houston, TX M 40000 888665555 5 Joyce A English 453453453 31-Jul-62 5631 Rice, Houston, TX F 25000 333445555 5 Ramesh K Narayan 666884444 15-Sep-52 975 Fire Oak, Humble, TX M 38000 333445555 5 James E Borg 888665555 10-Nov-27 450 Stone, Houston, TX M 55000 1 Jennifer S Wallace 987654321 20-Jun-31 291 Berry, Bellaire, TX F 43000 888665555 4 Ahmad V Jabbar 987987987 29-Mar-59 980 Dallas, Houston, TX M 25000 987654321 4 Alicia J Zelaya 999887777 19-Jul-58 3321 Castle, SPring, TX F 25000 987654321 4 DEPARTMENT DNAME DNUMBER MGRSSN MGRSTARTDATE Headquarters 1 888665555 19-Jun-71 Administration 4 987654321 01-Jan-85 Research 5 333445555 22-May-78 Automation 7 123456789 06-Oct-05 DEPENDENT ESSN DEPENDENT_NAME SEX BDATE RELATIONSHIP 123456789 Alice F 31-Dec-78 Daughter 123456789 Elizabeth F 05-May-57 Spouse 123456789 Michael M 01-Jan-78 Son 333445555 Alice F 05-Apr-76 Daughter 333445555 Joy F 03-May-48 Spouse 333445555 Theodore M 25-Oct-73 Son 987654321 Abner M 29-Feb-32 Spouse DEPT_LOCATIONS DNUMBER DLOCATION 1 Houston 4 Stafford 5 Bellaire 5 Sugarland 5 Houston PROJECT PNAME PNUMBER PLOCATION DNUM ProductX 1 Bellaire 5 ProductY 2 Sugarland 5 ProductZ 3 Houston 5 Computerization 10 Stafford 4 Reorganization 20 Houston 1 Newbenefits 30 Stafford 4 WORKS_ON ESSN PNO Hours 123456789 1 32.5 123456789 2 7.5 333445555 2 10 333445555 3 10 333445555 10 10 333445555 20 10 453453453 1 20 453453453 2 20 666884444 3 40 888665555 20 987654321 20 15 987654321 30 20 987987987 10 35 987987987 30 5 999887777 10 10 999887777 30 30