Accelerated Computer Training for Working Professionals Bookstore Case Orange Coast Database Associates Course (800)355-9855 or http://www.d2associates.com Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Classes custom designed forWorking Professionals http://www.d2associates.com San Juan Capistrano, CA (800)355-9855 SQL201W Accelerated Introduction to SQL Using MySQL
SQL201W Accelerated Introduction to SQL Using MySQL. Covers create, alter,drop, select, insert,update and delete. Includes joins, calculations and grouping.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Accelerated Computer Training for Working Professionals Bookstore Case
Orange Coast Database Associates Course (800)355-9855 or http://www.d2associates.com
Orange CoastDatabase Associates
Specializing in Microsoft Office,
Access, SQL, and related technologiesClasses custom designed forWorking Professionals
http://www.d2associates.com
San Juan Capistrano, CA
(800)355-9855
SQL201W Accelerated Introductionto SQL Using MySQL
1
Bookstore SQL201W 1
An accelerated introduction to SQL for non-programmers
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com
Welcome to SQL201W –
Accelerated Introduction toSQL Using MySQL
Bookstore SQL201W 2
Accelerated Introduction toSQL
• Introduction (s)
• Facilities
• Course Packet (contents may vary)– Student questionnaire
– Collaterals (Maps, Catalogs, Etc.)
– PowerPoint handouts for all sessions
– Evaluation form
– Training certificate
2
Bookstore SQL201W 3
SQL Curriculum
SQL200*
SQL200S*
SQL212Oracle
SQL202Transact-SQL
SQL201WMySQL
* = included inabove courses
Bookstore SQL201W 4
Accelerated Introduction to SQL
• Assumes no prior knowledge of SQL
• Quick pace for experienced computer users
• End-user, not programmer, oriented
• Focus is on SQL, not Management Studio
• SQL200s is first two modules of SQL200,otherwise identical.
3
Bookstore SQL201W 5
Accelerated Introduction toSQL
• Select– Basic– Filters– Special Operators– Multi-table retrieval
• Joins• Subqueries• Unions
– Calculations andAggregates
SQL Covered (Day 1: All Day):
Bookstore SQL201W 6
Accelerated Introduction toSQL
• Data Updates– Insert– Update– Delete
• Data structures– Create– Drop– Alter
SQL Covered (Day 2: AM Only):
4
Bookstore SQL201W 7
Accelerated Introduction toSQL
• 3 Sessions
• Lecture
• Demo
• Student “hands-on”
• Many exercises are cumulative –later examples build on queriescreated earlier
Course Format:
Bookstore SQL201W 8
Accelerated Introduction toSQL
• Session 1 – Basic SQL
• Session 2 – Multi-tableRetrieval
• Session 3 – Modifying Data
Course Schedule (“1/2” day sessions):
1
Bookstore SQL201W Module 1 1
SQL201W
SQL ProgrammingBased on SQL Clearly Explained by Jan Harrington
Module 1 – Relational Database Background,Basic Single Table Retrieval Operations
Note on SQL200 Slides
• These slides were originally designed to support thesingle SQL200 course which was used for any ofMS Access, MySQL, Oracle and SQL Server.
• As such you may see here slides developed in anyone of the above products.
• We are in the process of migrating the variousvendor slides out into their own slide sets. TheSQL200 slides will cover MySQL and SQL Serverwhich are virtually identical for purposes of thiscourse.
Bookstore SQL201W Module 1 2
2
Bookstore SQL201W Module 1 3
Warning!
• Below are some table name changes to beaware of in doing queries. We have createdsynonyms so either name should work.
New Name Old Name
Orders Order_filled
Order_Lines Orderlines
Bookstore SQL201W Module 1 4
SQL200 Contact Information
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://[email protected]
SQL uses three valued logic. Must use IS NULLto test for unknowns. A null is NOT the same asblank or empty.
IS NULL Results
Bookstore SQL201W Module 1 48
25
Bookstore SQL201W Module 1 49
On Your Own
• Find all customers with an address not equal to4592 Maple Lane
• Was Peter Johnson selected?
• Why or why not?
Bookstore SQL201W Module 1 50
BETWEEN
List all orders placed in 1999.
select *from orderswhere order_date BETWEEN ‘1-Jan-99’ and ’31-Dec-99’;
Note: date formats vary from product to product.
26
Bookstore SQL201W Module 1 51
Where with Between
Bookstore SQL201W Module 1 52
Removing Duplicates
Select DISTINCTcustomer_cityFrom customers;
List once each city in which there arecustomers
Removes duplicate rows from result set
27
Bookstore SQL201W Module 1 53
Removing Duplicates
Bookstore SQL201W Module 1 54
Sorting – ORDER BY
DESC will sort in descending order
Basic syntax:
Select <column list> or *From <table list>Where <selection criteria>Order by <column list> [DESC]
28
Bookstore SQL201W Module 1 55
Sorting – ORDER BY
select *from customersorder by customer_state,customer_city
List all records sorted by state, city
Bookstore SQL201W Module 1 56
Sorting Results with Order By
29
Bookstore SQL201W Module 1 57
SQL Exercises
• List all books whose publisher name beginswith “H” or “T”; sort by title [hint: use LIKE]
• List all customers whose last name ends with“S”; sort by state, city, last name
• Find the order numbers of orders with orderdates in 1999; sort by order #. [Hint: useBETWEEN]
• Find the order numbers and order dates of allorders with a “2” in character position 2 of thecredit card #; sort by order date descending
[end module]
Notes
Bookstore SQL201W Module 1 58
30
Notes
Bookstore SQL201W Module 1 59
Notes
Bookstore SQL201W Module 1 60
1
Bookstore SQL201W Module 2 1
SQL201W
SQL Programming
Workshop 2 – Joins, Calculations and Grouping
Based on SQL Clearly Explained by Jan Harrington
Note on SQL200 Slides
Bookstore SQL201W Module 2 2
• These slides were originally designed to support thesingle SQL200 course which was used for any of MSAccess, MySQL, Oracle and SQL Server.
• As such you may see here slides developed in any one ofthe above products.
• We are in the process of migrating the various vendorslides out into their own slide sets. The SQL200 slideswill cover MySQL and SQL Server which are virtuallyidentical for purposes of this course.
2
Bookstore SQL201W Module 2 3
Warning!
• Below are some table name changes to beaware of in doing queries. We have createdsynonyms so either name should work.
New Name Old Name
Orders Order_filled
Order_Lines Orderlines
Bookstore SQL201W Module 2 4
SQL200 Contact Information
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://[email protected]
selectconcat(customer_first_name,’ ‘,customer_last_name)as Name
from customers
30
Concatenation
Bookstore SQL201W Module 2 59
Bookstore SQL201W Module 2 60
Pretty up the concatenation
• Use trimfunctions, add aspace
• Sort by lastname – a neattrick forreporting!
31
Bookstore SQL201W Module 2 61
Date Functions
• Numerous date functions
• Often vendor specific
• Often used:– year
– month
• Ex: where year(order_date) = 1999
Bookstore SQL201W Module 2 62
Date Functions
• Remember your three table query with theorder dates?
• Restrict it to orders in December
32
Bookstore SQL201W Module 2 63
Aggregate Functions
• Count
• Sum
• Min
• Max
• Avg
• Often used in conjunction with grouping
Bookstore SQL201W Module 2 64
Aggregate Functions
Basic syntax:
Select <function>(<column>)From <table>Group by <column-list>Having <predicate>
Group by all columns to left of one(s)you want to aggregate
33
Bookstore SQL201W Module 2 65
Aggregate Functions
SELECT order_numb, Count(*) AS[Number of Order Lines] ,Sum(quantity) AS [Total Quantity],Sum(quantity * cost_each) AS [TotalAmount]
FROM order_lines
GROUP BY order_numb
having count(*) > 1;
Bookstore SQL201W Module 2 66
Aggregates with Group By
34
Bookstore SQL201W Module 2 67
Having Clause
SELECT Order_lines.order_numb,Count(*) AS ‘Number of Order Lines ‘,Sum(Order_lines.quantity) ASTotal_Quantity,Sum(quantity*cost_each) AS extension
FROM Order_lines
GROUP BY Order_lines.order_numb
having count(*) > 1
Bookstore SQL201W Module 2 68
Group By with Having
35
Bookstore SQL201W Module 2 69
Having vs. Where
• Having and Where clauses are similar butnot the same
• Having removes groups after they areformed
• Where removes rows before groups areformed
Bookstore SQL201W Module 2 70
On Your Own
• Find the aggregates without any grouping –see what you get
• Then restrict the ISBN
36
Bookstore SQL201W Module 2 71
Summary Exercise
• List all customers and their orders
– Name nicely formatted
– With orders in the year of 1999 (do not usebetween, etc.)
– Show total order quantities and amounts
– Only include orders with more than threeorder lines
Bookstore SQL201W Module 2 72
Exercise Result
[end module]
1
Bookstore SQL201W Module 3 1
SQL/200
SQL Programming
Workshop 3 – Modifying Data, Managing theDatabase
Based on SQL Clearly Explained by Jan Harrington
Note on SQL200 Slides
Bookstore SQL201W Module 3 2
• These slides were originally designed to support thesingle SQL200 course which was used for any ofMS Access, Oracle and SQL Server.
• As such you may see here slides developed in anyone of the above products.
• We are in the process of migrating the variousvendor slides out into their own slide sets. TheseSQL200 slides will focus on Microsoft SQL Serverand MySQL which are virtually identical forpurposes of this course.
2
Bookstore SQL201W Module 3 3
Warning!
• Below are some table name changes to beaware of in doing queries. We have createdsynonyms so either name should work.
New Name Old Name
Orders Order_filled
Order_Lines Orderlines
Bookstore SQL201W Module 3 4
SQL200 Contact Information
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://[email protected]
• Application developer preparesthese statements “behind thescenes” based on forms filled outby user
6
Bookstore SQL201W Module 3 11
Insert
• Adds new rows to an existing table
• Two forms:
– Single Row
– Multi-Row
Bookstore SQL201W Module 3 12
Single Row Insert
Basic Syntax:
Insert [into] <table-name>
Values (<value-list>)
7
Bookstore SQL201W Module 3 13
Single Row Insert
Basic Example:insert into sources(source_numb,source_name, source_street)values(22,'Specialty Books','Canal Street')
Bookstore SQL201W Module 3 14
Insert Statement
8
Bookstore SQL201W Module 3 15
Sources table after Insert
Bookstore SQL201W Module 3 16
Multi-row Insert
Basic Syntax:
Insert [into] <table-name>
Select <select-statement>
We will do this after creating a newtable later in this module
9
Bookstore SQL201W Module 3 17
Update
• Updates fields in an existing row
Basic Syntax:
Update <table-name>
Set <field1> = new value, <field2> = newvalue,…
Where <selection-criteria>
Bookstore SQL201W Module 3 18
Update
• Increase Ingram prices by 10%
Example:
Update books
Set retail_price = retail_price*1.10
Where source_numb = 1
10
Bookstore SQL201W Module 3 19
Ingram Book Prices before Update
Bookstore SQL201W Module 3 20
Ingram Book Prices after Update
11
Bookstore SQL201W Module 3 21
After update in MS Access
Bookstore SQL201W Module 3 22
Delete
• Deletes one or more rows
Basic Syntax:
Delete from <table-name>
Where <selection-criteria>
12
Bookstore SQL201W Module 3 23
Delete
Example:
Delete from sources
Where source_numb = 22
Bookstore SQL201W Module 3 24
Delete
13
Bookstore SQL201W Module 3 25
Sources table after Delete
Bookstore SQL201W Module 3 26
Delete and Referential Integrity
• Can affect referential integrity when deleting a“parent” row
• Can do following with child…– Cascade: delete the child row– Set null: set the child’s foreign key null– Set default: as above but to default value– No action: don’t allow delete of parent row
• Referential integrity can be established whencreating or modifying table structures which wewill look at later in the class
14
Bookstore SQL201W Module 3 27
SQL/200
SQL Programming
Part 2– Managing Database Structures
DDL
• Create
• Alter
• Drop
Bookstore SQL201W Module 3 28
15
Bookstore SQL201W Module 3 29
Schemas
• Logical view of a database; sort of a “sub-database” – we will not cover these in thismodule or…
– Catalogs
– Clusters
– Domains (somewhat like a user defined datatype)
• These topics are highly dependent upon thevendor DBMS and installation practices
Bookstore SQL201W Module 3 30
Tables
• Base tables
• Temporary tables
– Local (or module scope)
– Global (session scope)
16
Bookstore SQL201W Module 3 31
Creating Tables
• Use create statement
• Specify:
– Columns with data types and columnconstraints
– Table constraints
• Foreign key references
• Primary key designation
Bookstore SQL201W Module 3 32
Data Types
• Int – integers or whole numbers– Ex: how_many int