information resources management

49
Information Resources Information Resources Management Management March 6, 2001 March 6, 2001

Upload: nancy

Post on 05-Feb-2016

30 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Information Resources Management

Information Resources Information Resources ManagementManagement

March 6, 2001March 6, 2001

Page 2: Information Resources Management

AgendaAgenda

AdministriviaAdministrivia SQL Part 2SQL Part 2 Homework #6Homework #6

Page 3: Information Resources Management

AdministriviaAdministrivia

Mid-term ExamMid-term Exam Homework #4Homework #4 Homework #5Homework #5

Page 4: Information Resources Management

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

Page 5: Information Resources Management

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

Page 6: Information Resources Management

SQL - DMLSQL - DML

Insert, Modify, Delete TuplesInsert, Modify, Delete Tuples

InteractiveInteractive EmbeddedEmbedded

Transaction ControlTransaction Control

Page 7: Information Resources Management

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)

Page 8: Information Resources Management

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)

Page 9: Information Resources Management

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

Page 10: Information Resources Management

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

Page 11: Information Resources Management

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

Page 12: Information Resources Management

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

Page 13: Information Resources Management

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

Page 14: Information Resources Management

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

Page 15: Information Resources Management

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

Page 16: Information Resources Management

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

Page 17: Information Resources Management

SQL - DMLSQL - DML

INSERTINSERT UPDATEUPDATE DELETEDELETE

Page 18: Information Resources Management

INSERTINSERT

INSERT INTO tableINSERT INTO table

VALUES (………)VALUES (………)

INSERT INTO table(attributes)INSERT INTO table(attributes)

VALUES (………)VALUES (………)

Page 19: Information Resources Management

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

Page 20: Information Resources Management

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

Page 21: Information Resources Management

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

Page 22: Information Resources Management

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%

Page 23: Information Resources Management

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’

Page 24: Information Resources Management

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

Page 25: Information Resources Management

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%’

Page 26: Information Resources Management

DELETEDELETE

DELETE FROM tableDELETE FROM table

DELETE FROM tableDELETE FROM table

WHERE conditionsWHERE conditions

Page 27: Information Resources Management

DELETE ExampleDELETE Example

Delete everything from the gift tableDelete everything from the gift table

DELETE FROM GiftDELETE FROM Gift

Page 28: Information Resources Management

DELETE ExampleDELETE Example

Delete all employees who do not have Delete all employees who do not have access to a PCaccess to a PC

Page 29: Information Resources Management

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)

Page 30: Information Resources Management

SQL - DDLSQL - DDL

CREATE TABLECREATE TABLE DROP TABLEDROP TABLE ALTER TABLEALTER TABLE

Page 31: Information Resources Management

CREATE TABLECREATE TABLE

CREATE TABLE nameCREATE TABLE name

(attribute(attribute defndefn <constraint>,<constraint>,

attributeattribute defndefn <constraint>, …<constraint>, …

<integrity constraints>)<integrity constraints>)

Page 32: Information Resources Management

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

Page 33: Information Resources Management

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)

Page 34: Information Resources Management

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))

Page 35: Information Resources Management

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))

Page 36: Information Resources Management

CREATE TABLE ExampleCREATE TABLE Example

Create the MgrPCAccess table -- Create the MgrPCAccess table -- access type is requiredaccess type is required

Page 37: Information Resources Management

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))

Page 38: Information Resources Management

DROP TABLEDROP TABLE

DROP TABLE nameDROP TABLE name

DROP TABLE OfficeDROP TABLE Office

DROP vs. DELETEDROP vs. DELETE

Page 39: Information Resources Management

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

Page 40: Information Resources Management

Other SQL DDLOther SQL DDL

CREATE INDEXCREATE INDEX DROP INDEXDROP INDEX CREATE VIEWCREATE VIEW DROP VIEWDROP VIEW CREATE SCHEMACREATE SCHEMA

Page 41: Information Resources Management

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

Page 42: Information Resources Management

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?

Page 43: Information Resources Management

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)

Page 44: Information Resources Management

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)

Page 45: Information Resources Management

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

Page 46: Information Resources Management

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)

Page 47: Information Resources Management

Other Relational LanguagesOther Relational Languages

Chapter 10 of bookChapter 10 of book

Query-by Example (QBE)Query-by Example (QBE) AccessAccess

Page 48: Information Resources Management

In-Class ExerciseIn-Class Exercise

SQLSQL All 21 queriesAll 21 queries

Page 49: Information Resources Management

Homework #6Homework #6

Do remaining 5 from HW #5Do remaining 5 from HW #5 Keep problem numbersKeep problem numbers