Third module of SQL302 intermediate SQL course. Covers using subqueries in updates and deletes, update from and delete from, views, and altering tables.
1. SQL302
Intermediate SQL Programming
Based on SQL Clearly Explained by Jan Harrington and Microsoft SQL Server T-
SQL Fundamentals by Itzki Ben-gan
Workshop 3 – Modifying Data, Managing the
Database
SQL Server/Employees SQL302 Module 3 1
2. Note on SQL302 Slides
• These slides were originally designed to support the
single SQL302 course which was used for any of
MS Access, Oracle and SQL Server.
• As such you may see here slides developed in any
one of the above products.
• We are in the process of migrating the Oracle
MySQL, and MS Access slides out into their own
slide sets. These SQL302 slides will focus on
Microsoft SQL Server.
SQL Server/Employees SQL302 Module 3 2
3. Warning!
• Below are some table name changes to be
aware of in doing queries. We have created
synonyms so either name should work.
New Name Old Name
Orders Order_filled
Order_Lines Orderlines
SQL Server/Employees SQL302 Module 3 3
4. SQL302 Contact Information
P.O. Box 6142
Laguna Niguel, CA 92607
949-489-1472
http://www.d2associates.com
slides.1@dhdursoassociates.com
Copyright 2001-2011. All rights reserved.
SQL Server/Employees SQL302 Module 3 4
5. SQL302 Resources
• Bookstore database scripts found on
box.net at
http://tinyurl.com/SQLScripts
• Slides can be viewed on SlideShare…
http://www.slideshare.net/OCDatabases
• Follow up questions?
sql.support@dhdursoassociates.com
SQL Server/Employees SQL302 Module 3 5
6. SQL302 Module 3
• Part 1 – Modifying Data
• Part 2 – Managing Database Structures
SQL Server/Employees SQL302 Module 3 6
7. SQL302
SQL Programming
Part 1 – Modifying Data
SQL Server/Employees SQL302 Module 3 7
10. Transactions
• Statements can be grouped
into transactions
• All must succeed
• If so they are all
committed
• If not they are all rolled
back
• Helps guarantee data is
consistent
SQL Server/Employees POS/410 Workshop 3 10
11. SQL Server Transactions Syntax
begin transaction
Statements
…
…
if @@error = 0
Commit transaction
else
Rollback error
SQL Server/Employees POS/410 Workshop 3 11
12. Data Modification Statements
• End-user rarely sees these
statements
• Application developer prepares
these statements “behind the
scenes” based on forms or web
pages filled out by user
SQL Server/Employees SQL302 Module 3 12
13. Data Modification Statements
• This course (SQL302)
– Select Into
– Table Valued Constructors (TVCs)
– Update
– Delete
• Basic DML already covered in
SQL202
SQL Server/Employees SQL302 Module 3 13
14. Select into
• Select….into will copy a table
• Uses a regular SQL statement with into
clause before the from not after
• Syntax:
Select <select list>
Into <tablename>
From <tablename>
SQL Server/Employees SQL302 Module 3 14
15. Select into
• Example:
– Copy a subset of the employees table to a
temporary working table
• Code:
SQL Server/Employees SQL302 Module 3 15
16. Insert with TVC
• Newer form of insert statement long
available in MySQL
• Can shorten your code
• Each element in the insert list can actually
be an expression but we will use literals in
this class
SQL Server/Employees SQL302 Module 3 16
18. Update and Delete
• Updates and deletes can use
subqueries
• SQL Server allows update and
delete statements to be based on
joins, too
• We will review this in the next few
slides.
SQL Server/Employees SQL302 Module 3 18
19. Update
• You can use subqueries in an update.
– In where predicate
– To set the field value
UPDATE table
SET columnname = value from subquery
[WHERE condition with subquery]
SQL Server/Employees SQL302 Module 3 19
20. UPDATE w/ Subquery in where
• Example: Increase range minimums for unused
job codes
SQL Server/Employees SQL302 Module 3 20
22. UPDATE FROM Statement
SQL Server allows updates from a table, including joined
tables. As an example one could update quantity on hand
in an inventory table from a transaction table.
UPDATE table
SET columnname = value…
[FROM tablelist]
[WHERE condition]
SQL Server/Employees SQL302 Module 3 22
25. Update w/ subquery in set clause
• Update employee temp table – set salaries to the
average for that code
SQL Server/Employees SQL302 Module 3 25
26. DELETE FROM Statement
SQL Server allows deletes from a table based on a
subquery or a join condition.
Basic syntax:
DELETE [from] table
FROM tablelist
[WHERE condition]
SQL Server/Employees SQL302 Module 3 26
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 when
creating or modifying table structures which we
will look at later in the class
SQL Server/Employees SQL302 Module 3 29
32. Managing Database Structures
• We will first create a table and then use the
alter command to
– Add columns
– Modify columns
– Add constraints
SQL Server/Employees SQL302 Module 3 32
33. Creating Tables
• Use create statement
• Specify:
– Columns with data types and column
constraints
– Table constraints
• Foreign key references
• Primary key designation
SQL Server/Employees SQL302 Module 3 33
35. Create Table
Example 1: SQL202 Create summary table
(we will do this step by step using the alter
statement 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 35
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 37
38. Modifying a Table Design
Basic syntax:
Alter <table-name>
Add <field-name>,
Add <table-constraint>,
Alter <field-name>
Etc.
SQL Server/Employees SQL302 Module 3 38
39. Add a column
Example: add a column to hold how
many times a book has been sold
alter table #summary
Add how_many
numeric(5,2);
SQL Server/Employees SQL302 Module 3 39
40. 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
43. Add a column constraint
Example: modify the how many column
so there is a check constraint on
how_many
alter table #summary
add constraint check
(how_many >=0);
SQL Server/Employees SQL302 Module 3 43
44. Table Constraints
• Primary Key
• Foreign Key
• Compare fields against each other. I.e.
ship_date >= order_date
SQL Server/Employees SQL302 Module 3 44
45. Add a table constraint
Example: add a foreign key constraint
with cascade options
SQL Server/Employees SQL302 Module 3 45
47. Synonyms
• Can create another name for an object
– Used to provide a shorthand way to refer a
long, fully qualified table name
– Used where we want scripts to use an old
name as well as the newer name
• Syntax:
– Create <synonym> for <objectname>
SQL Server/Employees SQL302 Module 3 47
48. Synonyms
• Example:
– create a synonym for the orderlines table
• Code
create synonym orderlines for
order_lines;
SQL Server/Employees SQL302 Module 3 48
49. Views
• Think of a view as a named query wherein
the definition is stored in the database
• Can be read like a table
• Some are updateable
SQL Server/Employees SQL302 Module 3 49
50. Views
Basic syntax:
Create view <view-name> (<column-list>)
As
<select statement>
Column list can be used to name the output columns
instead of using the names in the select list
SQL Server/Employees SQL302 Module 3 50
52. Using Views
• Can be used like a table subject to various
limitations
– Cannot insert into grouped queries, etc.
– Etc.
• Sample select from syntax:
select column-list
from employee_view
SQL Server/Employees SQL302 Module 3 52
53. Inserting into a view
• Must insert into all required columns in
underlying table
• Sample code:
SQL Server/Employees SQL302 Module 3 53