Presentation is loading. Please wait.

Presentation is loading. Please wait.

Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.

Similar presentations


Presentation on theme: "Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems."— Presentation transcript:

1 Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems (Chapter 8 & 9 Tutorial)

2 Chapter 8 & 9 Tutorial1 COMPANY Schema Given the following schema for a COMPANY database: EMPLOYEE DEPARTMENT FNAMEMINITLNAMESSNSALARYDNO DNAMEDNUMBERLOCATION

3 Chapter 8 & 9 Tutorial2 SQL*Plus Open SQL*Plus, enter appropriate name, password, & host string scott psu stddata

4 Chapter 8 & 9 Tutorial3 Exercise 1 Write appropriate SQL DDL statements to declare the COMPANY schema. CREATE TABLE DEPARTMENT ( DNAMEVARCHAR2(20)NOT NULL UNIQUE, DNUMBERNUMBER(2), LOCATIONVARCHAR2(20), CONSTRAINT DEPT_PKEY PRIMARY KEY (DNUMBER)); CREATE TABLE EMPLOYEE ( FNAMEVARCHAR2(15)NOT NULL, MINIT CHAR, LNAMEVARCHAR2(15)NOT NULL, SSNCHAR(9), SALARYNUMBER(10,2)NOT NULL, DNONUMBER(2), CONSTRAINT EMP_PKEY PRIMARY KEY (SSN), CONSTRAINT EMP_FKEY FOREIGN KEY (DNO) REFERENCES DEPARTMENT (DNUMBER)); save company.SQL save company.SQL append

5 Chapter 8 & 9 Tutorial4 Exercise 2 Display the structure of your tables. describe DEPARTMENT describe EMPLOYEE

6 Chapter 8 & 9 Tutorial5 Exercise 3 Add the following columns to the EMPLOYEE table: ADDRESS SEX ALTER TABLE EMPLOYEE ADD ( ADDRESSVARCHAR2(30), SEXCHARCHECK (SEX IN ('M', 'F')));

7 Chapter 8 & 9 Tutorial6 Exercise 4 Insert the following tuple into the DEPARTMENT table: DNAME = ‘Project’ DNUMBER = 5 DLOCATION = ‘Houston’ INSERT INTO DEPARTMENT VALUES ('Project', 5, 'Houston');

8 Chapter 8 & 9 Tutorial7 Exercise 5 Write an INSERT statement in which each time it is run, the user will be prompted for a new set of values (DEPARTMENT table) INSERT INTO DEPARTMENT VALUES ('&D_NAME', &D_NUMBER, '&D_LOCATION'); Execute the previous statement twice, and insert the following values: D_NAME = Administration D_NUMBER = 4 D_LOCATION = Stafford D_NAME = Headquarters D_NUMBER = 1 D_LOCATION = Bellaire

9 Chapter 8 & 9 Tutorial8 Exercise 6 Write an SQL statement to modify the name of department number 5 to ‘Research’. UPDATE DEPARTMENT SET DNAME='Research' WHEREDNUMBER=5;

10 Chapter 8 & 9 Tutorial9 Exercise 7 Open the “data.SQL” file and execute all the SQL statements. (This file includes SQL statements to fill the EMPLOYEE table with data) Start data.SQL

11 Chapter 8 & 9 Tutorial Exercise 8 Write the following query in SQL: “Retrieve the names of all female employees who work in the ‘Research’ department” SELECTFNAME, LNAME FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND SEX='F' AND DNAME='Research'; 10

12 Chapter 8 & 9 Tutorial11 Exercise 9 Write the following query in SQL: “Retrieve the names and addresses of all employees who do not work in department number 5” SELECTFNAME, LNAME, ADDRESS FROMEMPLOYEE MINUS SELECTFNAME, LNAME, ADDRESS FROMEMPLOYEE WHEREDNO=5;

13 Chapter 8 & 9 Tutorial12 Exercise 10 Write the following query in SQL: “Retrieve the total salary of the male employees who work in department number 5” SELECTSUM(SALARY) FROMEMPLOYEE WHERESEX='M' AND DNO=5;

14 Chapter 8 & 9 Tutorial13 Exercise 11 Write the following query in SQL: “For each department, list the department name (Ascending order) and the number of its employees” SELECTDNAME, COUNT(*) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER GROUP BYDNAME ORDER BYDNAME;

15 Chapter 8 & 9 Tutorial14 Exercise 12 Specify the following view in SQL: “A view that has the department name and the number of employees (per department)” CREATE VIEW EMP_DEPT (DEPT_NAME, DEPT_EMP) AS SELECTDNAME, COUNT(*) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER GROUP BYDNAME;

16 Chapter 8 & 9 Tutorial15 Exercise 13 Write the following query in SQL (using the previous view): “For each department, list the department name (Ascending order) and the number of its employees” SELECTDEPT_NAME, DEPT_EMP FROMEMP_DEPT ORDER BYDEPT_NAME;

17 Chapter 8 & 9 Tutorial16 Exercise 14 Exit SQL*Plus with committing any outstanding changes to the database. quit


Download ppt "Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems."

Similar presentations


Ads by Google