Accelerated Computer Training for Working Professionals Bookstore Case Orange Coast Database Associates Course PowerPoint Slides (800)355-9855 or http://www.dhdursoassociates.com Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Classes custom designed forWorking Professionals http://www.dhdursoassociates.com San Juan Capistrano, CA (800)355-9855 SQL302 Intermediate SQL Using Microsoft SQL Server
SQL302 Intermediate SQL using Microsoft SQL Server. Covers additional uses of subqueries, set functions, table expressions, with clause, new SQL window functions.
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 PowerPoint Slides (800)355-9855 or http://www.dhdursoassociates.com
Orange CoastDatabase Associates
Specializing in Microsoft Office,
Access, SQL, and related technologiesClasses custom designed forWorking Professionals
http://www.dhdursoassociates.com
San Juan Capistrano, CA
(800)355-9855
SQL302 Intermediate SQL UsingMicrosoft SQL Server
1
Bookstore SQL302 1
An accelerated course in intermediate SQL forexperienced SQL users.
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com
Welcome to SQL302 –
Intermediate SQLUsing MS SQL Server
Bookstore SQL302 2
Intermediate SQL
• Introduction (s)
• Facilities
• Course Packet (contents may vary)– Student questionnaire
– Collaterals (Maps, Catalogs, Etc.)
– PowerPoint handouts for all sessions
– Evaluation form
– Training certificate
2
Bookstore2 SQL302 Module 2 3
SQL302 Contact Information
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://[email protected]
where cost_each > (select AVG(cost_each) fromorderlines)
order by isbn;
Bookstore SQL302 Module 2 61
Technique 2 – using a CTE
with average_cost_cte(average_cost)
as
(select AVG(cost_each)
from orderlines)
select isbn, cost_each, average_cost
from orderlines, average_cost_cte
where cost_each > average_cost
order by isbn;
Bookstore SQL302 Module 2 62
32
Bookstore SQL302 Module 1 63
SQL Exercises
• List all customers whose last name does notend with s or t.
• Find all customers who have not placed anorder.
• Find all order lines with an amount less thanthe average. Include the order date without thetime.
• List all books with a price greater than theaverage price for all books. Show the variancefrom the average, too. Use a CTE.
[end module]
Notes
Bookstore SQL302 Module 1 64
1
Bookstore SQL302 Module 2 1
SQL302
Intermediate SQL Programming
Workshop 2 – Joins, Set Operations, WindowFunctions
Based on SQL Clearly Explained by Jan Harrington and Microsoft SQL Server T-SQL Fundamentals by Itzki Ben-gan
Note on SQL302 Slides
• These slides were originally designed to support asingle SQL course which was used for any of MSAccess, 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 Oracle,Access, and MySQL slides out into their own slidesets. The SQL302 slides will cover Microsoft SQLServer.
Bookstore SQL302 Module 2 2
2
Bookstore SQL302 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 SQL302 Module 2 4
SQL302 Contact Information
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://[email protected]
, SUM(salary) over(partitionby job_title_code) as [Job CodeAverage]
, SUM(salary) over() as[Average]from employees;
27
Bookstore SQL302 Module 2 53
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
Notes
Bookstore SQL302 Module 2 54
28
Notes
Bookstore SQL302 Module 2 55
Notes
Bookstore SQL302 Module 2 56
1
SQL Server/Employees SQL302 Module 3 1
SQL302
Intermediate SQL Programming
Workshop 3 – Modifying Data, Managing theDatabase
Based on SQL Clearly Explained by Jan Harrington and Microsoft SQL Server T-SQL Fundamentals by Itzki Ben-gan
Note on SQL302 Slides
SQL Server/Employees SQL302 Module 3 2
• These slides were originally designed to support thesingle SQL302 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 OracleMySQL, and MS Access slides out into their ownslide sets. These SQL302 slides will focus onMicrosoft SQL Server.
2
SQL Server/Employees SQL302 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
SQL Server/Employees SQL302 Module 3 4
SQL302 Contact Information
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://[email protected]
SQL Server allows updates from a table, including joinedtables. As an example one could update quantity on handin an inventory table from a transaction table.
12
Update from
• Example: increase managerial salaries
SQL Server/Employees SQL302 Module 3 23
SQL Server/Employees SQL302 Module 3 24
UPDATE From Results
13
Update w/ subquery in set clause
• Update employee temp table – set salaries to theaverage for that code
SQL Server allows deletes from a table based on asubquery or a join condition.
Basic syntax:
14
Delete from
• Example: Delete unused job titles
SQL Server/Employees SQL302 Module 3 27
SQL Server/Employees SQL302 Module 3 28
DELETE FROM Results
15
SQL Server/Employees SQL302 Module 3 29
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
SQL Server/Employees SQL302 Module 3 30
SQL302
SQL Programming
Part 2– Managing Database Structures
16
Managing Database Structures
• Create
• Alter
• Synonyms
• Views
SQL Server/Employees SQL302 Module 3 31
Managing Database Structures
• We will first create a table and then use thealter command to
– Add columns
– Modify columns
– Add constraints
SQL Server/Employees SQL302 Module 3 32
17
SQL Server/Employees SQL302 Module 3 33
Creating Tables
• Use create statement
• Specify:
– Columns with data types and columnconstraints
– Table constraints
• Foreign key references
• Primary key designation
SQL Server/Employees SQL302 Module 3 34
Create Table
Basic syntax:
Create table <table-name>
(
<column1> <datatype> <constraints>
,.. <column1> <datatype> <constraints>
…
<table constraints>
);
18
SQL Server/Employees SQL302 Module 3 35
Create Table
Example 1: SQL202 Create summary table(we will do this step by step using the alterstatement in succeeding slides)
Create table summary(
isbn varchar(20) primary key,
How_many int check (how_many >= 0),
Constraint isbn_fk
Foreign key (isbn) references books(isbn)
)
SQL Server/Employees SQL302 Module 3 36
Create Table
Example 2: Create summary table
Create table #summary(
isbn varchar(20) primary key);
19
SQL Server/Employees SQL302 Module 3 37
Modifying a Table Design
• Applies to tables
• Use ALTER statement
– Add columns
– Delete columns
– Rename columns
– Add column constraints
– Add table constraints
SQL Server/Employees SQL302 Module 3 38
Modifying a Table Design
Basic syntax:
Alter <table-name>
Add <field-name>,
Add <table-constraint>,
Alter <field-name>
Etc.
20
SQL Server/Employees SQL302 Module 3 39
Add a column
Example: add a column to hold howmany times a book has been sold
alter table #summary
Add how_many numeric(5,2);
Modify a column
• Example
– Change the how many column to an integer
• Code
alter table #summary
alter column how_many int;
SQL Server/Employees SQL302 Module 3 40
21
New column results
SQL Server/Employees SQL302 Module 3 41
SQL Server/Employees SQL302 Module 3 42
Column Constraints
• Primary key
• Not NULL
• CHECK clause
• Default
• Unique
22
SQL Server/Employees SQL302 Module 3 43
Add a column constraint
Example: modify the how many columnso there is a check constraint onhow_many
alter table #summary
add constraint check(how_many >=0);
SQL Server/Employees SQL302 Module 3 44
Table Constraints
• Primary Key
• Foreign Key
• Compare fields against each other. I.e.ship_date >= order_date
23
SQL Server/Employees SQL302 Module 3 45
Add a table constraint
Example: add a foreign key constraintwith cascade options
SQL Server/Employees SQL302 Module 3 46
Constraints on Summary Table
24
Synonyms
• Can create another name for an object
– Used to provide a shorthand way to refer along, fully qualified table name
– Used where we want scripts to use an oldname as well as the newer name
• Syntax:
– Create <synonym> for <objectname>
SQL Server/Employees SQL302 Module 3 47
Synonyms
• Example:
– create a synonym for the orderlines table
• Code
create synonym orderlines fororder_lines;
SQL Server/Employees SQL302 Module 3 48
25
SQL Server/Employees SQL302 Module 3 49
Views
• Think of a view as a named query whereinthe definition is stored in the database
• Can be read like a table
• Some are updateable
SQL Server/Employees SQL302 Module 3 50
Views
Basic syntax:
Create view <view-name> (<column-list>)
As
<select statement>
Column list can be used to name the output columnsinstead of using the names in the select list
26
SQL Server/Employees SQL302 Module 3 51
Creating a View
SQL Server/Employees SQL302 Module 3 52
Using Views
• Can be used like a table subject to variouslimitations
– Cannot insert into grouped queries, etc.
– Etc.
• Sample select from syntax:
select column-list
from employee_view
27
Inserting into a view
• Must insert into all required columns inunderlying table
• Sample code:
SQL Server/Employees SQL302 Module 3 53
SQL Server/Employees SQL302 Module 3 54
Using a View
28
SQL302
SQL Server/Employees SQL302 Module 3 55
[end of course]
Notes
SQL Server/Employees SQL302 Module 3 56
Accelerated Computer Training for Working Professionals
Orange Coast Database Associates Course Material (800)355-9855 or http://www.dhdursoassociates.com
Orange CoastDatabase Associates
Specializing in Microsoft Office,
Access, SQL, and related technologiesComputer Training, Programming & Consulting