Académique Documents
Professionnel Documents
Culture Documents
Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn
Overview of Transact-SQL
Data Manipulation Language (DML) Data Definition Language (DDL) Data Control Language (DCL) Beyond relational data .net framework integration
Microsoft
Outline
Selecting Data from Existing Tables Inserting Data Updating Data Deleting Data Fulltext Serach
Microsoft
Select Data
The simple syntax to select the items from the exit table is:
select <the items > from <name of table> where <conditions>
Microsoft
Select Data
Microsoft
Select Data
Microsoft
Select
Microsoft
Select Data
You can use some functions or some operations in the Select Statement:
Microsoft
Select Data
Microsoft
Select Data
Functions
Maths Functions
String Functions
Microsoft
Select Data
Maths
String
Time
Microsoft
Select Data
Besides, there are some functions to convert the data type: - CAST() - CONVERT()
Microsoft
Select
With Into Join..on Cross join Group by Having Union Except , Intersect Oder by
Select
Order by.ect
Microsoft
Select-With statement
Specifies a temporary named result set, known as a common table expression (CTE).
Microsoft
Select-Order by
-
You can arrange the result follow the first letter by using the order by statement:
Microsoft
Select-multiple tables
You can select the items from more than a table as following: With outer join:
Microsoft
Inner join:
Microsoft
Select-multiple tables
-
When you select from more than 1 table, you can use the joinon statement or you can use the cross join statement. With the cross join statement, you dont have to have to relative condition between the tables and the result is all the Objects:
Microsoft
Select-multiple tables
Microsoft
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 new table named newtbl as the following:
Microsoft
Select-multiple tables
Microsoft
Select-merge
A new feature in SQL2008 is merge statement. You can merge 2 or more tables:
Microsoft
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.
Microsoft
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 a WHERE clause.
Microsoft
Microsoft
Select-intersect
INTERSECT returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.
Microsoft
Select-except
EXCEPT returns any distinct values from the left query that are not also found on the right query.
Microsoft
Select-Compute by
Generates totals that appear as additional summary columns at the end of the result set. When used with BY, the COMPUTE clause generates control-breaks and subtotals in the result set. You can specify COMPUTE BY and COMPUTE in the same query.
Microsoft
Select-Compute by
Microsoft
Select-For clause
FOR clause is used to specify either the BROWSE or the XML option. BROWSE and XML are unrelated options.
Microsoft
Select-For clause
Microsoft
Insert
Microsoft
Insert
-
You can insert some values into the table, and you have to insert all the value not null, and the primary key:
Microsoft
Insert
Microsoft
Insert
If you dont insert all the column not null, the process will terminate
Microsoft
Insert
-
You can create a new table and insert the values into that table
Microsoft
Insert
Microsoft
Insert
-
Microsoft
Insert
-
Microsoft
Insert
Microsoft
Delete
Microsoft
Delete
-
Microsoft
Delete
-
Microsoft
Delete
-
Microsoft
Delete
Microsoft
Update
Microsoft
Update
-
Microsoft
Update
-
Microsoft
Update
-
Before :
Microsoft
Update
After
Microsoft
Fulltext Search
Full-Text Search Architecture Administering Full-Text Search Querying SQL Server Using Full-Text Search
Microsoft
Overview
Microsoft
Overview
Microsoft
Full-text index Stores information about significant words and their location within a given column. This information is used to quickly 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 drive associated 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, except when 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 locale words such as "a", "and", "is", and "the" are considered noise words. These words are ignored to prevent the fulltext index from becoming bloated.
Microsoft
Microsoft
Microsoft
Addition of data to full-text indexes, called population, can be requested Updated automatically when the data through either a schedule or a specific upon which they are based is inserted, request, or can occur automatically with updated, or deleted. the addition of new data. Grouped within the same database into Not grouped. one or more full-text catalogs.
Microsoft
Like regular SQL Server indexes, full-text indexes can be automatically updated as data is modified in the associated tables. Alternatively, you can repopulate full-text indexes manually at appropriate intervals, but this can be time-consuming and resource-intensive. Therefore, index updating is usually performed as an asynchronous process that runs in the background during periods of low database activity. Tables with the same update characteristics (such as small number of changes versus large number of changes, or tables that change frequently during a particular time of day) should be grouped together and assigned to the same full-text catalog. By setting up full-text catalog population schedules in this way, full-text indexes stay synchronous with the tables without adversely affecting the resource usage of the database server during periods of high database activity.
It is important to plan the placement of full-text indexes for tables in full-text catalogs. When you assign a table to a full-text catalog, consider the following guidelines:
Microsoft
Always select the smallest unique index available for your full-text unique key. (A 4byte, 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 be able to proceed.
If you are indexing a table that has millions of rows, assign the table to its own fulltext catalog.
Consider the amount of change occurring in the tables being full-text indexed, as well as 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.
Microsoft
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-text indexes 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 unique structure allows ranking and scoring algorithms to efficiently satisfy possible queries.
Microsoft
Microsoft
Microsoft
Microsoft
CONTAINS ( { column_name | (column_list) | * } , '< contains_search_condition >' [ , LANGUAGE language_term ] ) < 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 > ::= word | " phrase " < prefix term > ::= { "word * " | "phrase *" } < 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*'''):
Microsoft
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
Microsoft
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
Microsoft
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, CONTAINS(Column, CONTAINS(Column, CONTAINS(Column,
'SQL') ' "SQL" ') 'Microsoft SQL Server') ' "Microsoft SQL Server" ')
Microsoft
Prefix terms
Microsoft
Generation terms
Two types:
Both return variants of the specified word, but variants are determined differently
Microsoft
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"
Microsoft
Thesaurus
Supposed to match synonyms of search terms but the thesaurus seems to be very limited Does not match plurals Not particularly useful
Microsoft
Proximity terms
Syntax
CONTAINS(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
Microsoft
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) )')
Microsoft
Microsoft