information resources management
DESCRIPTION
Information Resources Management. March 6, 2001. Agenda. Administrivia SQL Part 2 Homework #6. Administrivia. Mid-term Exam Homework #4 Homework #5. SQL Structured Query Language. The standard relational database language Two Parts DDL - Data Definition Language - PowerPoint PPT PresentationTRANSCRIPT
Information Resources Information Resources ManagementManagement
March 6, 2001March 6, 2001
AgendaAgenda
AdministriviaAdministrivia SQL Part 2SQL Part 2 Homework #6Homework #6
AdministriviaAdministrivia
Mid-term ExamMid-term Exam Homework #4Homework #4 Homework #5Homework #5
SQLSQLStructured Query LanguageStructured Query Language TheThe standard relational database standard relational database
languagelanguage
Two PartsTwo Parts DDL - Data Definition LanguageDDL - Data Definition Language DML - Data Manipulation LanguageDML - Data Manipulation Language
SQL - DDLSQL - DDL
Data Definition: Define schemas, delete Data Definition: Define schemas, delete relations, create indices, modify relations, create indices, modify schemasschemas
View DefinitionView Definition AuthorizationAuthorization IntegrityIntegrity
SQL - DMLSQL - DML
Insert, Modify, Delete TuplesInsert, Modify, Delete Tuples
InteractiveInteractive EmbeddedEmbedded
Transaction ControlTransaction Control
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
tableX (b, c, e)tableX (b, c, e) tableY(a, c)tableY(a, c) tableZ (a, d)tableZ (a, d)
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
1 - Join all tables (Cartesian product)1 - Join all tables (Cartesian product)
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
2 - For 2 - For each roweach row, apply WHERE conditions, apply WHERE conditions
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHEREWHERE x.c = y.c AND x.c = y.c AND
x.b IN x.b IN (SELECT d FROM tableZ as z(SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
3 - WHERE subquery, each row evaluated 3 - WHERE subquery, each row evaluated separatelyseparately
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVINGHAVING COUNT (x.e) >= 5 COUNT (x.e) >= 5
4 - GROUP BY (with HAVING) - Order 4 - GROUP BY (with HAVING) - Order remaining data (all rows) by groupsremaining data (all rows) by groups
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
5 - Apply HAVING to each group5 - Apply HAVING to each group
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= ALL (SELECT AVG(d) from HAVING COUNT (x.e) >= ALL (SELECT AVG(d) from tableZ as Z)tableZ as Z)
5a - HAVING with “standalone” SELECT5a - HAVING with “standalone” SELECT
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, SELECT y.a, AVG(x.b)AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
6 - Calculate aggregate functions for each 6 - Calculate aggregate functions for each (remaining) group(remaining) group
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
7 - SELECT desired output columns7 - SELECT desired output columns
Evaluating SQL StatementsEvaluating SQL Statements
SELECT y.a, AVG(x.b)SELECT y.a, AVG(x.b)
FROM tableX as x, tableY as yFROM tableX as x, tableY as y
WHERE x.c = y.c ANDWHERE x.c = y.c AND
x.b IN (SELECT d FROM tableZ as zx.b IN (SELECT d FROM tableZ as z
WHERE z.a = y.a)WHERE z.a = y.a)
GROUP BY y.aGROUP BY y.a
HAVING COUNT (x.e) >= 5HAVING COUNT (x.e) >= 5
8 - ORDER BY (sort) is processed last8 - ORDER BY (sort) is processed last
SQL - DMLSQL - DML
INSERTINSERT UPDATEUPDATE DELETEDELETE
INSERTINSERT
INSERT INTO tableINSERT INTO table
VALUES (………)VALUES (………)
INSERT INTO table(attributes)INSERT INTO table(attributes)
VALUES (………)VALUES (………)
INSERT ExampleINSERT Example
Add an employeeAdd an employee
INSERT INTO EmployeeINSERT INTO Employee
VALUES(‘123456789’,’John Smith’,,212)VALUES(‘123456789’,’John Smith’,,212)
INSERT INTO Employee(EmpID, Name, OfficeNBR)INSERT INTO Employee(EmpID, Name, OfficeNBR)
VALUES(‘123456789’,’John Smith’,212)VALUES(‘123456789’,’John Smith’,212)
MgrFlag is NULL in both casesMgrFlag is NULL in both cases
UPDATEUPDATE
UPDATE tableUPDATE table
SET attribute = value or calculationSET attribute = value or calculation
UPDATE tableUPDATE table
SET attribute = value or calculationSET attribute = value or calculation
WHERE conditionsWHERE conditions
UPDATE ExampleUPDATE Example
Increase the prices of all properties by Increase the prices of all properties by 5%5%
UPDATE PropertyUPDATE Property
SET Price = Price * 1.05SET Price = Price * 1.05
UPDATE ExampleUPDATE Example
Increase the prices of all properties in Increase the prices of all properties in St. Paul, MN by 7.5%St. Paul, MN by 7.5%
UPDATE ExampleUPDATE Example
Increase the prices of all properties in Increase the prices of all properties in St. Paul, MN by 7.5%St. Paul, MN by 7.5%
UPDATE PropertyUPDATE Property
SET Price = Price * 1.075SET Price = Price * 1.075
WHERE City = ‘St. Paul’WHERE City = ‘St. Paul’
AND State = ‘MN’AND State = ‘MN’
UPDATE ExampleUPDATE Example
Change the zip code of all offices in Change the zip code of all offices in 15214 to 15217-017315214 to 15217-0173
UPDATE ExampleUPDATE Example
Change the zip code of all offices in Change the zip code of all offices in 15214 to 15217-017315214 to 15217-0173
UPDATE OfficeUPDATE Office
SET Zip = ‘15217-0173’SET Zip = ‘15217-0173’
WHERE Zip LIKE ‘15214%’WHERE Zip LIKE ‘15214%’
DELETEDELETE
DELETE FROM tableDELETE FROM table
DELETE FROM tableDELETE FROM table
WHERE conditionsWHERE conditions
DELETE ExampleDELETE Example
Delete everything from the gift tableDelete everything from the gift table
DELETE FROM GiftDELETE FROM Gift
DELETE ExampleDELETE Example
Delete all employees who do not have Delete all employees who do not have access to a PCaccess to a PC
DELETE ExampleDELETE Example
Delete all employees who do not have Delete all employees who do not have access to a PCaccess to a PC
DELETE FROM EmployeeDELETE FROM Employee
WHERE EmpID NOT INWHERE EmpID NOT IN
(SELECT EmpID(SELECT EmpID
FROM PCAccess)FROM PCAccess)
SQL - DDLSQL - DDL
CREATE TABLECREATE TABLE DROP TABLEDROP TABLE ALTER TABLEALTER TABLE
CREATE TABLECREATE TABLE
CREATE TABLE nameCREATE TABLE name
(attribute(attribute defndefn <constraint>,<constraint>,
attributeattribute defndefn <constraint>, …<constraint>, …
<integrity constraints>)<integrity constraints>)
CREATE TABLECREATE TABLE
CREATE TABLE nameCREATE TABLE name
(attribute(attribute defndefn <constraint>,<constraint>,
attributeattribute defndefn <constraint>, …<constraint>, …
<integrity constraints>)<integrity constraints>)
Attribute Definitions - Table 9-1, p. 329Attribute Definitions - Table 9-1, p. 329 Attribute ConstraintsAttribute Constraints
NOT NULLNOT NULL UNIQUEUNIQUE
CREATE TABLECREATE TABLE
CREATE TABLE nameCREATE TABLE name
(attribute(attribute defndefn <constraint>,<constraint>,
attributeattribute defndefn <constraint>, …<constraint>, …
<integrity constraints>)<integrity constraints>)
Integrity ConstraintsIntegrity Constraints PRIMARY KEY (attribute, …)PRIMARY KEY (attribute, …) FOREIGN KEY (attribute,…) FOREIGN KEY (attribute,…)
REFERENCES (table name)REFERENCES (table name)
CREATE TABLE ExampleCREATE TABLE Example
Create the Office tableCreate the Office table
CREATE TABLE OfficeCREATE TABLE Office
(OfficeNbr(OfficeNbr INTEGER NOT NULL UNIQUE,INTEGER NOT NULL UNIQUE,
AddressAddress VARCHAR(50),VARCHAR(50),
CityCity VARCHAR(25),VARCHAR(25),
StateState CHAR(2),CHAR(2),
ZipZip CHAR(10),CHAR(10),
PhoneNbrPhoneNbr CHAR(13),CHAR(13),
PRIMARY KEY (OfficeNbr))PRIMARY KEY (OfficeNbr))
CREATE TABLE ExampleCREATE TABLE Example
Create the Manager tableCreate the Manager table
CREATE TABLE ManagerCREATE TABLE Manager
(EmpID(EmpID CHAR(9),CHAR(9),
OfficeNbrOfficeNbr INTEGER,INTEGER,
PRIMARY KEY (EmpID),PRIMARY KEY (EmpID),
FOREIGN KEY (EmpID) REFERENCES FOREIGN KEY (EmpID) REFERENCES (Employee),(Employee),
FOREIGN KEY (OfficeNbr) REFERENCES FOREIGN KEY (OfficeNbr) REFERENCES (Office))(Office))
CREATE TABLE ExampleCREATE TABLE Example
Create the MgrPCAccess table -- Create the MgrPCAccess table -- access type is requiredaccess type is required
CREATE TABLE ExampleCREATE TABLE Example
Create the MgrPCAccess table -- access Create the MgrPCAccess table -- access type is requiredtype is required
CREATE TABLE MgrPCAccessCREATE TABLE MgrPCAccess
(PC#(PC# INTEGER,INTEGER,
EmpIDEmpID CHAR(9),CHAR(9),
AccessTypeAccessType CHAR(15) NOT NULL,CHAR(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 TABLEDROP TABLE
DROP TABLE nameDROP TABLE name
DROP TABLE OfficeDROP TABLE Office
DROP vs. DELETEDROP vs. DELETE
ALTER TABLEALTER TABLE
ALTER TABLE name ADD attributesALTER TABLE name ADD attributes ALTER TABLE name DROP attributesALTER TABLE name DROP attributes
Add - existing tuples get NULLsAdd - existing tuples get NULLs Nulls must be allowedNulls must be allowed
Drop - cannot drop the primary keyDrop - cannot drop the primary key
Other SQL DDLOther SQL DDL
CREATE INDEXCREATE INDEX DROP INDEXDROP INDEX CREATE VIEWCREATE VIEW DROP VIEWDROP VIEW CREATE SCHEMACREATE SCHEMA
Multiple Tables - JOINsMultiple Tables - JOINs
FROM multiple tables WHERE condFROM multiple tables WHERE cond ““INNER” joinINNER” join Equi-join & Natural join variationsEqui-join & Natural join variations
What if second table is “optionally” What if second table is “optionally” included?included? ““OUTER” joinOUTER” join
Outer JoinOuter Join
List the address, city, and manager name (if any) List the address, city, and manager name (if any) of all offices.of all offices.
SELECT address, city, e.nameSELECT address, city, e.name
FROM Office as O, Employee as EFROM Office as O, Employee as E
WHERE O.OfficeNbr = E.OfficeNbr AND WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1MgrFlag = 1
What happens to offices without a mgr?What happens to offices without a mgr?
Outer JoinOuter Join
List the address, city, and manager name List the address, city, and manager name (if any) of all offices.(if any) of all offices.SELECT address, city, e.nameSELECT address, city, e.name
FROM Office as O, Employee as EFROM Office as O, Employee as E
WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1
UNIONUNION
SELECT address, city, ‘’SELECT address, city, ‘’
FROM Office as OFROM Office as O
WHERE O.OfficeNbr NOT IN (SELECT OfficeNbr FROM WHERE O.OfficeNbr NOT IN (SELECT OfficeNbr FROM Employee WHERE MgrFlag = 1)Employee WHERE MgrFlag = 1)
Outer Join - AccessOuter Join - Access
List the address, city, and manager List the address, city, and manager name (if any) of all offices.name (if any) of all offices.SELECT address, city, e.nameSELECT address, city, e.name
FROM Office as O LEFT JOIN Employee as EFROM Office as O LEFT JOIN Employee as E
WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1WHERE O.OfficeNbr = E.OfficeNbr AND MgrFlag = 1
LEFT JOIN - all rows for table on left LEFT JOIN - all rows for table on left included (RIGHT JOIN)included (RIGHT JOIN)
Outer Join - OracleOuter Join - Oracle
List the address, city, and manager name List the address, city, and manager name (if any) of all offices.(if any) of all offices.SELECT address, city, e.nameSELECT address, city, e.name
FROM Office as O, Employee as EFROM Office as O, Employee as E
WHERE O.OfficeNbr = E.OfficeNbr (+) AND MgrFlag = 1WHERE O.OfficeNbr = E.OfficeNbr (+) AND MgrFlag = 1
(+) here LEFT JOIN(+) here LEFT JOIN
WHERE O.OfficeNbr (+) = E.OffficeNbr WHERE O.OfficeNbr (+) = E.OffficeNbr RIGHT JOINRIGHT JOIN
Outer JoinOuter Join
Outer Joins are not SQL standardOuter Joins are not SQL standard Not always availableNot always available Not consistentNot consistent Can Can alwaysalways do the same query using do the same query using
standard SQL (UNION & NOT IN)standard SQL (UNION & NOT IN)
Other Relational LanguagesOther Relational Languages
Chapter 10 of bookChapter 10 of book
Query-by Example (QBE)Query-by Example (QBE) AccessAccess
In-Class ExerciseIn-Class Exercise
SQLSQL All 21 queriesAll 21 queries
Homework #6Homework #6
Do remaining 5 from HW #5Do remaining 5 from HW #5 Keep problem numbersKeep problem numbers