• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
SQL302 Intermediate SQL Workshop 3
 

SQL302 Intermediate SQL Workshop 3

on

  • 339 views

Third module of SQL302 intermediate SQL course. Covers using subqueries in updates and deletes, update from and delete from, views, and altering tables.

Third module of SQL302 intermediate SQL course. Covers using subqueries in updates and deletes, update from and delete from, views, and altering tables.

Statistics

Views

Total Views
339
Views on SlideShare
339
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    SQL302 Intermediate SQL Workshop 3 SQL302 Intermediate SQL Workshop 3 Presentation Transcript

    • 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 DatabaseSQL Server/Employees SQL302 Module 3 1
    • 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
    • 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 OrderlinesSQL Server/Employees SQL302 Module 3 3
    • 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
    • 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.comSQL Server/Employees SQL302 Module 3 5
    • SQL302 Module 3• Part 1 – Modifying Data• Part 2 – Managing Database StructuresSQL Server/Employees SQL302 Module 3 6
    • SQL302 SQL Programming Part 1 – Modifying DataSQL Server/Employees SQL302 Module 3 7
    • Relational Database with constraints (from text)SQL Server/Employees SQL302 Module 3 8
    • Sample Employees DatabaseBookstore2 & SQL204 Module 1 9Employees
    • 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 consistentSQL Server/Employees POS/410 Workshop 3 10
    • SQL Server Transactions Syntax begin transaction Statements … … if @@error = 0 Commit transaction else Rollback errorSQL Server/Employees POS/410 Workshop 3 11
    • 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 userSQL Server/Employees SQL302 Module 3 12
    • Data Modification Statements • This course (SQL302) – Select Into – Table Valued Constructors (TVCs) – Update – Delete • Basic DML already covered in SQL202SQL Server/Employees SQL302 Module 3 13
    • 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
    • Select into• Example: – Copy a subset of the employees table to a temporary working table• Code:SQL Server/Employees SQL302 Module 3 15
    • 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 classSQL Server/Employees SQL302 Module 3 16
    • Insert with TVCSQL Server/Employees SQL302 Module 3 17
    • 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
    • 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
    • UPDATE w/ Subquery in where• Example: Increase range minimums for unused job codesSQL Server/Employees SQL302 Module 3 20
    • UPDATE ResultsSQL Server/Employees SQL302 Module 3 21
    • 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
    • Update from• Example: increase managerial salariesSQL Server/Employees SQL302 Module 3 23
    • UPDATE From ResultsSQL Server/Employees SQL302 Module 3 24
    • Update w/ subquery in set clause• Update employee temp table – set salaries to the average for that codeSQL Server/Employees SQL302 Module 3 25
    • 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
    • Delete from• Example: Delete unused job titlesSQL Server/Employees SQL302 Module 3 27
    • DELETE FROM ResultsSQL Server/Employees SQL302 Module 3 28
    • 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 classSQL Server/Employees SQL302 Module 3 29
    • SQL302 SQL Programming Part 2– Managing Database StructuresSQL Server/Employees SQL302 Module 3 30
    • Managing Database Structures• Create• Alter• Synonyms• ViewsSQL Server/Employees SQL302 Module 3 31
    • Managing Database Structures• We will first create a table and then use the alter command to – Add columns – Modify columns – Add constraintsSQL Server/Employees SQL302 Module 3 32
    • Creating Tables• Use create statement• Specify: – Columns with data types and column constraints – Table constraints • Foreign key references • Primary key designationSQL Server/Employees SQL302 Module 3 33
    • Create Table Basic syntax: Create table <table-name> ( <column1> <datatype> <constraints> ,.. <column1> <datatype> <constraints> … <table constraints> );SQL Server/Employees SQL302 Module 3 34
    • 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
    • Create Table Example 2: Create summary table Create table #summary( isbn varchar(20) primary key);SQL Server/Employees SQL302 Module 3 36
    • Modifying a Table Design• Applies to tables• Use ALTER statement – Add columns – Delete columns – Rename columns – Add column constraints – Add table constraintsSQL Server/Employees SQL302 Module 3 37
    • 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
    • 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
    • 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
    • New column resultsSQL Server/Employees SQL302 Module 3 41
    • Column Constraints• Primary key• Not NULL• CHECK clause• Default• UniqueSQL Server/Employees SQL302 Module 3 42
    • 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
    • Table Constraints• Primary Key• Foreign Key• Compare fields against each other. I.e. ship_date >= order_dateSQL Server/Employees SQL302 Module 3 44
    • Add a table constraint Example: add a foreign key constraint with cascade optionsSQL Server/Employees SQL302 Module 3 45
    • Constraints on Summary TableSQL Server/Employees SQL302 Module 3 46
    • 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
    • Synonyms• Example: – create a synonym for the orderlines table• Code create synonym orderlines for order_lines;SQL Server/Employees SQL302 Module 3 48
    • 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 updateableSQL Server/Employees SQL302 Module 3 49
    • 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 listSQL Server/Employees SQL302 Module 3 50
    • Creating a ViewSQL Server/Employees SQL302 Module 3 51
    • 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_viewSQL Server/Employees SQL302 Module 3 52
    • Inserting into a view• Must insert into all required columns in underlying table• Sample code:SQL Server/Employees SQL302 Module 3 53
    • Using a ViewSQL Server/Employees SQL302 Module 3 54
    • SQL302 [end of course]SQL Server/Employees SQL302 Module 3 55
    • NotesSQL Server/Employees SQL302 Module 3 56