sql - 2071b 09

Upload: jose-alberto

Post on 31-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 SQL - 2071B 09

    1/19

    Module 9: Introductionto Programming Objects

  • 8/14/2019 SQL - 2071B 09

    2/19

    Overview

    Displaying the Text of a ProgrammingObject

    Introduction to Views

    Advantages of Views

    Creating Views

    Introduction to Stored Procedures

    Introduction to Triggers

    Introduction to User-defined Functions

  • 8/14/2019 SQL - 2071B 09

    3/19

    Displaying the Text of aProgramming Object

    EXEC sp_helptext [@objectname = ]name

    Not Every Programming Object HasAssociated Text

    USE library

    EXEC sp_helptext 'dbo.OverdueView'GO

  • 8/14/2019 SQL - 2071B 09

    4/19

    Introduction to Views

    TitleViewTitleView

    title author

    Last of the MohicansThe Village Watch-TowerPoems

    James Fenimore CooperKate Douglas WigginWilfred Owen

    titletitle

    title_no title author synopsis

    123

    Last of the MohicansThe Village Watch-TowerPoems

    James Fenimore CooperKate Douglas WigginWilfred Owen

    ~~~~~~~~~

    Users ViewUsers View

    USE library

    GO

    CREATE VIEW dbo.TitleView

    AS

    SELECT title, author

    FROM title

    GO

  • 8/14/2019 SQL - 2071B 09

    5/19

    Advantages of Views

    Focus the Data for Users

    Focus on important or appropriate dataonly

    Limit access to sensitive data

    Mask Database Complexity

    Hide complex database design

    Simplify complex queries, includingdistributed queries to heterogeneousdata

    Simplify Management of UserPermissions

    Or anize Data for Ex ort to Other

  • 8/14/2019 SQL - 2071B 09

    6/19

    Creating Views

    Defining Views

    Restrictions on Creating Views

    Example: Viewing Information fromMultiple Tables

  • 8/14/2019 SQL - 2071B 09

    7/19

    Defining Views

    USE library

    GO

    CREATE VIEW dbo.UnpaidFinesView (Member, TotalUnpaidFines)

    AS

    SELECT member_no, (sum(fine_assessed-fine_paid))

    FROM loanhistGROUP BY member_no

    HAVING SUM(fine_assessed-fine_paid) > 0

    GO

    SELECT *

    FROM UnpaidFinesView

    GO

    Example 1: Creating a View

    Example 2: Querying a View

  • 8/14/2019 SQL - 2071B 09

    8/19

    Restrictions on Creating Views

    Can Reference a Maximum of 1024Columns

    Cannot Include COMPUTE or COMPUTE

    BY clauses

    Cannot Include ORDER BY Clause,Unless Used in Conjunction with a TOPClause

    Cannot Include the INTO Keyword

    Cannot Reference a Temporary Table

    Must Be Expressed as a Single

    Transact-SQL Batch

  • 8/14/2019 SQL - 2071B 09

    9/19

    lastnamelastname

    Thomas

    Funk

    firstnamefirstname

    Gary

    Frank

    Birth DateBirth Date

    92.01.16

    84.01.18

    member_nomember_no12

    13

    adult_noadult_no11

    6

    birth_datebirth_date1992-01-16 00:00:00.0001984-01-18 00:00:00.000

    Example: Viewing Information fromMultiple Tables

    membe

    r

    juvenile

    BirthdayView

    USE library

    GO

    CREATE VIEW dbo.birthdayview

    (lastname, firstname, birthday)

    AS

    SELECT lastname, firstname,CONVERT(char(8), birth_date, 2)

    FROM member

    INNER JOIN juvenile

    ON member.member_no = juvenile.member_no

    GO

    member_nomember_no11

    12

    13

    14

    lastnamelastnameThomas

    Thomas

    Funk

    Rudd

    firstnamefirstnameGary

    Clair

    Frank

    Clair

    middleinitialmiddleinitial~~~

    ~~~

    ~~~

    ~~~

    photographphotograph~~~

    ~~~

    ~~~

    ~~~

  • 8/14/2019 SQL - 2071B 09

    10/19

    Defining Stored Procedures

    Advantages of Using StoredProcedures

    Introduction to Stored Procedures

  • 8/14/2019 SQL - 2071B 09

    11/19

    Defining Stored Procedures

    A Stored Procedure Is a PrecompiledCollection of Transact-SQL Statements

    A Stored Procedure Encapsulates

    Repetitive Tasks Stored Procedures Can:

    Contain statements that performoperations

    Accept input parameters

    Return status value to indicate successor failure

    Return multiple output parameters

  • 8/14/2019 SQL - 2071B 09

    12/19

    Advantages of Using StoredProcedures

    Share Application Logic

    Shield Database Schema Details

    Provide Security Mechanisms Improve Performance

    Reduce Network Traffic

  • 8/14/2019 SQL - 2071B 09

    13/19

    Introduction to Triggers

    A Trigger Is a Special Type of StoredProcedure

    A Trigger Is:

    Associated with a table

    Invoked automatically

    Not called directly

    Treated as part of the transaction thatfired it

  • 8/14/2019 SQL - 2071B 09

    14/19

    Introduction to User-definedFunctions

    What Is a User-defined Function?

    Creating a User-defined Function

  • 8/14/2019 SQL - 2071B 09

    15/19

    What Is a User-defined Function?

    Scalar Functions

    Similar to a built-in function

    Returns a single data value built by a series of

    statements

    Multi-Statement Table-valued Functions

    Content like a stored procedure

    Referenced like a view

    In-line Table-valued Functions

    Similar to a view with parameters

    Returns a table as the result of single SELECTstatement

  • 8/14/2019 SQL - 2071B 09

    16/19

  • 8/14/2019 SQL - 2071B 09

    17/19

    Verify Object Definition Text with EXEC sp_helptext

    Use Views to Capture and Reuse Queries

    Use Stored Procedures to Encapsulate Complex Proced

    Use User-defined Functions to Encapsulate Expressio

    Recommended Practices

  • 8/14/2019 SQL - 2071B 09

    18/19

    Lab A: Working with Views

  • 8/14/2019 SQL - 2071B 09

    19/19

    Review

    Displaying the Text of a ProgrammingObject

    Introduction to Views

    Advantages of Views

    Creating Views

    Introduction to Stored Procedures

    Introduction to Triggers

    Introduction to User-defined Functions