77
T-SQL for Data Manipulation Vu Tuyet Trinh [email protected] Hanoi University of Technology 1

Session3_TSQL_DML

Embed Size (px)

Citation preview

Page 1: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 1/77

T-SQL for Data

Manipulation

Vu Tuyet [email protected]

Hanoi University of Technology

1

Page 2: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 2/77

Microsoft

Microsoft

Overview of Transact-SQL

Based on AINSI SQL 92 standard

Composing of three categories

Data Manipulation Language (DML) Data Definition Language (DDL)

Data Control Language (DCL)

Having some Microsoft specific extensions

Beyond relational data

.net framework integration

Page 3: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 3/77

Microsoft

Microsoft

Outline

Selecting Data from Existing Tables

Inserting Data

Updating Data Deleting Data

Fulltext Serach

Page 4: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 4/77

Microsoft

Microsoft

Select Data

The simple syntax to select the itemsfrom the exit table is:

select <the items >

from <name of table>

where <conditions>

Page 5: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 5/77

Microsoft

Microsoft

Select Data

You can change the name of the items in the table:

Page 6: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 6/77

Microsoft

Microsoft

Select Data

You can insert some items as following:

Page 7: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 7/77

MicrosoftMicrosoft

Select

Example a function µcount¶ to count the columns of the

table:

Page 8: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 8/77

MicrosoftMicrosoft

Select Data

You can use some functions or some operations in the

Select Statement:

Page 9: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 9/77

MicrosoftMicrosoft

Select Data

Page 10: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 10/77

MicrosoftMicrosoft

Select Data

Maths

Functions

Functions

String

Functions

The Time

functions

Page 11: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 11/77

MicrosoftMicrosoft

- ASCII()

- CHAR()

- UPPER()

- LOWER()

- LEN()- LTRIM()

- RTRIM()

- LEFT()

- RIGHT()

- AVG()

- MIN()

- MAX()

- SUM()- COUNT()

- SQUARE()

- SQRT()

- ROUND()

- GETDATE()

-

DATEPART(YY,g

etdate())

-DATEDIFF(X,Y,Z)

-

DAY(),MONTH(),

YEAR()

Select Data

Maths String Time

Page 12: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 12/77

MicrosoftMicrosoft

Select Data

Besides, there are some functions to convert the data

type:

- CAST() - CONVERT()

Page 13: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 13/77

MicrosoftMicrosoft

Select

Select

Order by«.ect«

Join..on

Cross join

Group by

Oder by

Compute

For 

Into

Having

With

Except ,

Intersect

Union

Option

Page 14: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 14/77

MicrosoftMicrosoft

Select-With statement

Specifies a temporary named result set, known as a common

table expression (CTE).

Page 15: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 15/77

MicrosoftMicrosoft

Select-Order by

- You can arrange the result follow the first letter by using the

order by statement:

Page 16: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 16/77

MicrosoftMicrosoft

Select-multiple tables

You can select the items from more than a table as

following:

With ³outer join´:

Page 17: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 17/77

MicrosoftMicrosoft

Select ± multiple tables

Inner join:

Page 18: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 18/77

MicrosoftMicrosoft

Select-multiple tables

- When you select from more than 1 table, you can use the

 join«on statement or you can use the cross join statement.

With the cross join statement, you don¶t have to have torelative condition between the tables and the result is all the

Objects:

Page 19: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 19/77

MicrosoftMicrosoft

Select-multiple tables

Page 20: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 20/77

MicrosoftMicrosoft

Select-multiple tables

- You can select from many tables, and create a new table to

store that result:

 Now, you can refresh the sql folder, and you can see the newtable named newtbl as the following:

Page 21: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 21/77

MicrosoftMicrosoft

Select-multiple tables

Page 22: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 22/77

MicrosoftMicrosoft

Select-merge

 A new feature in SQL2008 is merge statement. You can merge 2 or 

more tables:

Page 23: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 23/77

MicrosoftMicrosoft

Select-Group by

Specifies the groups into which output rows are to be placed. If aggregate

functions are included in the SELECT clause <select list>, GROUP BY 

calculates a summary value for each group.

Page 24: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 24/77

MicrosoftMicrosoft

Select-Having

Specifies a search condition for a group or an aggregate

HAVING is typically used in a GROUP BY clause. When

GROUP BY

is not used, HAVING behaves like aW

HEREclause.

Page 25: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 25/77

MicrosoftMicrosoft

Page 26: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 26/77

MicrosoftMicrosoft

Select-intersect

INTERSECT returns any distinct values that are returned by both the query

on the left and right sides of the INTERSECT operand.

Page 27: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 27/77

MicrosoftMicrosoft

Select-except

EXCEPT returns any distinct values from the left query that

are not also found on the right query.

Page 28: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 28/77

Page 29: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 29/77

MicrosoftMicrosoft

Select-Compute by

Page 30: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 30/77

Page 31: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 31/77

MicrosoftMicrosoft

Select-For clause

Page 32: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 32/77

MicrosoftMicrosoft

Insert

The simple syntax for Insert statement is:INSERT INTO<table(x,y,«)>

VALUES(a,b,«)

Page 33: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 33/77

MicrosoftMicrosoft

Insert

- You can insert some values into the table, and you have to

insert all the value not null, and the primary key:

Page 34: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 34/77

MicrosoftMicrosoft

Insert

Page 35: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 35/77

Page 36: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 36/77

MicrosoftMicrosoft

Insert

- You can create a new table and insert the values into that table

Page 37: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 37/77

MicrosoftMicrosoft

Insert

Page 38: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 38/77

MicrosoftMicrosoft

Insert

- Inserting data into a unique identifier column by using

 NEWID()

Page 39: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 39/77

MicrosoftMicrosoft

Insert

- Inserting data into a table through a view

Page 40: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 40/77

MicrosoftMicrosoft

Insert

Page 41: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 41/77

MicrosoftMicrosoft

Delete

The simple syntax:

delete from<table>

- Delete with the condition where

Page 42: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 42/77

MicrosoftMicrosoft

Delete

- Using DELETE on the current row of a cursor 

Page 43: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 43/77

MicrosoftMicrosoft

Delete

- Using DELETE based on a subquery and using the Transact-

SQL extension

Page 44: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 44/77

MicrosoftMicrosoft

Delete

- Using DELETE with the TOP clause

Page 45: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 45/77

MicrosoftMicrosoft

Delete

Using DELETE with the OUTPUT clause

Page 46: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 46/77

MicrosoftMicrosoft

Update

The simple syntax for Update statement is:

UPDATE <table>

SET <items to update>

WHERE <conditions>

Page 47: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 47/77

MicrosoftMicrosoft

Update

- Using UPDATE with the FROM Clause

Page 48: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 48/77

Page 49: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 49/77

MicrosoftMicrosoft

Update

- Before :

Page 50: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 50/77

MicrosoftMicrosoft

Update

 After 

Page 51: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 51/77

MicrosoftMicrosoft

Fulltext Search

Full-Text Search Architecture

 Administering Full-Text Search

Querying SQL Server Using Full-Text Search

Page 52: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 52/77

MicrosoftMicrosoft

Overview

Page 53: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 53/77

MicrosoftMicrosoft

Overview

The architecture consists of the following processes:

SQL Server process (Sqlservr.exe)

Microsoft Full-Text Engine for SQL Server process (Msftesql.exe)

Microsoft Full-Text Engine Filter Daemon process (Msftefd.exe)

Page 54: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 54/77

MicrosoftMicrosoft

Full-Text Search Terminology

Full-text index

Stores information about significant words and their location within a given column. This information is used toquickly compute full-text queries that search for rows with particular words or combinations of words.

Full-text catalog

 A full-text catalog contains zero or more full-text indexes. Full-text catalogs must reside on a local hard driveassociated with the instance of SQL Server. Each catalog can serve the indexing needs of one or more tables

within a database. Full-text catalogs cannot be stored on removable drives, floppy disks, or network drives, exceptwhen you attached a read-only database that contains a full-text catalog.

Word breaker 

For a given language, a word breaker tokenizes text based on the lexical rules of the language.

Token

Is a word or a character string identified by the word breaker.

Stemmer 

For a given language, a stemmer generates inflectional forms of a particular word based on the rules of that

language. Stemmers are language specific. Filter Given a specified file type, for example .doc, filters extract text from a file stored in a varbinary(max) or 

image column.

Population or Crawl Is the process of creating and maintaining a full-text index.

Noise words Are frequently occurring words that do not help the search. For example , for the English localewords such as "a", "and", "is", and "the" are considered noise words. These words are ignored to prevent the full-text index from becoming bloated.

Page 55: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 55/77

MicrosoftMicrosoft

 Administering Full-Text Search

The system stored procedures that are used to implement and query

full-text indexes.

sp_fulltext_catalog

sp_help_fulltext_catalogs_cursor sp_fulltext_column

sp_help_fulltext_columns

sp_fulltext_database

sp_help_fulltext_columns_cursor 

sp_fulltext_service

sp_help_fulltext_tablessp_fulltext_table

sp_help_fulltext_tables_cursor 

sp_help_fulltext_catalogs

DENY  permission [ ,...n ] ON FULLTEXT CATALOG :: full-

t ex t_catal og  _name TO d atabase _ princ ipal [ ,...n ] [ CASCADE ] [ AS

Page 56: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 56/77

MicrosoftMicrosoft

 Administering Full-Text Search

Full-text administration can be separated into three main

tasks:

Creating/altering/dropping full-text catalogs

Creating/altering/dropping full-text indexes

Scheduling and maintaining index population.

Page 57: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 57/77

MicrosoftMicrosoft

 Administering Full-Text Search

Full-text indexes Regular SQL Server indexesStored in the file system, but

administered through the database.

Stored under the control of the database

in which they are defined.

Only one full-text index allowed per 

table.

Several regular indexes allowed per 

table.

 Addition of data to full-text indexes, 

called population, can be requested

through either a schedule or a specific

request, or can occur automatically withthe addition of new data.

Updated automatically when the data

upon which they are based is inserted, 

updated, or deleted.

Grouped within the same database into

one or more full-text catalogs.Not grouped.

Page 58: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 58/77

Page 59: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 59/77

MicrosoftMicrosoft

Guidelines in administering full-text indexes (2)

 Always select the smallest unique index available for your full-text unique key. (A 4-

byte, integer-based index is optimal.) This reduces the resources required by

Microsoft Search service in the file system significantly. If the primary key is large

(over 100 bytes), consider choosing another unique index in the table (or creating

another unique index) as the full-text unique key. Otherwise, if the full-text unique key

size exceeds the maximum size allowed (900 bytes), full-text population will not beable to proceed.

If you are indexing a table that has millions of rows, assign the table to its own full-

text catalog.

Consider the amount of change occurring in the tables being full-text indexed, as wellas the number of table rows. If the total number of rows being changed, together with

the numbers of rows in the table present during the last full-text population, 

represents millions of rows, assign the table to its own full-text catalog.

Page 60: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 60/77

MicrosoftMicrosoft

Guidelines in administering full-text indexes (3)

 After the word breaker-routine has a list of valid words for a row within a column, the

full-text engine calculates tokens to represent the words. A token is simply a

compressed form of the original word that saves space and ensures that full-textindexes can be created in as compact a form as possible.

The full text-text functionality then builds all tokens in a column into inverted, stacked, 

compressed structure within a file that is used for search operations. This uniquestructure allows ranking and scoring algorithms to efficiently satisfy possible queries.

Page 61: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 61/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

Full-Text query keywords

FREETEXT

FREETEXTTABLE

CONTAINS

CONTAINSTABLE

Page 62: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 62/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

FREETEXT

FREETEXT ( { c olumn _name | (c olumn _l ist ) | * } , 'f reet ex t_st ring ' [ ,

LANGUAGE lang uage _t erm ] )

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE [Description] LIKE N'%bike%';

SELECT ProductDescriptionID, Description FROM Production.ProductionDescription

WHERE FREETEXT(Description, N¶bike¶); --Must be UNICODE. Otherwise prevents a

query optimizer from parameter sniffing

SELECT ProductDescriptionID, Description FROM Production.ProductionDescription

WHERE CONTAINS(Description, N¶bike¶);

Page 63: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 63/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

Page 64: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 64/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

FREETEXTTABLE

FREETEXTTABLE (tabl e , { c olumn _name | (c olumn _l ist ) | * } , 'f reet ex t_st ring '

[ ,LANGUAGE lang uage _t erm ] [ ,t op _n _by  _r ank ] )

The FREETEXTTABLE version of the previous FREETEXT query would look

like this:

SELECT PD.ProductDescriptionID, PD.Description, KEYTBL.[KEY], 

KEYTBL.RANK FROM Production.ProductDescription AS PD

INNER JOIN FREETEXTTABLE()Production.ProductDescription, Description, 

N¶bike¶)

 AS KEYTBL ON PD.ProductDescriptionID = KEYTBL.[KEY]

Page 65: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 65/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

CONTAINS

CONTAINS ( { c olumn _name | (c olumn _l ist ) | * } , '< contains_search_condition

>' [ , LANGUAGE lang uage _t erm ] ) < contains_search_condition > ::= { <

simple_term > | < prefix_term > | < generation_term > | < proximity_term > | 

< weighted_term > } | { ( < contains_search_condition > ) [ { < AND > | < ANDNOT > | < OR > } ] < contains_search_condition > [ ...n ] } < simple_term >

::= wor d  | " phr ase " < prefix term > ::= { "wor d  * " | " phr ase *" } <

generation_term > ::= FORMSOF ( { INFLECTIONAL | THESAURUS } , <

simple_term > [ ,...n ] ) < proximity_term > ::= { < simple_term > | < prefix_term >

} { { NEAR | ~ }

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, N'bike');

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, N'''bike*'''):

Page 66: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 66/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, N' FORMSOF (INFLECTIONAL, bike) ');

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, N' FORMSOF (THESAURUS, bike) ');

Word proximity is a common way of searching documents for multiple keywords or phrases. This typeof query uses the NEAR(~) keyword. The closer words are to each other , the better the match for 

these types of queries. The proximity is used as a part of RANK calculation for rows matching the

search criteria. This keyword is rarely used with the CONTAINS predicate because the rank of 

matched results cannot be evaluated directly.

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, N'mountain NEAR bike');

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, N'mountain ~ bike');

SELECT ProductDescriptionID, Description FROM Production.ProductDescription

WHERE CONTAINS(Description, 'ISABOUT (mountain weight(.8), bike weight (.2) )');

Page 67: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 67/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

The CONTAINSTABLE function has the same capabilities as the CONTAINS function.

However , like the FREETEXTABLE function, it returns a rowset that contains a RANK

and a KEY column that can be used to return the best matches to a search.

CONTAINSTABLE ( tabl e , { c olumn _name | (c olumn _l ist  ) | * } , ' <contains_search_condition > ' [ , LANGUAGE lang uage _t erm] [ ,t op _n _by  _r ank ] ) <

contains_search_condition > ::= { < simple_term > | < prefix_term > | <

generation_term > | < proximity_term > | < weighted_term > } | { ( <

contains_search_condition > ) { { AND | & } | { AND NOT | &! } | { OR | | } } <

contains_search_condition > [ ...n ] } < simple_term > ::= wor d  | " phr ase " <

prefix term > ::= { "wor d  * " | " phr ase *" } < generation_term > ::= FORMSOF (

{ INFLECTIONAL | THESAURUS } , < simple_term > [ ,...n ] ) < proximity_term >::= { < simple_term > | < prefix_term > } { { NEAR | ~ } { < simple_term > | <

prefix_term > } } [ ...n ] < weighted_term > ::= ISABOUT ( { { < simple_term

> | < prefix_term > | < generation_term > | < proximity_term > } [ WEIGHT (

weight_value ) ] } [ ,...n ] )

Page 68: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 68/77

MicrosoftMicrosoft

Querying SQL Server Using Full-Text Search

Full-text search much more powerful than LIKE

Used correctly, will produce more specific, relevant results

Better performance ± LIKE queries are designed for small amounts of 

text data, full-text search scales to huge documents

Provides ranking of results

Common uses

Search through the content in a text-intensive, database driven website, 

e.g. a knowledge base

Search the contents of documents stored in BLOB fields

Perform advanced searches

e.g. with exact phrases - "to be or not to be" (however needs care!)

e.g. Boolean operators - AND, OR, NOT, NEAR

Page 69: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 69/77

MicrosoftMicrosoft

Writing FTS terms

The power of FTS is in the expression which is passed

to the CONTAINS or CONTAINSTABLE function

Several different types of terms:

Simple terms

Prefix terms

Generation terms

Proximity terms

Weighted terms

Page 70: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 70/77

MicrosoftMicrosoft

Simple terms

Either words or phrases

Quotes are optional, but recommended

Matches columns which contain the exact words or phrases

specified Case insensitive

Punctuation is ignored

e.g.

CONTAINS(Column, 'SQL')

CONTAINS(Column, ' "SQL" ')

CONTAINS(Column, 'Microsoft SQL Server')

CONTAINS(Column, ' "Microsoft SQL Server" ')

Page 71: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 71/77

MicrosoftMicrosoft

Prefix terms

Matches words beginning with the specified text

e.g.

CONTAINS(Column, ' "local*" ')

matches local, locally, locality

CONTAINS(Column, ' "local wine*" ')

matches "local winery", "locally wined"

Page 72: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 72/77

MicrosoftMicrosoft

Generation terms

Two types:

Inflectional

FORMSOF(INFLECTIONAL, "expression")

Thesaurus

FORMSOF(THESAURUS, "expression")

Both return variants of the specified word, but variants

are determined differently

Page 73: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 73/77

MicrosoftMicrosoft

Inflectional

Matches plurals and words which share the

same stem

When vague words such as "best" are used, doesn't match the exact word, only "good"

Page 74: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 74/77

MicrosoftMicrosoft

Thesaurus

Supposed to match synonyms of search terms ± but the

thesaurus seems to be very limited

Does not match plurals

Not particularly useful

Page 75: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 75/77

MicrosoftMicrosoft

Proximity terms

SyntaxCONTAINS(Column, 'local NEAR winery')

CONTAINS(Column, ' "local" NEAR "winery" ')

Matches words which are NEAR each other  Terms on either side of NEAR must be either simple or 

proximity terms

Page 76: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 76/77

MicrosoftMicrosoft

Weighted terms

Each word can be given a rank

Can be combined with simple, prefix, generation and

proximity terms

e.g. CONTAINS(Column, 'ISABOUT(

performance weight(.8),

comfortable weight(.4)

)')

CONTAINS(Column, 'ISABOUT(

FORMSOF(INFLECTIONAL, "performance") weight (.8),

FORMSOF(INFLECTIONAL, "comfortable") weight (.4)

)')

Page 77: Session3_TSQL_DML

8/7/2019 Session3_TSQL_DML

http://slidepdf.com/reader/full/session3tsqldml 77/77