Presentation is loading. Please wait.

Presentation is loading. Please wait.

In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.

Similar presentations


Presentation on theme: "In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives."— Presentation transcript:

1 In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives

2 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

3 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

4 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

5 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

6 Renaming the name of the view without deleting it. Syntax: sp_rename old_viewname, new_viewname Let’s see how… Managing Views (Contd.)

7 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

8 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.)

9 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.)

10 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.)

11 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

12 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

13 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

14 Just a minute Which predicate is used to search for a specific phrase or for an exact match? Answer: CONTAINS

15 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

16 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.)

17 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

18 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

19 Allow batches to perform conditional execution of statements with the following control-of-flow statements: IF…ELSE statement CASE statement WHILE statement Using Constructs

20 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.)

21 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.)

22 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.)

23 Errors in SQL Server can be handled on two levels: Using the TRY-CATCH construct Using the RAISERROR statement Handling Errors and Exceptions

24 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.)

25 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.)

26 Just a minute Which system function returns the text of the error message when used in the CATCH block? Answer: ERROR_MESSAGE()

27 Just a minute How can you return a user-defined error message in a batch? Answer: Using the RAISERROR statement

28 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

29 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.)

30 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.)

31 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


Download ppt "In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives."

Similar presentations


Ads by Google