Download presentation
Presentation is loading. Please wait.
Published byGriselda Kelley Modified over 6 years ago
1
Structured Query Language (SQL) How to build something useful
Advanced Topics And How to build something useful MIS Winter 2006
2
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.
3
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.
4
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.
5
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’);
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.
7
SELECT DISTINCT Listing Unique Values
SELECT DISTINCT P_CODE FROM POLine; 52
8
SELECT TOP* SELECT TOP n FROM nnn WHERE x=y Example:
SELECT TOP 5 TotalPop FROM County ORDER BY TotalPop DESC;
9
LIKE Show all of the data for Products whose Description contains CD (i.e. CD, CD-ROM, CD-RW…) SELECT * FROM Product WHERE Description LIKE ‘CD%’; % Multicharacter match (* in Access) _ Single character match (? In Access)
10
Soundex There is an “algorithm” that lets you search for words that sound alike. Capitalize all letters in the word and drop all punctuation marks. Pad the word with rightmost blanks as needed during each procedure step. Retain the first letter of the word. Change all occurrence of the following letters to '0' (zero): 'A', E', 'I', 'O', 'U', 'H', 'W', 'Y'. 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' Remove all pairs of digits which occur beside each other from the string that resulted after step (4). Remove all zeros from the string that results from step 5.0 (placed there in step 3) 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>.
11
Converting Columns to Rows
Test Code Jan Feb Mar Apr … 1 20 23 24 25 2 33 56 67 55 3 21 22 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 Test UNION ALL SELECT Code, Feb FROM Test UNION ALL SELECT Code, Mar FROM Test UNION ALL SELECT Code, Apr FROM Test;
12
Another Example SELECT 'A' as COLX, COLA as COLY from Example
UNION ALL SELECT 'B' , COLB SELECT 'C' , COLC SELECT 'D' , COLD order by COLX, COLY Example COLA COLB COLC COLD Result COLX COLY A A B B C C D D
13
How to Build Something Useful
14
The Issue Businesses, Health Care facilities, Non-profits, the Military and Governmental agencies run on the movement of information. In other words… Paperwork
15
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.
16
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.
17
Forms Examples The Purchase Order The Invoice Intake logs Test results
Etc…..
18
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
19
What is so difficult about this?
What are the “Facts” we find here?
20
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
21
Lets build a model
22
The Simplest Version Phase 0
PurchaseOrder What is wrong with this approach?
23
PO Table What are these facts about?
24
PO Model phase 1 1 M Has POInfo Lines
How much redundancy does this add?
25
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.
26
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
27
PO Model phase 2 1 N Has POInfo Line M 1 Has Vendor
28
PO Model phase 3 PartNum PartDescr PONumber LineKey PartNum PONumber 1
POInfo POLine Product M 1 VendNum Vendor VendNum VendAddr
29
Where do you put? Totals and subtotals? Taxes? Payment details?
30
Data Dictionary Table Attribute DataType PK/FK On Vendor V_Code
Integer PK V_Name Char Product P_Code POInfo PONum FK POLine LineKey
31
Now Build the Tables
32
Data 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)); 12
33
Data 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)); 12
34
Data 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)); 12
35
Data 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); 13
36
Data 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! 14
37
How About “Payment Type”?
How do you handle something like this? What is it a “fact” about.
38
How about a more Complex Example
39
Sub Sections
40
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.
41
So now that you built it We need to query it. We need to manage it.
43
How would we recreate the Purchase Order in SQL?
It depends on what the Model looks like.
44
Phase 0 revisited Simple SQL based on One table.
45
Listing 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! 18
46
PO Model phase 3 PartNum PartDescr PONumber LineKey PartNum PONumber 1
POInfo POLine Parts M VendNum Vendor VendNum VendAddr
47
The Key Question What do you want to know?
48
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
49
More Complex Queries SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME, VENDOR.V_CONTACT, VENDOR.V_AREACODE, VENDOR.V_PHONE FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE AND VENDOR.V_CODE=1234;
50
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.
51
WHERE CUSTOMER.AGENT_CODE = AGENT.AGENT_CODE
Join WHERE CUSTOMER.AGENT_CODE = AGENT.AGENT_CODE
52
Product of the two tables
53
Refine the Join
54
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.
55
Print a PO FROM VENDOR, PRODUCT, PO, POLine
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";
56
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 2 WR3/TT3 Steel matting, 4'x8'x1/6", .5" mesh $119.95
57
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.
58
Sub Query Example SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE AND VENDOR.V_CODE IN (SELECT VENDOR.V_CODE FROM VENDOR WHERE VENDOR.V_CODE > “1234-TK”) ;
59
Data Input What can we learn about Data Input from looking at the models?
60
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.