Tài liệu MASTERING SQL SERVER 2000- P6 ppt

50 434 0
Tài liệu MASTERING SQL SERVER 2000- P6 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CHAPTER 6 • SELECT QUERIES 220 10. On the Start Copying Files screen, click Next to complete the installation. 11. After setup has completed copying files, click Finish. After the short task of installation has been completed, you are ready to configure Full-Text Search for use. The first thing you need to do is create a full-text index. Full- text indexes are created with SQL Server tools, such as Enterprise Manager, but they are maintained by the Microsoft Search Service and stored on the disk as files separate from the database. To keep the full-text indexes organized, they are stored in catalogs in the database. You can create as many catalogs in your databases as you like to orga- nize your indexes, but these catalogs cannot span databases. When a full-text index is first created, it is completely useless. Because these indexes are maintained by the Microsoft Search Service, you must specifically instruct the Search Service to fill the index with information about the text fields that you want to search. This filling of the full-text indexes is called populating the index. As your data changes over time, you will need to tell the Search Service to rebuild your full-text indexes to match your data—this process is called repopulating. In the following steps, you will create a catalog and index for the Employees table in the Northwind database. Employees was chosen because it has a text column in it (actually it’s ntext, which is Unicode as opposed to standard ANSI text). Here’s how to create the index and catalog: 1. While still in Enterprise Manager, click the Northwind icon and from the Tools menu select Full-Text Indexing. 2. On the first screen of the Full-Text Indexing Wizard, click Next. 2627ch06.qxt 9/6/00 11:16 AM Page 220 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 221 3. On the second screen, you must select a table to index. Here, pick [dbo].[Employees] because it has a text column and click Next. 4. Each table on which you create a full-text index must already have a unique index associated with it for Full-Text to work. In this instance, select the default PK_Employees index and click Next. FULL-TEXT SEARCHING Transact-SQL PART II 2627ch06.qxt 9/6/00 11:16 AM Page 221 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHAPTER 6 • SELECT QUERIES 222 5. On the next screen, you are asked which column you want to full-text index. Because Notes is your ntext column, select it here by checking the box next to it and click Next. 2627ch06.qxt 9/6/00 11:16 AM Page 222 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 223 6. On the next screen, you are asked in which catalog you would like to store this new index. You’ll need to create a new one here, because there are none avail- able. In the Name field, enter Northwind Catalog and click Next. 7. On the next screen, you are asked to create a schedule for automatically repopu- lating the full-text index. If your data is frequently updated, you will want to do this more often, maybe once a day. If it is read more often than it is changed, you should repopulate less frequently. You can schedule population for a single table or an entire catalog at a time. Here, you will set repopulation to happen just once for the entire catalog by clicking the New Catalog Schedule button. 8. On the New Schedule Properties screen, enter Populate Northwind and click OK. FULL-TEXT SEARCHING Transact-SQL PART II 2627ch06.qxt 9/6/00 11:16 AM Page 223 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHAPTER 6 • SELECT QUERIES 224 9. When you are taken back to the Full-Text Indexing Wizard, click Next. 10. On the final screen of the Wizard, you are given a summary of the choices you have made. Click Finish to create the index. To use your new full-text index, you will need to populate it for the first time. Here’s how: 1. In Enterprise Manager, expand Northwind and select Full-Text Catalogs. 2627ch06.qxt 9/6/00 11:16 AM Page 224 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 225 2. In the contents pane (on the right), right-click the Northwind Catalog and move to Start Population. 3. From Start Population, select Full Population. With a new, fully populated full-text index in place, you are ready to unleash the power of the full-text search. To do that, you need to know how to modify your SELECT query to work with the Microsoft Search Service that scans your new index. Let’s look at some new clauses for full-text search. Performing Full-Text Searches The nice thing about performing a full-text search is that you already know how to do it, or at least you are very close. Full-text searches are just SELECT queries that use full-text operators. Four operators are used to search through a full-text index: CONTAINS and CONTAINSTABLE: These can be used to get exact or not- so-exact words or phrases from text columns. Not-so-exact means that if you look for cook, you could also find cooks, cooked, cooking, etc. FREETEXT and FREETEXTTABLE: These are less precise than CON- TAINS; they return the meaning of phrases in the search string. For example, if you search for the string “SQL is a database server”, you would receive results containing the words SQL, database, server, and any derivative thereof. The difference between CONTAINS/FREETEXT and CONTAINSTABLE/FREETEXT- TABLE is that the latter don’t return a normal result set. Instead, they create a whole new table for you to search through. These operators are generally used in complex queries that require you to join the original table with the newly created table that came from the CONTAINSTABLE/FREETEXTTABLE query. To see how to use the CONTAINS/FREETEXT operators, let’s execute some queries: 1. Open Query Analyzer and log in using Windows NT Authentication. 2. Execute the following code: USE Northwind SELECT notes FROM Employees WHERE CONTAINS (Notes, ‘”French”’) FULL-TEXT SEARCHING Transact-SQL PART II 2627ch06.qxt 9/6/00 11:16 AM Page 225 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHAPTER 6 • SELECT QUERIES 226 3. In the result set, notice that each record returned contains the word French. Now execute the following code to test FREETEXT: USE Northwind SELECT notes FROM Employees WHERE FREETEXT (Notes, ‘“selling peace”’) 4. In the result set, notice that each record contains either selling or peace in some form. 2627ch06.qxt 9/6/00 11:16 AM Page 226 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 227 The FREETEXTTABLE and CONTAINSTABLE operators function quite a bit differ- ently from their counterparts. These two operators look through the full-text indexes and create a brand-new table with two columns: key and rank. The key column tells you the record number of the record that matches your query, so if record number 3 in the queried table matches your query, the key column would simply say 3. The rank column tells you how closely the record matches your query: 1000 indicates an exact match, and 1 indicates a low chance of a match. You can use the new table that is created by FREETEXTTABLE in a JOIN to see how closely each record in your table matches your query. For example, if you want to know who in your company speaks French and German, you could use the following query (also see Figure 6.21): USE Northwind SELECT new.[key], new.rank, emp.lastname, emp.firstname, emp.notes FROM employees AS emp INNER JOIN FREETEXTTABLE(employees, notes, ‘German French’) AS new ON emp.employeeid = new.[key] ORDER BY new.rank DESC FULL-TEXT SEARCHING Transact-SQL PART II 2627ch06.qxt 9/6/00 11:16 AM Page 227 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. CHAPTER 6 • SELECT QUERIES 228 FIGURE 6.21 FREETEXTTABLE generates a completely new table with a rank column. Let’s examine the result set that comes from this query, as displayed in Figure 6.21. First you told SQL to select the key and rank columns from the table that the FREE- TEXTTABLE operator creates. The key column tells you the record number of the matching record, and the rank column tells you how closely that record matches. Take a look at the first record: The key is 2, which means that the record number is 2 (literally, it is the second record in the table). The rank column is 174—this is the highest matching record in the table. Now read the notes column and notice that it has both German and French. The same is true of the second record in the result set— both German and French are mentioned. In the third record of the result set, you will notice that the rank is 0, which means that it has a very low chance of containing the data you want. In fact, if you look at the notes column for the third record of the result set, you will see that only French is mentioned, not German. The same is true of the other records as well, each having no chance of meeting your requirements. These full-text search queries can be very powerful tools for locating data in large text columns, but they are valueless if you don’t maintain them. Let’s see what it takes to administer your newfound indexes. 2627ch06.qxt 9/6/00 11:16 AM Page 228 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 229 Administering Full-Text Search There is not a lot of work involved in administering Full-Text Search. The most impor- tant thing to remember is the repopulation of the full-text indexes, and that can be scheduled when you first create the catalog. However, if you underestimate the fre- quency of data updates, you may need to change that schedule. To change the repop- ulation schedule, follow these steps: 1. In Enterprise Manager, expand the database containing the full-text catalog you want to modify. In this instance, it is Northwind. 2. Click the Full-Text Catalog icon. 3. In the contents (right) pane, right-click the Northwind Catalog icon and select Schedules. 4. In the Schedules dialog box that pops up, select the schedule to change and click Edit, then select Recurring. FULL-TEXT SEARCHING Transact-SQL PART II 2627ch06.qxt 9/6/00 11:16 AM Page 229 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... every time the query is finished To link a SQL Server named Washington, for example, you would use the following: sp_addlinkedserver ‘Washington’, SQL Server To query the Northwind database on the Washington SQL Server machine, all you need to do is add it to your SELECT query, as follows: SELECT * FROM Washington.Northwind Employees Linking to a non -SQL server is just as easy—it just requires a little... SQL Server to log in to another database server, just the way you log in with Query Analyzer This allows SQL Server to perform queries on the remote server on behalf of the end user The database server in question does not even have to be SQL Server, which means that you can query an Access or Oracle database with this type of query Two different types of linked server queries are at your disposal: ad... you could raise the price of every book by 10%: UPDATE titles SET price = price * 1.1 When SQL Server sees a word that’s not a keyword (such as price in this example), SQL Server tries to identify it as the name of a SQL Server object Here, because the UPDATE statement works on the titles table, it’s clear to SQL Server that there’s only one such object, the price column in the table You can use the special... Microsoft Search Service and include the MSSQL2000\DATA directory in your Windows NT backup strategy Using all of the tools we have discussed thus far, you can get any data you want out of your server However, many companies have data spread across many servers To get to that multiserver data, you need to link your servers and perform linked server queries Linked Server Queries PA R T SELECT Access.* FROM... queries on each server and then manually try to combine the results into something meaningful To ease the process of getting result sets that comprise data from multiple servers, there are linked server queries (also known as distributed or heterogeneous queries) When you perform a query using Query Analyzer, you are asked to log in every time The process of linking servers allows one SQL Server to log... SQL, it’s useful to be able to experiment with the UPDATE and DELETE statements without actually altering data You can do this by using transactions You’ll learn about transactions in depth in Chapter 8, but basically, a transaction is a SQL Server unit of work You can tell SQL Server when to start this unit of work with the BEGIN TRANSACTION statement When you’re done with the work, you can tell SQL. .. Northwind database If you need to execute a linked server query on a more frequent basis, OPENROWSET is not going to work for you For frequent linked server queries, you will need to permanently link your server with the sp_addlinkedserver stored procedure This stored procedure will allow the local server (where the user logs on) to log on to the remote server and stay logged on With OPENROWSET, the link... 2627ch06.qxt 9/6/00 11:16 AM Page 231 LINKED SERVER QUERIES 231 The only other administrative activity you need to engage in for Full-Text Search is backing up the indexes themselves Although full-text indexes are managed through Enterprise Manager, they are not actually part of the SQL Server database structure In fact, they are stored outside of SQL Server in an entirely separate directory, which... later in this section • An optional WITH LOG In previous versions of SQL Server, you could use this clause to force the WRITETEXT operation to be logged In SQL Server 2000, this clause is simply ignored, and logging depends on the recovery model in Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Transact -SQL 2627ch07.qxt 2627ch07.qxt 254 8/22/00 10:40 AM Page 254 CHAPTER... www.verypdf.com to remove this watermark II Transact -SQL A growing number of companies have more than one server from which they need to extract data to formulate reports With all of the queries you have seen thus far, this task would be very difficult, because all of these SELECT queries are designed to work with only one server at a time To get data from multiple servers with standard query methods, you would . of linking servers allows one SQL Server to log in to another data- base server, just the way you log in with Query Analyzer. This allows SQL Server to. use the following: sp_addlinkedserver ‘Washington’, SQL Server To query the Northwind database on the Washington SQL Server machine, all you need to

Ngày đăng: 24/12/2013, 02:18

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan