Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 22 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
22
Dung lượng
908,41 KB
Nội dung
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. [...]... 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 Ver 1.0 Session 10 Slide 28 of 31 Summarize the session 10. 18 Instructor Inputs NIIT Slide 29 Querying and Managing Data Using SQL Server 2005 Summary (Contd.) A full-text index can be created... CATCH block? Answer: ERROR_MESSAGE() Ver 1.0 Session 10 Slide 26 of 31 Reiterate the concepts taught in the preceding slides by asking the question NIIT Instructor Inputs 10. 17 Slide 27 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 Ver 1.0 Session 10 Slide 27 of 31 Reiterate the concepts taught... such a data search? Ver 1.0 Session 10 Slide 15 of 31 At the end of this demo, the students will be able to implement full-text search in a database NIIT Instructor Inputs 10. 11 Slide 16 Querying and Managing Data Using SQL Server 2005 Demo: Implementing Full-Text Search (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1 Enable the Full-text search on the AdventureWorks... batches are: TRY-CATCH construct RAISERROR statement Ver 1.0 10. 20 Instructor Inputs Session 10 Slide 31 of 31 NIIT FAQs 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... which gets executed when an error occurs Let’s see how… Ver 1.0 Session 10 Slide 24 of 31 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 10. 16 Instructor Inputs NIIT Slide 25 Querying and Managing Data Using SQL Server... execution of statements with the following control-of-flow statements: IF…ELSE statement CASE statement WHILE statement Ver 1.0 Session 10 Slide 19 of 31 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 NIIT Instructor Inputs 10. 13 Slide 20 Querying and Managing Data Using... 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 Ver 1.0 NIIT Session 10 Slide 30 of 31 Instructor Inputs 10. 19 Slide 31 Querying and Managing Data Using SQL Server 2005 Summary (Contd.) The CONTINUE statement causes... Ver 1.0 Session 10 Slide 22 of 31 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 NIIT Instructor Inputs 10. 15 Slide 23 Querying and Managing Data Using SQL Server 2005 Handling Errors and Exceptions Errors in SQL Server can be handled on two levels: Using the TRY-CATCH construct... 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 NIIT Instructor Inputs 10. 21 10. 22 Instructor Inputs NIIT ... can use the examples given in the Student Guide to demonstrate how to create batches 10. 12 Instructor Inputs NIIT Slide 18 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 Session 10 Ver 1.0 Slide 18 of 31 Reiterate the concepts taught in the preceding slides by asking . Instructor Inputs Session 10 ¤NIIT Instructor Inputs 10. 3 This session includes sections two and three of Chapter 6 and section. to enable a full-text search. 10. 10 Instructor Inputs ¤NIIT Slide 12 Slide 12 of 3 1Session 10 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 To search using full-text index, use. 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 3 1Session 10 Ver. 1.0 Querying