1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives Begin the sess
Trang 1Instructor Inputs S e
Trang 3This session includes sections two and three of Chapter 6 and section one of Chapter 7 of
the Student Guide
Slide 1
Slide 1 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
In this session, you will learn to:
Create and manage views Implement a full-text search Implement batches
Objectives
Begin the session by sharing the objectives with the students
Slide 2
Slide 2 of 31 Session 10
Created by using CREATE VIEW statement
Syntax:
CREATE VIEW view_name [(column_name [, column_name] )]
[WITH ENCRYPTION] [, SCHEMABINDING]]
AS select_statement [WITH CHECK OPTION]
Let’s see how…
Creating Views
Trang 4that views are used for two reasons, to simplify complex queries for users and to restrict users from viewing data directly from the table
While describing views to the students, you can use the example of the small window on your classroom door A person peeping through the window will only be able to see only
a small portion of the room and will think that the visible portion is the entire classroom Similarly, for a view which displays only four columns of a table, the user will perceive that the table contains only four columns
You need to emphasize that views cannot store data by themselves, they obtain the data from the base tables A view is nothing but a query stored as an object Use the examples given in the Student Guide to demonstrate how to create the view
Explain to the students that it is a good practice to follow the naming conventions while creating views Prefix the name of the view by using ‘vw’
If you want to restrict users from seeing the definition of the view by using the
sp_helptext procedure, you can encrypt the definition by using the WITH ENCRYPTION option in the CREATE VIEW statement
Slide 3
Slide 3 of 31 Session 10
Improves query performance
Is performed by creating an unique clustered index on a view and afterwards nonclustered index can also be created
Is performed by using CREATE INDEX statement
Let’s see how…
Indexing Views
In this slide, you need to explain the concept, importance, and benefit of indexing the views Use the examples given in the Student Guide to demonstrate how to create indexed views
Trang 5Slide 4 of 31 Session 10
Ver 1.0
Just a minute
In which of the following conditions will you NOT create an indexed view:
1 When the data is large
2 When the data is regularly updated
3 When you need to improve the performance of the view
Answer:
2 When the data is regularly updated
Reiterate the concepts taught in the preceding slides by asking the question
Slide 5
Slide 5 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Managing a view involves altering, dropping, or renaming.
Altering a view involves modifying a view without dropping it.
Syntax:
ALTER VIEW view_name [(column_name)]
WITH ENCRYPTION]
AS select_statement WITH CHECK OPTION]
Dropping a view involves deleting the view when it is no longer required.
Syntax:
DROP VIEW view_name
Managing Views
In this slide, you need to explain how to manage views This involves altering the
definition and dropping a view
Use the examples given in the Student Guide to demonstrate how to manage views
Trang 6Slide 6 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Renaming the name of the view without deleting it.
Syntax:
sp_rename old_viewname, new_viewname
Let’s see how…
Managing Views (Contd.)
In this slide, you will explain how to rename a view A view can be renamed by using the
sp_rename system stored procedure
The syntax of the sp_rename procedure is:
sp_rename old_viewname, new_viewname
Trang 7Slide 7 of 31 Session 10
Ver 1.0
Problem Statement:
You are a database developer at AdventureWorks, Inc You need to frequently generate a report containing the following details of the employees:
Employee ID Employee First Name Employee Last Name Title
Manager First Name Manager Last Name
Demo: Creating Views
At the end of this demo, students will be able to create views
Slide 8
Slide 8 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Problem Statement (Contd.):
To retrieve this data, you need to execute the following statement on the database:
SELECT e1.EmployeeID, c1.FirstName, c1.LastName, e1.Title,
c2.FirstName AS [Manager First Name],c2.LastName AS [Manager Last Name]
FROM HumanResources.Employee e1 INNER JOIN Person.Contact c1
ON e1.ContactID = c1.ContactID INNER JOIN HumanResources.Employee AS e2
ON e1.ManagerID = e2.EmployeeID INNER JOIN Person.Contact AS c2
ON e2.ContactID = c2.ContactID
Demo: Creating Views (Contd.)
Trang 8Slide 9 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Problem Statement (Contd.):
As a database developer, you need to simplify the execution of this query so that you do not need to send such a large query
to the database engine every time the report is required.
Demo: Creating Views (Contd.)
Slide 10
Slide 10 of 31 Session 10
2 Verify the simplification of the query execution.
Demo: Creating Views (Contd.)
Trang 9Note
Slide 11 of 31 Session 10
Ver 1.0
Full-Text Search:
Allows you to perform complex search on the data Allows you to search for synonyms or antonyms of a particular word
Feature is disabled by default
To retrieve the required details by using full-text, you need
to perform the following tasks:
1 Enable the full-text search in the database.
2 Create a full-text catalog.
3 Create a unique index.
4 Create a full-text index.
5 Populate the full-text index.
Let’s see how…
Configuring Full-Text Search
In this topic, you need to explain the concept of full-text search to the students To clarify
the concept of full-text search, you can use the example of a Web-based search engine
where you need to allow flexible data search where the users might need to search for a
set of words, synonyms, or antonyms Tell the students that if they want to include the
functionalities similar to the functionality provided by a search engine, such as Google,
you can implement full-text in your database
After you have explained the concepts, you need to explain full-text catalogs and full-text
indexes Use the examples given in the Student Guide to demonstrate how to enable
full-text search
There are certain words that are used very often and may hinder a query These words are
called noise words and are excluded from your search string For example, if your search
string is “Who is the governor of California”, the full-text search will not look for the
words, such as ‘is’ and ‘the’ Some noise words include a, an, the, and are
Full-text indexes can be created on the base tables but not on the views or the system
tables
You need to have DBO rights to enable a full-text search
Trang 10Slide 12 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
To search using full-text index, use the following full-text predicates:
FREETEXT: Searches for any variation of a word or a group of words given in the search column.
CONTAINS: Searches for a specific phrase, for the proximity
of words within a text or for the exact match.
Let’s see how…
Searching Data by Using a Full-Text Search
In this topic, you need to explain the concepts of various full-text predicates to the students In addition, you need to explain the difference between the two predicates You can use the examples given in the Student Guide to demonstrate how to use full-text predicates
Slide 13
Slide 13 of 31 Session 10
2 Change tracking based population
3 Incremental timestamp based population
Reiterate the concepts taught in the preceding slides by asking the question
Trang 11Slide 14 of 31 Session 10
‘drive’ in the AddressLine1 column Similarly, they might need
to search for the locations that contain the words ‘Santa’ and
‘Street’ in the AddressLine1 column.
How will you enable the users to perform such a data search?
Demo: Implementing Full-Text Search
At the end of this demo, the students will be able to implement full-text search in a database
Trang 12Slide 16 of 31 Session 10
1 Enable the Full-text search on the AdventureWorks database.
2 Create a default full-text catalog.
3 Create a full-text index.
4 Search the data by using the CONTAINS predicate.
Demo: Implementing Full-Text Search (Contd.)
Slide 17
Slide 17 of 31 Session 10
Uses PRINT statement to display user-defined messages and values of variables
Let’s see how…
Creating Batches
In this topic, you need to explain the concept of batches in SQL Server Mention that batches help in reducing the network traffic as in a batch multiple commands are
submitted and executed together thereby reducing the
Demonstrate a set of INSERT statements or SELECT statements as a batch You can use the examples given in the Student Guide to demonstrate how to create batches
Trang 13Slide 18 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Allow batches to perform conditional execution of statements with the following control-of-flow statements:
IF…ELSE statement CASE statement WHILE statement
Using Constructs
In this topic, explain the syntax and usage of different kinds of constructs in batches Tell that these constructs help in implementing programming logic when executing SQL statements
Trang 14Slide 20 of 31 Session 10
Let’s see how…
Using Constructs (Contd.)
In this topic, you need to explain IF ELSE construct to the students Use the examples given in the Student Guide to how to use IF ELSE construct
Tell your students that when more than one statement needs to be executed, the statements needs to be put within the BEGIN and END block, else only the first line of the SQL block gets executed
Trang 15Slide 21 of 31 Session 10
[ ]]
[ELSE expression]
END
Let’s see how…
Using Constructs (Contd.)
In this slide, you need to explain the CASE construct to the students Use the examples given in the Student Guide to demonstrate the use of CASE construct
Slide 22
Slide 22 of 31 Session 10
Uses BREAK and CONTINUE statements to break the loop or
to continue the loop Syntax:
WHILE boolean_expression {sql_statement | statement_block}
[BREAK]
{sql_statement | statement_block}
[CONTINUE]
Let’s see how…
Using Constructs (Contd.)
In this slide, you need to explain the concept of WHILE construct to the students You can use the examples given in the Student Guide to demonstrate the use of WHILE construct
Trang 16Slide 23 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
Errors in SQL Server can be handled on two levels:
Using the TRY-CATCH construct Using the RAISERROR statement
Handling Errors and Exceptions
In this slide, you need to explain your students about errors Use the examples given in the Student Guide to demonstrate how to handle errors and exceptions
Slide 24
Slide 24 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
TRY-CATCH construct is used in the following way:
Try block encloses a group of T-SQL statements If any error occurs in the statements of TRY block, control passes to the CATCH block.
CATCH block encloses another group of statements, which gets executed when an error occurs.
Let’s see how…
Handling Errors and Exceptions (Contd.)
In this slide, you will explain the concept of TRY-CATCH block Tell your students that this type of handling is known as structured error handling Use the examples given in the Student Guide to demonstrate the use of TRY-CATCH block
Trang 17Slide 25 of 31 Session 10
Ver 1.0
RAISERROR:
Is used to return messages back to the called applications Uses the same message format as a system error or warning message generated by the SQL Server Database Engine Can also return user-defined error messages
Let’s see how…
Handling Errors and Exceptions (Contd.)
In this slide, you need to explain the concept of RAISERROR to the students Use the examples given in the Student Guide to demonstrate the usage of RAISERROR statement Slide 26
Slide 26 of 31 Session 10
Trang 18Slide 27 of 31 Session 10
Using the RAISERROR statement
Reiterate the concepts taught in the preceding slides by asking the question
Slide 28
Slide 28 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
In this session, you learned that:
A view is a virtual table, which derives its data from one or more tables known as the base or underlying tables
Views serve as security mechanisms, thereby protecting data
in the base tables
The SQL Server allows data to be modified only in one of the underlying tables when using views, even if the view is derived from multiple underlying tables.
The SQL Server enables users to search for a wide range of text in the SQL tables through a full-text query feature
You can enable the full-text search by using the command, SP_FULLTEXT_DATABASE ENABLE.
A full-text catalog can be created by using the CREATE FULLTEXT CATALOG statement.
Summary
Summarize the session
Trang 19Slide 29 of 31 Session 10
Full-text predicates that can be used to perform full-text search are CONTAINS and FREETEXT.
A FREETEXT predicate searches for any variation of a word or
a group of words given in the search column.
The CONTAINS predicate searches for a specific phrase or for the exact match.
A batch is a set of SQL statements submitted together to the server for execution.
You can use a variable to store a temporary value.
Summary (Contd.)
Slide 30
Slide 30 of 31 Session 10
Ver 1.0
Querying and Managing Data Using SQL Server 2005
You can use the PRINT statement to display a user-defined message or the content of a variable on the screen.
You can use the comment entries in batches to write a description of the code.
You can use the IF…ELSE statement for conditional execution
The BREAK statement causes an exit from the WHILE loop.
Summary (Contd.)
Trang 20Slide 31 of 31 Session 10
Two ways to handle errors in batches are:
TRY-CATCH construct RAISERROR statement
Trang 211 In a CATCH block how do you get to know an error?
Ans: @@ERROR system function returns 0 if the last T-SQL statement executed successfully If the statement generated an error, @@ERROR returns the error number The value of @@ERROR changes on the completion of each T-SQL statement
2 What is a partitioned view?
Ans: A partitioned view joins horizontally partitioned data from a set of member tables across one or more servers This makes the data appear as if from one table A view that joins member tables on the same instance of SQL Server is a local partitioned view