Information Resources Management March 6, 2001
Agenda n Administrivia n SQL Part 2 n Homework #6
Administrivia n Mid-term Exam n Homework #4 n Homework #5
SQL Structured Query Language n The standard relational database language n Two Parts n DDL - Data Definition Language n DML - Data Manipulation Language
SQL - DDL n Data Definition: Define schemas, delete relations, create indices, modify schemas n View Definition n Authorization n Integrity
SQL - DML n Insert, Modify, Delete Tuples n Interactive n Embedded n Transaction Control
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)
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) >= Join all tables (Cartesian product)
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) >= For each row, apply WHERE conditions
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) >= WHERE subquery, each row evaluated separately
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) >= GROUP BY (with HAVING) - Order remaining data (all rows) by groups
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) >= Apply HAVING to each group
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
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) >= Calculate aggregate functions for each (remaining) group
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) >= SELECT desired output columns
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) >= ORDER BY (sort) is processed last
SQL - DML n INSERT n UPDATE n DELETE
INSERT INSERT INTO table VALUES (………) INSERT INTO table(attributes) VALUES (………)
INSERT Example n Add an employee INSERT INTO Employee VALUES(‘ ’,’John Smith’,,212) INSERT INTO Employee(EmpID, Name, OfficeNBR) VALUES(‘ ’,’John Smith’,212) MgrFlag is NULL in both cases
UPDATE UPDATE table SET attribute = value or calculation UPDATE table SET attribute = value or calculation WHERE conditions
UPDATE Example n Increase the prices of all properties by 5% UPDATE Property SET Price = Price * 1.05
UPDATE Example n Increase the prices of all properties in St. Paul, MN by 7.5%
UPDATE Example n Increase the prices of all properties in St. Paul, MN by 7.5% UPDATE Property SET Price = Price * WHERE City = ‘St. Paul’ AND State = ‘MN’
UPDATE Example n Change the zip code of all offices in to
UPDATE Example n Change the zip code of all offices in to UPDATE Office SET Zip = ‘ ’ WHERE Zip LIKE ‘15214%’
DELETE DELETE FROM table WHERE conditions
DELETE Example n Delete everything from the gift table DELETE FROM Gift
DELETE Example n Delete all employees who do not have access to a PC
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)
SQL - DDL n CREATE TABLE n DROP TABLE n ALTER TABLE
CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) )
CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) ) n Attribute Definitions - Table 9-1, p. 329 n Attribute Constraints n NOT NULL n UNIQUE
CREATE TABLE CREATE TABLE name (attributedefn, attributedefn, … attributedefn, … ) ) n Integrity Constraints n PRIMARY KEY (attribute, …) n FOREIGN KEY (attribute,…) REFERENCES (table name)
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))
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))
CREATE TABLE Example n Create the MgrPCAccess table -- access type is required
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))
DROP TABLE n DROP TABLE name n DROP TABLE Office n DROP vs. DELETE
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
Other SQL DDL n CREATE INDEX n DROP INDEX n CREATE VIEW n DROP VIEW n CREATE SCHEMA
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
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?
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)
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)
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
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)
Other Relational Languages n Chapter 10 of book n Query-by Example (QBE) n Access
In-Class Exercise n SQL n All 21 queries
Homework #6 n Do remaining 5 from HW #5 n Keep problem numbers