61
1 6 Structured Query Language (SQL) Advanced Topics And How to build something useful MIS 304 Winter 2006

Structured Query Language (SQL) Advanced Topics And How to build something useful

  • Upload
    nanji

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

Structured Query Language (SQL) Advanced Topics And How to build something useful. MIS 304 Winter 2006. Goal for this class. Advanced SQL Commands Understand how to use Relational Technologies to solve “M to N” and other advanced problems. - PowerPoint PPT Presentation

Citation preview

Page 1: Structured Query Language (SQL) Advanced Topics And How to build something useful

1

6

Structured Query Language (SQL)Advanced Topics

And

How to build something useful

MIS 304 Winter 2006

Page 2: Structured Query Language (SQL) Advanced Topics And How to build something useful

2

6

Goal for this class

• Advanced SQL Commands• Understand how to use Relational

Technologies to solve “M to N” and other advanced problems.

• Understand Relational Database Cost/Benefit analysis.

• Be able to write SQL queries to solve advanced problems.

Page 3: Structured Query Language (SQL) Advanced Topics And How to build something useful

3

6

Advanced SQL

• SQL is at the same time simple and yet deceptively complex.

• Worse, simple queries can run very slowly and complex one very quickly.

• The trick to learning SQL is just to do it.

Page 4: Structured Query Language (SQL) Advanced Topics And How to build something useful

4

6

Sub Queries

• Anything enclosed in Parentheses ( ) is done first as a separate query.

• It returns a result set that you can use in the “main” query.

• Commonly used with the IN operator.

Page 5: Structured Query Language (SQL) Advanced Topics And How to build something useful

5

6

IN

• SELECT * FROM Product WHERE V_Code = 12345 OR V_Code = 12346;

• SELECT * FROM Product WHERE V_Code IN (12345, 12346);

• SELECT * FROM Product WHERE V_Code IN (SELECT V_CODE FROM VENDOR WHERE State = ‘MI’);

Page 6: Structured Query Language (SQL) Advanced Topics And How to build something useful

6

6

Sub Query Hints

• Sub Queries can be hard to use.• Develop the Sub Query first as a

separate query. Make sure it works independently first.

• Remember a Sub Query returns a ‘virtual’ table.

• Keep sub queries to a minimum.

Page 7: Structured Query Language (SQL) Advanced Topics And How to build something useful

7

6

• Listing Unique Values

SELECT DISTINCT P_CODEFROM POLine;

SELECT DISTINCT SELECT DISTINCT

Page 8: Structured Query Language (SQL) Advanced Topics And How to build something useful

8

6

SELECT TOP*

SELECT TOP n FROM nnn WHERE x=y

Example:SELECT TOP 5 TotalPopFROM CountyORDER BY TotalPop DESC;

Page 9: Structured Query Language (SQL) Advanced Topics And How to build something useful

9

6

LIKE

• Show all of the data for Products whose Description contains CD (i.e. CD, CD-ROM, CD-RW…)

• SELECT * FROM ProductWHERE Description LIKE ‘CD%’;

• % Multicharacter match (* in Access)• _ Single character match (? In Access)

Page 10: Structured Query Language (SQL) Advanced Topics And How to build something useful

10

6Soundex

There is an “algorithm” that lets you search for words that sound alike.

1. Capitalize all letters in the word and drop all punctuation marks. Pad the word with rightmost blanks as needed during each procedure step.

2. Retain the first letter of the word. 3. Change all occurrence of the following letters to '0' (zero):

  'A', E', 'I', 'O', 'U', 'H', 'W', 'Y'. 4. Change letters from the following sets into the digit given:

– 1 = 'B', 'F', 'P', 'V' – 2 = 'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z' – 3 = 'D','T' – 4 = 'L' – 5 = 'M','N' – 6 = 'R'

5. Remove all pairs of digits which occur beside each other from the string that resulted after step (4).

6. Remove all zeros from the string that results from step 5.0 (placed there in step 3)

7. Pad the string that resulted from step (6) with trailing zeros and return only the first four positions, which will be of the form <uppercase letter> <digit> <digit> <digit>.

Page 11: Structured Query Language (SQL) Advanced Topics And How to build something useful

11

6Converting Columns to Rows

• Suppose you have a table that has Attributes for the various months and you now need to roll to another year.

• SELECT Code, Jan FROM TestUNION ALL SELECT Code, Feb FROM TestUNION ALL SELECT Code, Mar FROM TestUNION ALL SELECT Code, Apr FROM Test;

Code Jan Feb Mar Apr …

1 20 23 24 25

2 33 56 67 55

3 21 22 22 22

Test

Page 12: Structured Query Language (SQL) Advanced Topics And How to build something useful

12

6Another Example

ExampleCOLA COLB COLC COLD 1 2 3 4 5 6 7 8

ResultCOLX COLY A 1 A 5 B 2 B 6 C 3 C 7 D 4 D 8

SELECT 'A' as COLX, COLA as COLY from Example UNION ALL SELECT 'B' , COLB from Example UNION ALL SELECT 'C' , COLC from Example UNION ALLSELECT 'D' , COLDfrom Example order by COLX, COLY

Page 13: Structured Query Language (SQL) Advanced Topics And How to build something useful

13

6

How to Build Something Useful

Page 14: Structured Query Language (SQL) Advanced Topics And How to build something useful

14

6

The Issue

• Businesses, Health Care facilities, Non-profits, the Military and Governmental agencies run on the movement of information. In other words…

Paperwork

Page 15: Structured Query Language (SQL) Advanced Topics And How to build something useful

15

6

The Business of IT

• Very often you are given a bunch of seemingly unconnected “artifacts” and you need to make something out of them.

• These can take the form of Databases, Files, Spreadsheets, Word Documents but most often pieces of paper.

Page 16: Structured Query Language (SQL) Advanced Topics And How to build something useful

16

6

Paperwork

• Modeling the structure and the flow of paper or now paper like objects is the nature of what IT people do.

• Most of those pieces of paper end up as one or more tables in a relational database.

Page 17: Structured Query Language (SQL) Advanced Topics And How to build something useful

17

6

Forms Examples

• The Purchase Order• The Invoice• Intake logs• Test results• Etc…..

Page 18: Structured Query Language (SQL) Advanced Topics And How to build something useful

18

6

Forms and Business Rules

• Forms also encode many of our business rules

• You may need to represent some of the business rules in your database.

• Examples– Code sets

– Numbering schemes

– etc

Page 19: Structured Query Language (SQL) Advanced Topics And How to build something useful

19

6

What is so difficult about this?

• What are the “Facts” we find here?

Page 20: Structured Query Language (SQL) Advanced Topics And How to build something useful

20

6

Facts about many things

• Facts about the PO or Invoice itself• Facts about customers or suppliers• Facts about items sold or shipped• Facts about payment• Facts about shipping• Facts about business process• Misc. facts

Page 21: Structured Query Language (SQL) Advanced Topics And How to build something useful

21

6

Lets build a model

Page 22: Structured Query Language (SQL) Advanced Topics And How to build something useful

22

6

The Simplest VersionPhase 0

PurchaseOrder

What is wrong with this approach?

Page 23: Structured Query Language (SQL) Advanced Topics And How to build something useful

23

6

PO Table

What are these facts about?

Page 24: Structured Query Language (SQL) Advanced Topics And How to build something useful

24

6

POInfo Lines

M1

PO Model phase 1PO Model phase 1

Has

How much redundancy does this add?

Page 25: Structured Query Language (SQL) Advanced Topics And How to build something useful

25

6

Results

• You need a new Foreign Key in the Lines table that adds a link to the new Primary Key PONumber

• This means you are going to duplicate the PONumber many times in the tables.

Page 26: Structured Query Language (SQL) Advanced Topics And How to build something useful

26

6

What else can we do?

• Look for other “redundancies”• What is the Cost/Benefit of adding new

tables to address them?1

New FK FieldSize X N = %ofDuplicates X Total RecordSize X N

New FK FieldSize = %ofDuplicates Total RecordSize

1. cost of redundancy only

Page 27: Structured Query Language (SQL) Advanced Topics And How to build something useful

27

6

POInfo Line

N1

Vendor

1

PO Model phase 2PO Model phase 2

Has

Has

M

Page 28: Structured Query Language (SQL) Advanced Topics And How to build something useful

28

6

POInfo Product

PONumber LineKey

1 M

PartNum

PONumber

M 1

PartNum

Vendor

VendNum

VendNum

POLine

M

PO Model phase 3PO Model phase 3

PartDescr

VendAddr

1

Page 29: Structured Query Language (SQL) Advanced Topics And How to build something useful

29

6

Where do you put?

• Totals and subtotals?

• Taxes?

• Payment details?

Page 30: Structured Query Language (SQL) Advanced Topics And How to build something useful

30

6

Data Dictionary

Table Attribute DataType PK/FK On

Vendor V_Code Integer PK

V_Name Char

Product P_Code Integer PK

POInfo PONum Integer PK

V_Code Integer FK Vendor

POLine LineKey Integer PK

PONum Integer FK POInfo

P_Code Integer FK Product

Page 31: Structured Query Language (SQL) Advanced Topics And How to build something useful

31

6

Now Build the Tables

Page 32: Structured Query Language (SQL) Advanced Topics And How to build something useful

32

6

Data Definition CommandsData Definition Commands

CREATE TABLE POInfo (PO_Number FCHAR(5) NOT NULL

UNIQUE,V_CODE FCHAR(3) NOT NULL,PRIMARY KEY (PO_Number);

FOREIGN KEY (S_CODE) REFERENCES VENDOR ON DELETE RESTRICT

ON UPDATE CASCADE));

Page 33: Structured Query Language (SQL) Advanced Topics And How to build something useful

33

6

Data Definition CommandsData Definition Commands

CREATE TABLE POLine(LineKey FCHAR(5) NOT NULL UNIQUE, PONumber VCHAR(35) NOT NULL, PartNum VCHAR(15) NOT NULL, PRIMARY KEY (LineKey));

FOREIGN KEY (PONumber) REFERENCES POInfo ON DELETE RESTRICT

ON UPDATE CASCADE));

FOREIGN KEY (PartNum) REFERENCES Product ON DELETE RESTRICT

ON UPDATE CASCADE));

Page 34: Structured Query Language (SQL) Advanced Topics And How to build something useful

34

6

Data Definition CommandsData Definition Commands

CREATE TABLE Vendor(V_CODE FCHAR(5) NOT NULL UNIQUE, V_NAME VCHAR(35) NOT NULL, V_CONTACT VCHAR(15)NOT NULL, V_AREACODE FCHAR(3) NOT NULL, V_PHONE FCHAR(3) NOT NULL, V_STATE FCHAR(2) NOT NULL, v_ORDER FCHAR(1) NOT NULL, PRIMARY KEY (V_CODE));

Page 35: Structured Query Language (SQL) Advanced Topics And How to build something useful

35

6

Data Definition CommandsData Definition Commands

CREATE TABLE PRODUCT(P_CODE VCHAR(10) NOT NULL UNIQUE, P_DESCRIPT VCHAR(35) NOT NULL, P_INDATE DATE NOT NULL, P_ONHAND SMALLINT NOT NULL, P_MIN SMALLINT NOT NULL, P_PRICE DECIMAL(8,2) NOT NULL, P_DISCOUNT DECIMAL(4,1) NOT NULL, V_CODE SMALLINT, PRIMARY KEY (P_CODE), FOREIGN KEY (S_CODE) REFERENCES VENDOR ON DELETE RESTRICT

ON UPDATE CASCADE);

Page 36: Structured Query Language (SQL) Advanced Topics And How to build something useful

36

6

Data Definition CommandsData Definition Commands

• SQL Integrity Constraints

– Entity Integrity• PRIMARY KEY• NOT NULL and UNIQUE

– Referential Integrity• FOREIGN KEY • ON DELETE• ON UPDATE

Remember the business rules!

Page 37: Structured Query Language (SQL) Advanced Topics And How to build something useful

37

6

How About “Payment Type”?

• How do you handle something like this?• What is it a “fact” about.

Page 38: Structured Query Language (SQL) Advanced Topics And How to build something useful

38

6

How about a more Complex Example

Page 39: Structured Query Language (SQL) Advanced Topics And How to build something useful

39

6

Sub Sections

Page 40: Structured Query Language (SQL) Advanced Topics And How to build something useful

40

6

Modeling Issues

• Keep the “Facts” right.• Look at the Cost/Benefit relationships• Make sure you get rid of all of the

M x N relationships.• KISS = Keep It Simple Students.

Page 41: Structured Query Language (SQL) Advanced Topics And How to build something useful

41

6

So now that you built it

• We need to query it.• We need to manage it.

Page 42: Structured Query Language (SQL) Advanced Topics And How to build something useful

42

6

Page 43: Structured Query Language (SQL) Advanced Topics And How to build something useful

43

6

How would we recreate the Purchase Order in SQL?

• It depends on what the Model looks like.

Page 44: Structured Query Language (SQL) Advanced Topics And How to build something useful

44

6

Phase 0 revisited

Simple SQL based on One table.

Page 45: Structured Query Language (SQL) Advanced Topics And How to build something useful

45

6

Listing the Table ContentsListing the Table Contents

SELECT * FROM PurchaseOrder;

SELECT P_CODE, P_DESCRIPT, P_INDATE, P_ONHAND, P_MIN, P-PRICE, P_DISCOUNT, S_CODE FROM PurchaseOrder;

But we said this One Table approach was too costly!

Page 46: Structured Query Language (SQL) Advanced Topics And How to build something useful

46

6

POInfo Parts

PONumber LineKey

1 M

PartNum

PONumber

M 1

PartNum

Vendor

VendNum

VendNum

POLine

M

PO Model phase 3PO Model phase 3PartDescr

VendAddr

Page 47: Structured Query Language (SQL) Advanced Topics And How to build something useful

47

6

The Key Question

• What do you want to know?

Page 48: Structured Query Language (SQL) Advanced Topics And How to build something useful

48

6

Bringing it Together

• We now need to get information from more than one table.

• This is done with the SQL “Join”.• Joins use the Primary and Foreign

Keys to link the tables in the SQL Command together.

Remember!!!WHERE TABLE1.PRIMARY=TABLE2.FOREIGN

Page 49: Structured Query Language (SQL) Advanced Topics And How to build something useful

49

6

More Complex Queries

SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME, VENDOR.V_CONTACT, VENDOR.V_AREACODE, VENDOR.V_PHONEFROM PRODUCT, VENDORWHERE PRODUCT.V_CODE = VENDOR.V_CODE

AND VENDOR.V_CODE=1234;

Page 50: Structured Query Language (SQL) Advanced Topics And How to build something useful

50

6

Another Cost

• When you join two tables you incur a cost due to the Join process.

• A Join builds an intermediate file that this the combination of all of the entity instances of the one table combined with all of the entity instances of the other table.

• These tables can get very large.

Page 51: Structured Query Language (SQL) Advanced Topics And How to build something useful

51

6

Join

WHERE CUSTOMER.AGENT_CODE = AGENT.AGENT_CODE

Page 52: Structured Query Language (SQL) Advanced Topics And How to build something useful

52

6

Product of the two tables

Page 53: Structured Query Language (SQL) Advanced Topics And How to build something useful

53

6

Refine the Join

Page 54: Structured Query Language (SQL) Advanced Topics And How to build something useful

54

6

More Complex Queries

• Anything in a valid SQL statement can be put in the WHERE part of a joined SQL Query.

• More than two tables can be joined.• YOU MUST HAVE THE JOIN EQUALITY• This means they can get quite complex• You can use Sub Queries ( ) and

“pretty printing” to help keep things straight.

Page 55: Structured Query Language (SQL) Advanced Topics And How to build something useful

55

6

Print a PO

SELECT PO.PONumber, VENDOR.V_NAME, POLine.Line, PRODUCT.P_CODE, PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE

FROM VENDOR, PRODUCT, PO, POLine WHERE PO.PONumber = POLine.PONumber

AND PRODUCT.P_CODE = POLine.P_CODE AND VENDOR.V_CODE = PO.V_CODE

AND PO.PONumber ="1001";

Page 56: Structured Query Language (SQL) Advanced Topics And How to build something useful

56

6

Results

PONumber V_NAME Line P_CODE P_DESCRIPT P_PRICE

1001 Rubicon Sis.

1 11QER/31 Power painter, 15 psi., 3-nozzle

$109.99

1001 Rubicon Sis.

2 WR3/TT3 Steel matting, 4'x8'x1/6", .5" mesh

$119.95

Page 57: Structured Query Language (SQL) Advanced Topics And How to build something useful

57

6

Complications

• Any “high fidelity” representation of the PO is going to take multiple passes through the SQL engine.

• The best way to do this is with a Report Writing tool. (e.g. Crystal Reports)

• Even the report tool is SQL based.

Page 58: Structured Query Language (SQL) Advanced Topics And How to build something useful

58

6

Sub Query Example

SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME

FROM PRODUCT, VENDORWHERE PRODUCT.V_CODE =

VENDOR.V_CODEAND VENDOR.V_CODE IN

(SELECT VENDOR.V_CODE FROM VENDOR WHERE VENDOR.V_CODE > “1234-TK”) ;

Page 59: Structured Query Language (SQL) Advanced Topics And How to build something useful

59

6

Data Input

• What can we learn about Data Input from looking at the models?

Page 60: Structured Query Language (SQL) Advanced Topics And How to build something useful

60

6

Data Input

• The more complex the model the more data validation issues you have.

• Here again you can use a sub query to look up data in an existing table.

• You also need to SQL Enable data input devices.

Page 61: Structured Query Language (SQL) Advanced Topics And How to build something useful

61

6