Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information Resources Management March 6, 2001. Agenda n Administrivia n SQL Part 2 n Homework #6.

Similar presentations


Presentation on theme: "Information Resources Management March 6, 2001. Agenda n Administrivia n SQL Part 2 n Homework #6."— Presentation transcript:

1 Information Resources Management March 6, 2001

2 Agenda n Administrivia n SQL Part 2 n Homework #6

3 Administrivia n Mid-term Exam n Homework #4 n Homework #5

4 SQL Structured Query Language n The standard relational database language n Two Parts n DDL - Data Definition Language n DML - Data Manipulation Language

5 SQL - DDL n Data Definition: Define schemas, delete relations, create indices, modify schemas n View Definition n Authorization n Integrity

6 SQL - DML n Insert, Modify, Delete Tuples n Interactive n Embedded n Transaction Control

7 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 tableX (b, c, e)tableY(a, c)tableZ (a, d)

8 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 1 - Join all tables (Cartesian product)

9 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 2 - For each row, apply WHERE conditions

10 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 3 - WHERE subquery, each row evaluated separately

11 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 4 - GROUP BY (with HAVING) - Order remaining data (all rows) by groups

12 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 5 - Apply HAVING to each group

13 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= ALL (SELECT AVG(d) from tableZ as Z) 5a - HAVING with “standalone” SELECT

14 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 6 - Calculate aggregate functions for each (remaining) group

15 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 7 - SELECT desired output columns

16 Evaluating SQL Statements SELECT y.a, AVG(x.b) FROM tableX as x, tableY as y WHERE x.c = y.c AND x.b IN (SELECT d FROM tableZ as z WHERE z.a = y.a) GROUP BY y.a HAVING COUNT (x.e) >= 5 8 - ORDER BY (sort) is processed last

17 SQL - DML n INSERT n UPDATE n DELETE

18 INSERT INSERT INTO table VALUES (………) INSERT INTO table(attributes) VALUES (………)

19 INSERT Example n Add an employee INSERT INTO Employee VALUES(‘123456789’,’John Smith’,,212) INSERT INTO Employee(EmpID, Name, OfficeNBR) VALUES(‘123456789’,’John Smith’,212) MgrFlag is NULL in both cases

20 UPDATE UPDATE table SET attribute = value or calculation UPDATE table SET attribute = value or calculation WHERE conditions

21 UPDATE Example n Increase the prices of all properties by 5% UPDATE Property SET Price = Price * 1.05

22 UPDATE Example n Increase the prices of all properties in St. Paul, MN by 7.5%

23 UPDATE Example n Increase the prices of all properties in St. Paul, MN by 7.5% UPDATE Property SET Price = Price * 1.075 WHERE City = ‘St. Paul’ AND State = ‘MN’

24 UPDATE Example n Change the zip code of all offices in 15214 to 15217-0173

25 UPDATE Example n Change the zip code of all offices in 15214 to 15217-0173 UPDATE Office SET Zip = ‘15217-0173’ WHERE Zip LIKE ‘15214%’

26 DELETE DELETE FROM table WHERE conditions

27 DELETE Example n Delete everything from the gift table DELETE FROM Gift

28 DELETE Example n Delete all employees who do not have access to a PC

29 DELETE Example n Delete all employees who do not have access to a PC DELETE FROM Employee WHERE EmpID NOT IN (SELECT EmpID FROM PCAccess)

30 SQL - DDL n CREATE TABLE n DROP TABLE n ALTER TABLE

31 CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) )

32 CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) ) n Attribute Definitions - Table 9-1, p. 329 n Attribute Constraints n NOT NULL n UNIQUE

33 CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) ) n Integrity Constraints n PRIMARY KEY (attribute, …) n FOREIGN KEY (attribute,…) REFERENCES (table name)

34 CREATE TABLE Example n Create the Office table CREATE TABLE Office (OfficeNbrINTEGER NOT NULL UNIQUE, AddressVARCHAR(50), AddressVARCHAR(50), CityVARCHAR(25), CityVARCHAR(25), StateCHAR(2), StateCHAR(2), ZipCHAR(10), ZipCHAR(10), PhoneNbrCHAR(13), PhoneNbrCHAR(13), PRIMARY KEY (OfficeNbr)) PRIMARY KEY (OfficeNbr))

35 CREATE TABLE Example n Create the Manager table CREATE TABLE Manager (EmpIDCHAR(9), OfficeNbrINTEGER, OfficeNbrINTEGER, PRIMARY KEY (EmpID), PRIMARY KEY (EmpID), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (OfficeNbr) REFERENCES (Office)) FOREIGN KEY (OfficeNbr) REFERENCES (Office))

36 CREATE TABLE Example n Create the MgrPCAccess table -- access type is required

37 CREATE TABLE Example n Create the MgrPCAccess table -- access type is required CREATE TABLE MgrPCAccess (PC#INTEGER, EmpIDCHAR(9), EmpIDCHAR(9), AccessTypeCHAR(15) NOT NULL, AccessTypeCHAR(15) NOT NULL, PRIMARY KEY (PC#, EmpID), PRIMARY KEY (PC#, EmpID), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (EmpID) REFERENCES (Employee), FOREIGN KEY (PC#) REFERENCES (PC)) FOREIGN KEY (PC#) REFERENCES (PC))

38 DROP TABLE n DROP TABLE name n DROP TABLE Office n DROP vs. DELETE

39 ALTER TABLE n ALTER TABLE name ADD attributes n ALTER TABLE name DROP attributes n Add - existing tuples get NULLs n Nulls must be allowed n Drop - cannot drop the primary key

40 Other SQL DDL n CREATE INDEX n DROP INDEX n CREATE VIEW n DROP VIEW n CREATE SCHEMA

41 Multiple Tables - JOINs n FROM multiple tables WHERE cond n “INNER” join n Equi-join & Natural join variations n What if second table is “optionally” included? n “OUTER” join

42 Outer Join n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O, Employee as E WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1 What happens to offices without a mgr?

43 Outer Join n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O, Employee as E WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1 UNION SELECT address, city, ‘’ FROM Office as O WHERE O.OfficeNbr NOT IN (SELECT OfficeNbr FROM Employee WHERE MgrFlag = 1)

44 Outer Join - Access n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O LEFT JOIN Employee as E WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1 LEFT JOIN - all rows for table on left included (RIGHT JOIN)

45 Outer Join - Oracle n List the address, city, and manager name (if any) of all offices. SELECT address, city, e.name FROM Office as O, Employee as E WHERE O.OfficeNbr = E.OfficeNbr (+) AND MgrFlag = 1 (+) here LEFT JOIN WHERE O.OfficeNbr (+) = E.OffficeNbr RIGHT JOIN

46 Outer Join n Outer Joins are not SQL standard n Not always available n Not consistent n Can always do the same query using standard SQL (UNION & NOT IN)

47 Other Relational Languages n Chapter 10 of book n Query-by Example (QBE) n Access

48 In-Class Exercise n SQL n All 21 queries

49 Homework #6 n Do remaining 5 from HW #5 n Keep problem numbers


Download ppt "Information Resources Management March 6, 2001. Agenda n Administrivia n SQL Part 2 n Homework #6."

Similar presentations


Ads by Google