Your SlideShare is downloading. ×
0
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
08 qmds2005 session11
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

08 qmds2005 session11

268

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
268
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Start the session by sharing the objectives with the students.
  • In this slide, you will explain the concept of views to the students. Explain the students that views are used for two basic reasons – to simplify complex queries for users and to restrict users from viewing data directly from the table. While explaining views to the students you can take the example of the small window on your class room door. If somebody peeps through it he or she will only be able to see the only a small portion of the class and will think that the visible portion is the entire class room. Similarly for a view which displays only say 4 columns of a table, the user will perceive that the table contains only 4 columns. It is a good practice to follow naming conventions while creating views. Prefix the name using ‘vw’. If you want to restrict users from seeing the definition of the view using the sp_helptext command, you can encrypt the definition using the WITH ENCRYPTION option in the CREATE VIEW statement. You need to stress the fact that the views cannot store data by themselves, they get the data from the underlying base tables. A view is nothing but a query stored as an object. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: CREATE VIEW vwEmployeeDepData AS SELECT e.EmployeeID, MaritalStatus, DepartmentID FROM HumanResources.Employee e JOIN HumanResources.EmployeeDepartmentHistory d ON e.EmployeeID = d.EmployeeID Additional Input Views are slow as the query is processed each time the view is used.
  • In this slide, you need to explain the concept, importance, and benefit of indexing the views. You can use the examples given in the Student Guide to clarify the concept of indexed views to the students. Further, you can execute the following statements to explain the concept: Example CREATE UNIQUE CLUSTERED INDEX idx_vwEmployeeDepData ON HumanResources.vwEmployeeDepData (EmployeeID, DepartmentID) Before executing the above statement, you need to bind the view vwEmployeeDepData to the schema as follows: ALTER VIEW HumanResources.vwEmployeeDepData WITH SCHEMABINDING AS SELECT e.EmployeeID, MaritalStatus, DepartmentID FROM HumanResources.Employee e JOIN HumanResources.EmployeeDepartmentHistory d ON e.EmployeeID = d.EmployeeID The above statement alters the existing view, vwEmployeeDepData and binds it with the schema of the underlying tables. After that, you can create a unique clustered index on the view as shown in the above statement.
  • Reiterate the concepts taught in the preceding slides by asking the question.
  • In this slide, you need to explain how to manage views to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (Alter) ALTER VIEW vwEmployeeDepData AS SELECT e.EmployeeID, LoginID, MaritalStatus, DepartmentID FROM HumanResources.Employee e JOIN HumanResources.EmployeeDepartmentHistory d ON e.EmployeeID = d.EmployeeID Example: (Drop) DROP VIEW vwEmployeeDepData
  • In this slide, you will explain how to rename a view. You can execute the following statements to explain the concept: Example: (Rename) sp_rename vwSal, vwSalary
  • You need to ensure that after the end of this demo, the students are able create views.
  • You need to ensure that after the end of this demo, the students are able create views.
  • You need to ensure that after the end of this demo, the students are able create views.
  • In this slide, you need to explain the concept of full text search to the students. To clarify the concept of full-text search, you can give the example of a web based search engine. You can tell the students that if they want to include the functionalities similar to the functionality provided by a search engine like google, you can implement full-text in your database. Once you have explained the concepts, further you need to tell them about the full-text catalogs and full-text indexes. Stress on clarifying these concepts to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (Enable the full-text) Before using full text search feature of SQL Server, you first need to enable the database to use full-text service using the following statement: USE AdventureWorks GO SP_FULLTEXT_DATABASE ENABLE GO Example: (Create full-text catalog) The full-text catalog may have multiple full-text indexes. You can create a full-text catalog using the following command: CREATE FULLTEXT CATALOG CAT1 AS DEFAULT Example: (Create a unique index) You can create a unique index on the Production.ProductDescription table as shown below: CREATE UNIQUE INDEX IX_DESC ON Production.ProductDescription (ProductDescriptionID) Example: (Create full-text index) CREATE FULLTEXT INDEX ON PRODUCTION.PRODUCTDESCRIPTION (DESCRIPTION) KEY INDEX IX_DESC Example: (populate the full-text index) To create an empty full-text index on the ProductDescription table, you can execute the following statement: CREATE FULLTEXT INDEX ON PRODUCTION.PRODUCTDESCRIPTION (DESCRIPTION) KEY INDEX PK_ProductDescription_ProductDescriptionID WITH CHANGE_TRACKING OFF, NO POPULATION Next, to populate the index you need to execute the following statement: ALTER FULLTEXT INDEX ON Production.ProductDescription START FULL POPULATION The above statement will populate the full-text index created on the ProductDescription table. Additional Inputs: 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” , in this case full-text search will not look for the words like ‘is’ and ‘the’. Some of the noise words are 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 full-text search
  • In this slide, you need to explain the concepts of various full-text predicates to the students. In addition, you also need to explain the difference between the two predicates. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (FREETEXT) SELECT Description FROM Production.ProductDescription WHERE FREETEXT (Description, 'race winners') Example: (CONTAINS) SELECT Description FROM Production.ProductDescription WHERE CONTAINS (Description, 'Ride');
  • Reiterate the concepts taught in the preceding slides by asking the question.
  • Reiterate the concepts taught in the preceding slides by asking the question.
  • You need to ensure that after the end of this demo, the students are able to implement full-text in a database.
  • In this slide, you need to explain batches to the students. Explain the benefits of using batches clearly. Stress on the fact that batches help in reducing the network traffic. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: DECLARE @Rate int SELECT @Rate = max(Rate) FROM HumanResources.EmployeePayHistory PRINT @Rate GO
  • Reiterate the concepts taught in the preceding slides by asking the question.
  • In this slide, you need to define constructs to the students. As students have already read the constructs in previous modules, you need not to put much stress on the explanation of these.
  • In this slide, you need to need to explain IF..ELSE construct to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (IF…ELSE) DECLARE @Rate money SELECT @Rate = Rate FROM HumanResources.EmployeePayHistory WHERE EmployeeID = 23 IF @Rate < 15 PRINT 'Review required' ELSE BEGIN PRINT 'Review not required' PRINT 'your rate =' PRINT @Rate END GO 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. Remember that a block of SQL statements executed together is called a batch. Demonstrate a set of INSERT statements or SELECT statements as a batch.
  • In this slide, you need to explain the CASE construct to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (CASE) SELECT EmployeeID, 'Marital Status' = CASE MaritalStatus WHEN 'M' THEN 'Married' WHEN 'S' THEN 'Single' ELSE 'Not specified' END FROM HumanResources.Employee GO
  • 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 clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: (WHILE) WHILE (SELECT AVG(Rate)+1 from HumanResources.EmployeePayHistory) < 20 BEGIN UPDATE HumanResources.EmployeePayHistory SET Rate = Rate + 1 FROM HumanResources.EmployeePayHistory IF (Select MAX(Rate)+1 from HumanResources.EmployeePayHistory) > 127 BREAK ELSE CONTINUE END
  • In this slide, you need to explain your students about errors. You can use the examples given in the Student Guide to clarify the concept to the students.
  • 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. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example BEGIN TRY -- This will generate an error, as EmployeeID is an IDENTITY column -- Ergo, we can't specify a value for this column... INSERT INTO HumanResources.Employee(EmployeeID, Title) VALUES(1, 'Tool Designer') END TRY BEGIN CATCH SELECT 'There was an error! ' + ERROR_MESSAGE() AS ErrorMessage, ERROR_LINE() AS ErrorLine, ERROR_NUMBER() AS ErrorNumber, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState END CATCH Additional Input TRY-CATCH blocks were not in the previous versions of SQL Server.
  • In this slide, you need to explain the concept of RAISERROR to the students. You can use the examples given in the Student Guide to clarify the concept to the students. Further, you can execute the following statements to explain the concept: Example: Declare @Start datetime Declare @End datetime Declare @Date_diff int Select @Start = '1900-01-01 23:00:00.000', @End = '1900-01-02 06:00:00.000' Select @Date_diff = DATEDIFF(hh, @Start, @End) If (@Date_diff != 8) RAISERROR('Error Raised', 16, 1) ELSE BEGIN Update HumanResources.Shift Set StartTime = @Start, EndTime = @End Where ShiftID = 3 End End TRY BEGIN CATCH SELECT 'The difference between Start and End time should be 8 hours' End CATCH
  • Reiterate the concepts taught in the preceding slides by asking the question.
  • Reiterate the concepts taught in the preceding slides by asking the question.
  • You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  • You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  • You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  • Transcript

    • 1. Querying and Managing Data Using SQL Server 2005Objectives In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Ver. 1.0 Session 11 Slide 1 of 31
    • 2. Querying and Managing Data Using SQL Server 2005Creating Views 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… Ver. 1.0 Session 11 Slide 2 of 31
    • 3. Querying and Managing Data Using SQL Server 2005Indexing Views 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… Ver. 1.0 Session 11 Slide 3 of 31
    • 4. Querying and Managing Data Using SQL Server 2005Just 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 Ver. 1.0 Session 11 Slide 4 of 31
    • 5. Querying and Managing Data Using SQL Server 2005Managing Views 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 Ver. 1.0 Session 11 Slide 5 of 31
    • 6. Querying and Managing Data Using SQL Server 2005Managing Views (Contd.) Renaming the name of the view without deleting it. Syntax: sp_rename old_viewname, new_viewname Let’s see how… Ver. 1.0 Session 11 Slide 6 of 31
    • 7. Querying and Managing Data Using SQL Server 2005Demo: Creating Views 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 Ver. 1.0 Session 11 Slide 7 of 31
    • 8. Querying and Managing Data Using SQL Server 2005Demo: Creating Views (Contd.) 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 Ver. 1.0 Session 11 Slide 8 of 31
    • 9. Querying and Managing Data Using SQL Server 2005Demo: Creating Views (Contd.) 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. Ver. 1.0 Session 11 Slide 9 of 31
    • 10. Querying and Managing Data Using SQL Server 2005Demo: Creating Views (Contd.) 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. Ver. 1.0 Session 11 Slide 10 of 31
    • 11. Querying and Managing Data Using SQL Server 2005Configuring Full-Text Search 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… Ver. 1.0 Session 11 Slide 11 of 31
    • 12. Querying and Managing Data Using SQL Server 2005Searching Data by Using a Full-Text Search 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… Ver. 1.0 Session 11 Slide 12 of 31
    • 13. Querying and Managing Data Using SQL Server 2005Just 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 Ver. 1.0 Session 11 Slide 13 of 31
    • 14. Querying and Managing Data Using SQL Server 2005Just a minute Which predicate is used to search for a specific phrase or for an exact match? Answer: CONTAINS Ver. 1.0 Session 11 Slide 14 of 31
    • 15. Querying and Managing Data Using SQL Server 2005Demo: Implementing Full-Text Search 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? Ver. 1.0 Session 11 Slide 15 of 31
    • 16. Querying and Managing Data Using SQL Server 2005Demo: 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 database. 2. Create a default full-text catalog. 3. Create a full-text index. 4. Search the data by using the CONTAINS predicate. Ver. 1.0 Session 11 Slide 16 of 31
    • 17. Querying and Managing Data Using SQL Server 2005Creating Batches 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… Ver. 1.0 Session 11 Slide 17 of 31
    • 18. Querying and Managing Data Using SQL Server 2005Just a minute Which of the following statements can be used within a batch? 1. CREATE FUNCTION 2. CREATE RULE 3. DECLARE Answer: 3. DECLARE Ver. 1.0 Session 11 Slide 18 of 31
    • 19. Querying and Managing Data Using SQL Server 2005Using Constructs Allow batches to perform conditional execution of statements with the following control-of-flow statements: IF…ELSE statement CASE statement WHILE statement Ver. 1.0 Session 11 Slide 19 of 31
    • 20. Querying and Managing Data Using SQL Server 2005Using Constructs (Contd.) 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… Ver. 1.0 Session 11 Slide 20 of 31
    • 21. Querying and Managing Data Using SQL Server 2005Using Constructs (Contd.) 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… Ver. 1.0 Session 11 Slide 21 of 31
    • 22. Querying and Managing Data Using SQL Server 2005Using Constructs (Contd.) 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… Ver. 1.0 Session 11 Slide 22 of 31
    • 23. Querying and Managing Data Using SQL Server 2005Handling Errors and Exceptions Errors in SQL Server can be handled on two levels: Using the TRY-CATCH construct Using the RAISERROR statement Ver. 1.0 Session 11 Slide 23 of 31
    • 24. Querying and Managing Data Using SQL Server 2005Handling Errors and Exceptions (Contd.) 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… Ver. 1.0 Session 11 Slide 24 of 31
    • 25. Querying and Managing Data Using SQL Server 2005Handling Errors and Exceptions (Contd.) 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… Ver. 1.0 Session 11 Slide 25 of 31
    • 26. Querying and Managing Data Using SQL Server 2005Just a minute Which system function returns the text of the error message when used in the CATCH block? Answer: ERROR_MESSAGE() Ver. 1.0 Session 11 Slide 26 of 31
    • 27. Querying and Managing Data Using SQL Server 2005Just a minute How can you return a user-defined error message in a batch? Answer: Using the RAISERROR statement Ver. 1.0 Session 11 Slide 27 of 31
    • 28. Querying and Managing Data Using SQL Server 2005Summary 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. Ver. 1.0 Session 11 Slide 28 of 31
    • 29. Querying and Managing Data Using SQL Server 2005Summary (Contd.) – 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. Ver. 1.0 Session 11 Slide 29 of 31
    • 30. Querying and Managing Data Using SQL Server 2005Summary (Contd.) 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. Ver. 1.0 Session 11 Slide 30 of 31
    • 31. Querying and Managing Data Using SQL Server 2005Summary (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 Ver. 1.0 Session 11 Slide 31 of 31

    ×