sql - 2071b 07

Upload: jose-alberto

Post on 31-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 SQL - 2071B 07

    1/20

    Module 7:Modifying Data

  • 8/14/2019 SQL - 2071B 07

    2/20

    Overview

    Using Transactions

    Inserting Data

    Deleting Data Updating Data

    Performance Considerations

  • 8/14/2019 SQL - 2071B 07

    3/20

    Using Transactions

    StartingTransactions

    Explicit

    Autocommit

    Implicit

    Ending Transactions

    COMMIT statement

    ROLLBACK

    statement

    BEGIN TRANSACTION

    UPDATE savings

    . . .

    UPDATE checking

    . . .

    COMMIT TRANSACTION

  • 8/14/2019 SQL - 2071B 07

    4/20

    Inserting Data

    Inserting a Row of Data by Values

    Using the INSERTSELECT Statement

    Creating a Table Using the SELECTINTO Statement

    Inserting Partial Data

    Inserting Data by Using ColumnDefaults

  • 8/14/2019 SQL - 2071B 07

    5/20

    Inserting a Row of Data by Values

    Must Adhere to DestinationConstraints or theINSERT Transaction Fails

    Use a Column List to SpecifyDestination Columns

    Specify a Corresponding List of Values

    USE northwind

    INSERT customers

    (customerid, companyname, contactname, contacttitle

    ,address, city, region, postalcode, country, phone

    ,fax)

    VALUES ('PECOF', 'Pecos Coffee Company', 'Michael Dunn'

    ,'Owner', '1900 Oak Street', 'Vancouver', 'BC'

    ,'V3F 2K1', 'Canada', '(604) 555-3392'

    ,'(604) 555-7293')

    GO

  • 8/14/2019 SQL - 2071B 07

    6/20

    USE northwind

    INSERT customers

    SELECT substring(firstname, 1, 3)+ substring (lastname, 1, 2)

    ,lastname, firstname, title, address, city

    ,region, postalcode, country, homephone, NULL

    FROM employees

    GO

    Using the INSERTSELECTStatement

    All Rows That Satisfy the SELECTStatement Are Inserted

    Verify That the Table That ReceivesNew Row Exists

    Ensure That Data Types Are Compatible

    Determine Whether Default ValuesExist or Whether Null Values AreAllowed

  • 8/14/2019 SQL - 2071B 07

    7/20

    Creating a Table Using the SELECTINTO Statement

    Use to Create a Table and Insert Rowsinto the Tablein a Single Operation

    Create a Local or Global TemporaryTable

    Create Column Alias or SpecifyColumn Names in the Select List forNew TableUSE northwind

    SELECT productname AS products

    ,unitprice AS price

    ,(unitprice * 1.1) AS taxINTO #pricetable

    FROM products

    GO

  • 8/14/2019 SQL - 2071B 07

    8/20

    Inserting Partial Data

    USE northwind

    INSERT shippers (companyname)

    VALUES ('Fitch & Mather')

    GO

    Adding new data

    USE northwind

    SELECT *

    FROM shippers

    WHERE companyname = 'Fitch & Mather'

    GO

    Verifying new data

    shipperidshipperid

    37

    companynamecompanyname

    Fitch & Mather

    phonephone

    Null

    Allows Null Values

    Example 1

    Example 2

  • 8/14/2019 SQL - 2071B 07

    9/20

    Inserting Data by Using ColumnDefaults

    DEFAULT Keyword

    Inserts default values for specifiedcolumns

    Columns must have a default value orallow null values

    DEFAULT VALUES Keyword

    Inserts default values for all columns

    Columns must have a default value orallow null values

    USE northwind

    INSERT shippers (companyname, phone)

    VALUES ('Kenya Coffee Co.', DEFAULT)

    GO

  • 8/14/2019 SQL - 2071B 07

    10/20

    Deleting Data

    Using the DELETE Statement

    Using the TRUNCATE TABLE Statement

    Deleting Rows Based on Other Tables

  • 8/14/2019 SQL - 2071B 07

    11/20

    Using the DELETE Statement

    The DELETE statement removes one ormore rows in a table according to theWHERE clause condition, if specified

    Each Deleted Row Is Logged in theTransaction Log

    USE northwind

    DELETE orders

    WHERE DATEDIFF(MONTH, shippeddate, GETDATE()) >= 6GO

  • 8/14/2019 SQL - 2071B 07

    12/20

    USE northwind

    TRUNCATE TABLE orders

    GO

    Using the TRUNCATE TABLEStatement

    The TRUNCATE TABLE StatementDeletes All Rowsin a Table

    SQL Server Retains Table Structureand Associated Objects

    Only Deallocation of Data Pages IsLogged in the Transaction Log

  • 8/14/2019 SQL - 2071B 07

    13/20

    Deleting Rows Based on OtherTables

    Using an Additional FROM Clause

    First FROM clause indicates table tomodify

    Second FROM clause specifies restrictingcriteria for the DELETE statement

    Specifying Conditions in the WHERE

    Clause Subqueries determine which rows to

    delete

  • 8/14/2019 SQL - 2071B 07

    14/20

    Updating Data

    Updating Rows Based on Data in theTable

    Updating Rows Based on Other Tables

  • 8/14/2019 SQL - 2071B 07

    15/20

    USE northwindUPDATE products

    SET unitprice = (unitprice * 1.1)

    GO

    Updating Rows Based on Data in theTable

    WHERE Clause Specifies Rows toChange

    SET Keyword Specifies the New Data

    Input values must have compatibledata types with the columns

    Updates Do Not Occur in Rows ThatViolate Any Integrity Constraints

  • 8/14/2019 SQL - 2071B 07

    16/20

    Updating Rows Based on OtherTables

    How the UPDATE Statement Works

    Never updates the same row twice

    Requires table prefixes on ambiguouscolumn names

    Specifying Rows to Update Using Joins

    Uses the FROM clause

    Specifying Rows to Update UsingSubqueries

    Correlates the subquery with the

    updated table

  • 8/14/2019 SQL - 2071B 07

    17/20

    Performance Considerations

    All Data Modifications Occur Within aTransaction

    Data Page Allocation May Occur

    Modifying Indexed Data IncursAdditional Overhead

    Indexes Can Assist Search Criteria

  • 8/14/2019 SQL - 2071B 07

    18/20

    Recommended Practices

    Always Write a SELECT Statement That Does Not ModiData Before You Actually Modify Data

    Improve the Readability of a Result Set by Changing

    Column Names or by Using Literals

    Always Include a WHERE Clause with the DELETE anUPDATE Statements

  • 8/14/2019 SQL - 2071B 07

    19/20

    Lab A: Modifying Data

  • 8/14/2019 SQL - 2071B 07

    20/20

    Review

    Using Transactions

    Inserting Data

    Deleting Data Updating Data

    Performance Considerations