Tải bản đầy đủ (.pdf) (22 trang)

Instructor Inputs - Session 10 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (908.41 KB, 22 trang )

Instructor Inputs
Session 10

¤NIIT Instructor Inputs 10.3
This session includes sections two and three of Chapter 6 and section one of Chapter 7 of
the Student Guide.
Slide 1
Slide 1 of 31Session 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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
A View is:
Used to view data from tables
Similar to creating tables but it does not contain any data as it
derives its data from the underlying tables
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
Session Overview
10.4 Instructor Inputs ¤NIIT
In this slide, you will explain the concept of views to the students. Explain to the students
that 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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Indexing of Views:
Is done when the volume of data in the underlying table is
large and not updated frequently
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.
¤NIIT Instructor Inputs 10.5
Slide 4
Slide 4 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
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 31Session 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.
10.6 Instructor Inputs ¤NIIT
Slide 6
Slide 6 of 31Session 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
¤NIIT Instructor Inputs 10.7
Slide 7
Slide 7 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
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 31Session 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.)

10.8 Instructor Inputs ¤NIIT
Slide 9
Slide 9 of 31Session 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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Solution:
To solve the preceding problem, you need to perform the
following tasks:
1. Create a view.
2. Verify the simplification of the query execution.
Demo: Creating Views (Contd.)
¤NIIT Instructor Inputs 10.9
Note
Slide 11
Slide 11 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
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.
10.10 Instructor Inputs ¤NIIT
Slide 12
Slide 12 of 31Session 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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Just a minute
List the types of full-text index population methods.
Answer:
The three types of full-text index population methods
are:
1. Full population
2. Change tracking based population
3. Incremental timestamp based population
Reiterate the concepts taught in the preceding slides by asking the question.
¤NIIT Instructor Inputs 10.11

Slide 14
Slide 14 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Just a minute
Which predicate is used to search for a specific phrase or
for an exact match?
Answer:
CONTAINS
Reiterate the concepts taught in the preceding slides by asking the question.
Slide 15
Slide 15 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Problem Statement:
The users at AdventureWorks, Inc. need to frequently search
for employees, customers, or vendors based on the location.
The location details of all these entities are stored in the
Address table in the Person schema. The users want to search
for addresses with different combinations of the words
specified in the search criteria. For example, they need to
search for the locations that contain the words ‘Santa’ and
‘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.
10.12 Instructor Inputs ¤NIIT

Slide 16
Slide 16 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Solution:
To solve the preceding problem, you need to perform the
following tasks:
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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Batch:
Is created to send a group of SQL statements together to SQL
Server for execution
Compiles the statements as a single executable unit called an
execution plan
Uses GO command at the end to send the SQL statements to
the instance of SQL Server
Uses variables to store values. They are:
Local variables
Global variables
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.
¤NIIT Instructor Inputs 10.13
Slide 18
Slide 18 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Just a minute
Which of the following statements can be used within a
batch?
1. CREATE FUNCTION
2. CREATE RULE
3. DECLARE
Answer:
3. DECLARE
Reiterate the concepts taught in the preceding slides by asking the question.
Slide 19
Slide 19 of 31Session 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.
10.14 Instructor Inputs ¤NIIT
Slide 20
Slide 20 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
IF…ELSE statement:
Performs a particular action based on the result of the boolean
expression
Syntax:
IF boolean_expression {sql_statement |
statement_block} [ELSE boolean_expression
{sql_statement | statement_block}]
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.
¤NIIT Instructor Inputs 10.15
Slide 21
Slide 21 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
CASE statement:
Evaluates a list of conditions and returns one of the various
possible results
Syntax:

CASE
WHEN boolean_expression THEN expression
[[WHEN boolean_expression THEN expression]
[ ]]
[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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
WHILE statement:
Executes a batch repeatedly as long as the given condition
holds true
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.

10.16 Instructor Inputs ¤NIIT
Slide 23
Slide 23 of 31Session 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 31Session 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.
¤NIIT Instructor Inputs 10.17
Slide 25
Slide 25 of 31Session 10
Ver. 1.0

Querying and Managing Data Using SQL Server 2005
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 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Just a minute
Which system function returns the text of the error message
when used in the CATCH block?
Answer:
ERROR_MESSAGE()
Reiterate the concepts taught in the preceding slides by asking the question.
10.18 Instructor Inputs ¤NIIT
Slide 27
Slide 27 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Just a minute
How can you return a user-defined error message in a
batch?
Answer:
Using the RAISERROR statement

Reiterate the concepts taught in the preceding slides by asking the question.
Slide 28
Slide 28 of 31Session 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.
¤NIIT Instructor Inputs 10.19
Slide 29
Slide 29 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
A full-text index can be created by using the CREATE
FULLTEXT INDEX statement.
Three types of full-text population methods are full population,
change tracking-based population, and incremental

timestamp-based population.
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 31Session 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
of the SQL statements.
The CASE statement evaluates a list of conditions and returns
one of the various possible results.
You can use the WHILE statement in a batch to allow a set of
T-SQL statements to execute repeatedly as long as the given
condition holds true.
The BREAK statement causes an exit from the WHILE loop.
Summary (Contd.)
10.20 Instructor Inputs ¤NIIT
Slide 31

Slide 31 of 31Session 10
Ver. 1.0
Querying and Managing Data Using SQL Server 2005
Summary (Contd.)
The CONTINUE statement causes the WHILE loop to restart,
skipping any statements after the CONTINUE statement within
the loop.
Two ways to handle errors in batches are:
TRY-CATCH construct
RAISERROR statement
¤NIIT Instructor Inputs 10.21
1. 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.
FAQs
10.22 Instructor Inputs ¤NIIT

×