Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Faculty of Computer Science and Engineering Ho Chi Minh City University of Technology INDIVIDUAL PRACTICE ON SELECT – CREATE VIEW INSERT – DELETE - UPDATE Database Systems (CO2013) Computer Science Program Dr. Võ Thị Ngọc Châu ([email protected]) Semester 1 – 2020-2021 Main references Text: [1] R. Elmasri, S. R. Navathe, Fundamentals of Database Systems- 6th Edition, Pearson- Addison Wesley, 2011. R. Elmasri, S. R. Navathe, Fundamentals of Database Systems- 7th Edition, Pearson, 2016. References: [1] S. Chittayasothorn, Relational Database Systems: Language, Conceptual Modeling and Design for Engineers, Nutcha Printing Co. Ltd, 2017. [3] A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts – 6th Edition, McGraw-Hill, 2006. [4] H. G. Molina, J. D. Ullman, J. Widom, Database Systems: The Complete Book - 2nd Edition, Prentice-Hall, 2009. [5] R. Ramakrishnan, J. Gehrke, Database Management Systems - 2nd Edition, McGraw-Hill. [6] M. P. Papazoglou, S. Spaccapietra, Z. Tari, Advances in ObjectOriented Data Modeling, MIT Press, 2000. [7]. G. Simsion, Data Modeling: Theory and Practice, Technics Publications, LLC, 2007. 2 DML Statements What you will do on the COMPANY database Write SELECT statements for the queries and CREATE VIEW statements for virtual tables Write INSERT statements to populate data into the COMPANY database Write DELETE statements to remove data from the COMPANY database Write UPDATE statements to modify data in the COMPANY database PREPARATION with the given files Execute the DDL statements for the COMPANY database schema Execute the INSERT statements to initialize the COMPANY database 3 The COMPANY Database 4 The COMPANY database Source: [1] 5 Practice 1 1. Return the department number and name of each department managed by Smith, who does not live in Houston. 2. Return the department number and name of each department managed by Smith and controlling projects in Houston. 3. Return the department number and name of each department in Houston but not controlling any project in Houston. 4. Return the department number and name of each department that has the highest number of employees. 5. Create a virtual table that keeps the details of each department in Houston: department_number, department_name, number_of_projects, total_hours. 6. Create a virtual table that keeps the details of each department with: dnumber, dname, no_of_employees, avg_salary. 7. Return the department number and name of each department that has the less number of employees but higher average salary as compared to department 4. 8. Insert department 3, named Logistics, managed by employee 987987987, from the same time when department Research has been managed. 9. Change the manager of department 3 to Smith, who lives in Houston. 10. Remove data of the department which has controlled only one project in Houston. 6 Practice 1 1. Return the department number and name of each department managed by Smith, who does not live in Houston. SELECT dnumber, dname FROM department WHERE mgr_ssn IN (SELECT ssn FROM employee WHERE lname = 'Smith' AND NOT (address LIKE '%Houston%')); 2. Return the department number and name of each department managed by Smith and controlling projects in Houston. SELECT dnumber, dname FROM department WHERE mgr_ssn IN (SELECT ssn FROM employee WHERE lname = 'Smith') AND dnumber IN (SELECT dnum FROM project WHERE plocation = 'Houston'); 7 Practice 1 3. Return the department number and name of each department in Houston but not controlling any project in Houston. SELECT dnumber, dname FROM department NATURAL JOIN dept_locations WHERE dlocation = 'Houston' AND dnumber NOT IN (SELECT dnum FROM project WHERE plocation = 'Houston'); 4. Return the department number and name of each department that has the highest number of employees. SELECT dnumber, dname FROM department JOIN employee ON dnumber = dno GROUP BY dnumber HAVING COUNT(*) = (SELECT MAX(cnt) FROM (SELECT dno, COUNT(*) cnt FROM employee GROUP BY dno) dept_cnt); 8 5. Create a virtual table that keeps the details of each department in Houston: department_number, department_name, number_of_projects, total_hours. CREATE VIEW Houston_dept AS SELECT dnumber department_number, dname department_name, COUNT(DISTINCT pnumber) number_of_projects, SUM(Hours) total_hours FROM department NATURAL JOIN dept_locations JOIN project ON dnumber = dnum JOIN works_on ON pnumber = pno WHERE dlocation = 'Houston' GROUP BY dnumber, dname; 6. Create a virtual table that keeps the details of each department with: dnumber, dname, no_of_employees, avg_salary. CREATE VIEW dept_v AS SELECT dnumber, dname, COUNT(*) no_of_employees, AVG(salary) avg_salary FROM department JOIN employee ON dnumber = dno GROUP BY dnumber, dname; 7. Return the department number and name of each department that has the less number of employees but higher average salary as compared to department 4. SELECT d1.dnumber, d1.dname FROM dept_v d1 JOIN dept_v d4 ON d1.no_of_employees < d4.no_of_employees AND d1.avg_salary > d4.avg_salary WHERE d4.dnumber = 4; 9 Practice 1 8. Insert department 3, named Logistics, managed by employee 987987987, from the same time when department Research has been managed. INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (3, 'Logistics', '987987987', (SELECT mgr_start_date FROM department WHERE dname = 'Research')); 9. Change the manager of department 3 to Smith, who lives in Houston. UPDATE department SET mgr_ssn = (SELECT ssn FROM employee WHERE lname = 'Smith' AND address LIKE '%Houston%') WHERE dnumber = 3; 10. Remove data of the department which has controlled only one project in Houston. DELETE FROM department WHERE dnumber IN (SELECT dnum FROM project WHERE plocation = 'Houston' GROUP BY dnum HAVING COUNT(*) = 1); 10 Practice 1 8. Insert department 3, named Logistics, managed by employee 987987987, from the same time when department Research has been managed. INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (3, 'Logistics', '987987987', (SELECT mgr_start_date FROM department WHERE dname = 'Research')); Demonstration on MySQL 11 Practice 1 8. Insert department 3, named Logistics, managed by employee 987987987, from the same time when department Research has been managed. INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (3, 'Logistics', '987987987', (SELECT mgr_start_date FROM department WHERE dname = 'Research')); Demonstration on MySQL 12 8. Insert department 3, named Logistics, managed by employee 987987987, from the same time when department Research has been managed. Practice 1 INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (3, 'Logistics', '987987987', (SELECT mgr_start_date FROM department WHERE dname = 'Research')); INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (3, 'Logistics', '987987987', getMgrDate('Research')); INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (13, 'IT', '987987987', (SELECT d.mgr_start_date FROM department d JOIN department dd ON d.dnumber = dd.dnumber WHERE d.dname = 'Research')); Demonstration on MySQL 13 Practice 1 8’. Insert department 12, named Marketing, managed by employee 988988988, from the same time when department Research has been managed. INSERT INTO department (dnumber, dname, mgr_ssn, mgr_start_date) VALUES (12, ‘Marketing', '988988988', (SELECT mgr_start_date FROM department WHERE dname = 'Research')); Msg 547, Level 16, State 0, Procedure insert_trigger, Line 19 The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__MGR_S__0DAF0CB0". The conflict occurred in database "company", table "dbo.EMPLOYEE", column 'SSN'. The statement has been terminated. Demonstration on MS SQL Server 14 Practice 2 1. Retrieve the name, gender, and relationship of each dependent of the employee in department 4. 2. Retrieve the name, gender, and date of birth of each dependent of the manager of department Research. 3. Retrieve the details of the daughter of each employee who works on more than 3 projects in Houston. 4. Retrieve the number of sons of each employee who lives in Houston and works for departments also in Houston. 5. Create a virtual table that keeps the details of dependent of each employee in department Research with: ssn, first_name, last_name, dependent_name, gender, date_of_birth, relationship. 6. Create a virtual table that keeps the detailed number of dependents of each employee with: ssn, first_name, last_name, department_name, number_of_sons, number_of_daughters. 7. Retrieve ssn and name of each employee in department Research who has more daughters than sons. 8. Insert Nancy as a daughter of the manager of department Research who was born on July 12, 1990. 9. Change the name of the son of employee 543543543 to the first name of his/her supervisor. 10. Remove the data of the dependents of each employee who works for the department which has not yet been assigned any manager. 15 Practice 2 1. Retrieve the name, gender, and relationship of each dependent of the employee in department 4. SELECT dependent_name, sex, relationship FROM dependent WHERE essn IN (SELECT ssn FROM employee WHERE dno = 4); 2. Retrieve the name, gender, and date of birth of each dependent of the manager of department Research. SELECT dependent_name, sex, bdate FROM dependent WHERE essn IN (SELECT mgr_ssn FROM department WHERE dname = 'Research'); 3. Retrieve the details of the daughter of each employee who works on more than 3 projects in Houston. SELECT * FROM dependent WHERE relationship = 'Daughter' AND essn IN (SELECT essn FROM works_on JOIN project ON pno = pnumber WHERE plocation = 'Houston' GROUP BY essn HAVING COUNT(*) > 3); 16 Practice 2 4. Retrieve the number of sons of each employee who lives in Houston and works for departments also in Houston. SELECT essn, COUNT(*) number_of_sons FROM dependent WHERE relationship = 'Son' AND essn IN (SELECT ssn FROM employee WHERE address LIKE '%Houston%' AND dno IN (SELECT dnumber FROM dept_locations WHERE dlocation = 'Houston')) GROUP BY essn; 5. Create a virtual table that keeps the details of dependent of each employee in department Research with: ssn, first_name, last_name, dependent_name, gender, date_of_birth, relationship. CREATE VIEW Research_dep AS SELECT ssn, fname first_name, lname last_name, dependent_name, d.sex gender, d.bdate date_of_birth, relationship FROM dependent d JOIN employee ON essn = ssn JOIN department ON dno = dnumber WHERE dname = 'Research'; 17 Practice 2 6. Create a virtual table that keeps the detailed number of dependents of each employee with: ssn, first_name, last_name, department_name, number_of_sons, number_of_daughters. CREATE VIEW emp_dep AS SELECT ssn, fname first_name, lname last_name, dname department_name, number_of_sons, number_of_daughters FROM department JOIN employee ON dnumber = dno JOIN (SELECT essn, COUNT(*) number_of_sons FROM dependent WHERE relationship = 'Son' GROUP BY essn) s ON ssn = s.essn LEFT OUTER JOIN (SELECT essn, COUNT(*) number_of_daughters FROM dependent WHERE relationship = 'Daughter' GROUP BY essn) d ON ssn = d.essn; 7. Retrieve ssn and name of each employee in department Research who has more daughters than sons. SELECT ssn, first_name, last_name FROM emp_dep WHERE department_name = 'Research' AND number_of_daughters > number_of_sons; 18 Practice 2 8. Insert Nancy as a daughter of the manager of department Research who was born on July 12, 1990. INSERT INTO dependent (essn, name, sex, bdate, relationship) VALUES ((SELECT mgr_ssn FROM department WHERE dname = 'Research'), 'Nancy', 'F', '12-07-1990', 'Daughter'); 9. Change the name of the son of employee 543543543 to the first name of his/her supervisor. UPDATE dependent SET name = (SELECT fname FROM employee WHERE ssn IN (SELECT super_ssn FROM employee WHERE ssn = '543543543')) WHERE relationship = 'Son' AND essn = '543543543'; 10. Remove the data of the dependents of each employee who works for the department which has not yet been assigned any manager. DELETE FROM dependent WHERE essn IN (SELECT ssn FROM employee WHERE dno IN (SELECT dnumber FROM department WHERE mgr_ssn IS NULL)); 19 Practice 3 1. Retrieve ssn, first name, last name and department name of each employee who works for the department that controls projects in Houston. 2. Retrieve ssn, first name, and last name of each employee who works on projects each of which has more than 2 workers. 3. Retrieve ssn, first name, last name, salary, and department number of each employee who has the highest salary in his/ her department. 4. Retrieve ssn, name, and department name of each employee who do not work for any project in Houston. 5. Create a virtual table that keeps the details of each employee and his/ her department with: ssn, first_name, last_name, department_number, department_name 6. Create a virtual table that keeps the details of each employee and his/ her projects with: ssn, first_name, last_name, project_number, project_name. 7. Retrieve ssn, department number, project number of each employee who works on the same project Computerization as Wong but works in different departments. 8. Insert data of employee 789789789 born on Feb 01, 1970, working for the department where Wong’s supervisor works. 9. Update the salary of each new employee to the average salary of his/ her department. 10. Remove data of each employee who has the total sum of hours working on the projects less than 40. 20 Practice 3 1. Retrieve ssn, first name, last name and department name of each employee who works for the department that controls projects in Houston. SELECT ssn, fname, lname, dname FROM employee JOIN department ON dno = dnumber WHERE dnumber IN (SELECT dnum FROM project WHERE plocation = 'Houston'); 2. Retrieve ssn, first name, and last name of each employee who works on projects each of which has more than 2 workers. SELECT ssn, fname, lname FROM employee WHERE ssn IN (SELECT essn FROM works_on WHERE pno IN (SELECT pno FROM works_on GROUP BY pno HAVING COUNT(*)>2)); 21 Practice 3 3. Retrieve ssn, first name, last name, salary, and department number of each employee who has the highest salary in his/ her department. SELECT ssn, fname, lname, salary, dno FROM employee e WHERE salary = (SELECT MAX (salary) FROM employee WHERE dno = e.dno); 4. Retrieve ssn, name, and department name of each employee who do not work for any project in Houston. SELECT ssn, fname, lname, dname FROM employee JOIN department on dno = dnumber WHERE ssn NOT IN (SELECT essn FROM works_on WHERE pno IN (SELECT pnumber FROM project WHERE plocation = 'Houston')); 22 5. Create a virtual table that keeps the details of each employee and his/ her department with: ssn, first_name, last_name, department_number, department_name CREATE VIEW emp_dept AS SELECT ssn, fname first_name, lname last_name, dno department_number, dname department_name FROM employee JOIN department ON dno = dnumber; 6. Create a virtual table that keeps the details of each employee and his/ her projects with: ssn, first_name, last_name, project_number, project_name. CREATE VIEW emp_proj AS SELECT ssn, fname first_name, lname last_name, pno project_number, pname project_name FROM employee JOIN works_on ON ssn = essn JOIN project ON pno = pnumber; 7. Retrieve ssn, department number, project number of each employee who works on the same project Computerization as Wong but works in different departments. SELECT d.ssn, department_number, project_number FROM emp_dept d JOIN emp_proj p ON d.ssn = p.ssn WHERE project_name = 'Computerization' AND department_number NOT IN (SELECT department_number FROM emp_dept dw JOIN emp_proj pw ON dw.ssn = pw.ssn WHERE project_name = 'Computerization' AND dw.last_name = 'Wong'); 23 Practice 3 8. Insert data of employee 789789789 born on Feb 01, 1970, working for the department where Wong’s supervisor works. INSERT INTO employee (ssn, bdate, dno) VALUES ('789789789', '1970-02-01', (SELECT s.dno FROM employee e JOIN employee s ON e.super_ssn = s.ssn WHERE e.lname = 'Wong')); 9. Update the salary of each new employee to the average salary of his/ her department. UPDATE employee SET salary = (SELECT AVG(salary) FROM employee e WHERE e.dno = dno) WHERE salary IS NULL; 10. Remove data of each employee who has the total sum of hours working on the projects less than 40. DELETE FROM employee WHERE ssn IN (SELECT essn FROM works_on GROUP BY essn HAVING SUM(hours) < 40); 24 Database Systems (CO2013) 25