Structured Query Language (SQL) How to build something useful

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

CHAPTER OBJECTIVE: NORMALIZATION THE SNOWFLAKE SCHEMA.
What is a Database By: Cristian Dubon.
7 7 SQL Data Definition 4Spread throughout chapter 7.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
Chapter 2 The Relational Database Model
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
The Relational Database Model:
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
5 Chapter 5 Structured Query Language (SQL1) Revision.
Introduction to Structured Query Language (SQL)
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
The Relational Database Model
3 The Relational Model MIS 304 Winter Class Objectives That the relational database model takes a logical view of data That the relational model’s.
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 7 Introduction to Structured Query Language (SQL)
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
ZEIT2301 Design of Information Systems SQL: Creating a Database School of Engineering and Information Technology Dr Kathryn Merrick.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
CODD’s 12 RULES OF RELATIONAL DATABASE
Designing a Database (Part I) -Identify all fields needed to produce the required information -Group related fields into tables -Determine Each Table’s.
Chapter 5 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
Database Systems: Design, Implementation, and Management Tenth Edition
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
7 7 SQL Data Modification 4Spread throughout chapter 7.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
SQL – Structured Query Language
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 3 The Relational Database Model.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Description and exemplification use of a Data Dictionary. A data dictionary is a catalogue of all data items in a system. The data dictionary stores details.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
N5 Databases Notes Information Systems Design & Development: Structures and links.
CompSci 280 S Introduction to Software Development
Fundamentals of DBMS Notes-1.
CIS 336 AID Your Dreams Our Mission/cis336aid.com
The Relational Database Model
Advanced SQL Advanced SQL Week 8 Comparison Operators
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 4 Relational Model Characteristics
Introduction to Database Systems
Lecture 2 The Relational Model
CIS 336 Competitive Success/snaptutorial.com
CIS 336 Education for Service-- snaptutorial.com.
CIS 336 Teaching Effectively-- snaptutorial.com
Databases and Information Management
ORACLE SQL Developer & SQLPLUS Statements
Chapter 3 The Relational Database Model
STRUCTURED QUERY LANGUAGE
CS4222 Principles of Database System
Teaching slides Chapter 8.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Databases and Information Management
Oracle Data Definition Language (DDL)
The Relational Database Model
COP 4610L: Applications in the Enterprise Spring 2005
Relational Database Design
DCT 2053 DATABASE CONCEPT Chapter 2.2 CONTINUE
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

Structured Query Language (SQL) How to build something useful 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. Understand Relational Database Cost/Benefit analysis. Be able to write SQL queries to solve advanced problems.

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.

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.

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

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.

SELECT DISTINCT Listing Unique Values SELECT DISTINCT P_CODE FROM POLine; 52

SELECT TOP* SELECT TOP n FROM nnn WHERE x=y Example: SELECT TOP 5 TotalPop FROM County ORDER BY TotalPop DESC;

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)

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

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;

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 1 2 3 4 5 6 7 8 Result COLX COLY A 1 A 5 B 2 B 6 C 3 C 7 D 4 D 8

How to Build Something Useful

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

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.

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.

Forms Examples The Purchase Order The Invoice Intake logs Test results Etc…..

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

What is so difficult about this? What are the “Facts” we find here?

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

Lets build a model

The Simplest Version Phase 0 PurchaseOrder What is wrong with this approach?

PO Table What are these facts about?

PO Model phase 1 1 M Has POInfo Lines How much redundancy does this add?

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.

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

PO Model phase 2 1 N Has POInfo Line M 1 Has Vendor

PO Model phase 3 PartNum PartDescr PONumber LineKey PartNum PONumber 1 POInfo POLine Product M 1 VendNum Vendor VendNum VendAddr

Where do you put? Totals and subtotals? Taxes? Payment details?

Data Dictionary Table Attribute DataType PK/FK On Vendor V_Code Integer PK V_Name Char Product P_Code POInfo PONum FK POLine LineKey

Now Build the Tables

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

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

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

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

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

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

How about a more Complex Example

Sub Sections

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.

So now that you built it We need to query it. We need to manage it.

How would we recreate the Purchase Order in SQL? It depends on what the Model looks like.

Phase 0 revisited Simple SQL based on One table.

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

PO Model phase 3 PartNum PartDescr PONumber LineKey PartNum PONumber 1 POInfo POLine Parts M VendNum Vendor VendNum VendAddr

The Key Question What do you want to know?

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

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;

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.

WHERE CUSTOMER.AGENT_CODE = AGENT.AGENT_CODE Join WHERE CUSTOMER.AGENT_CODE = AGENT.AGENT_CODE

Product of the two tables

Refine the Join

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.

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";

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

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.

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

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

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.