建立全文索引

来源:互联网 发布:梓潼广播电视台网络 编辑:程序博客网 时间:2024/04/30 16:39

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}@font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}p{mso-style-noshow:yes;mso-style-priority:99;mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:12.0pt;font-family:宋体;mso-bidi-font-family:宋体;}span.top11{mso-style-name:top11;mso-style-unhide:no;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;}-->

 转载自:http://www.cnblogs.com/chorrysky/archive/2007/09/05/882939.html


微软的SQL Server数据库是一个在中低端企业应用中占有广泛市场的关系型数据库系统,它以简单、方便、易用等特性深得众多软件开发人员和数据库管理人员的钟爱。 SQL Server 7.0以前的数据库系统由于没有全文检索功能,致使无法提供像文本内容查找此类的服务,成为一个小小的遗憾。从SQLServer 7.0起,到如今的SQLServer 2000终于具备了全文检索功能,使用户可以高效地检索存储在数据库charvarchartextntextncharnvarchar等数 据类型列中的文本数据。
建立全文索

在进行全文检索之前,必须先建立和填充数据库全文索引。为了支持全文索引操作,SQLServer 7.0新增了一些存储过程和Transact-SQL语句。使用这些存储过程创建全文索引的具体步骤如下(括号内为调用的存储过程名称):

1.启动数据库的全文处理功能(sp_fulltext_database);

2.
建立全文检索目录(sp_fulltext_catalog);

3.
在全文检索目录中注册需要全文索引的表(sp_fulltext_table);

4.
指出表中需要全文检索的列名(sp_fulltext_

column);


5.
为表创建全文索引(sp_fulltext_table);

6.
填充全文检索目录(sp_fulltext_catalog)

下面举例说明如何创建全文索引,在本例中,对Test数据库Book表中Title列和Notes列建立全文索引。

use test //
打开数据库

//
打开全文索引支持,启动SQLServer的全文搜索服务

execute sp_fulltext_database ‘enable’

//
建立全文检索目录ft_test

execute sp_fulltext_catalog ‘ft_test’, ‘create’

Title列建立全文索引数据元,pk_titleBook表中由主键所建立的唯一索引,这个参数是必需的。


注意:这里的pk_title是主键或唯一键的索引名,而非列名

execute sp_fulltext_table ‘book’
‘create’, ‘ft_test’‘pk_title’

//
设置全文索引列名

execute sp_fulltext_column ‘book’, ‘title’, ‘add’

execute sp_fulltext_column ‘book’
‘notes’, ‘add’

//
建立全文索引

execute sp_fulltext_table ‘book’, ‘activate’

//
填充全文索引目录

execute sp_fulltext_catalog ‘ft_test’, ‘start_full’

至此,全文索引建立完毕。

进行全文检索

SQL Server 2000
提供的全文检索语句主要有CONTAINSFREETEXTCONTAINS语句的功能是在表的所有列或指定列中搜索:一个字或短语;一个字或短语的前缀;与一个字相近的另一个字;一个字的派生字;一个重复出现的字。

CONTAINS
语句的语法格式为:

CONTAINS({column | *}),

_condition>)

其中,column是搜索列,使用“*”时说明对表中所有全文索引列进行搜索。Contains_search_

condition
说明CONTAINS语句的搜索内容,其语法格式为:

{||||}[{{AND|AND NOT|OR}}] [...n]

下面就simple_termprefix_term参数做简要说明:

simple_term
CONTAINS语句所搜索的单字或短语,当搜索的是一个短语时,必须使用双引号作为定界符。其格式为:

{‘word’|“ phrase”}

prefix_term
说明CONTAINS语句所搜索的字或短语前缀,其格式为:

{“word*” | “phrase*”}

例如,下面语句检索Book表的Title列和Notes列中包含“database”“computer”字符串的图书名称及其注释信息:

select title, notes

from book

where contains(tilte, ‘database’) or contains(notes
‘database’)

or contains(title
‘computer’) or contains(notes‘computer’)

FREETEXT
语句的功能是在一个表的所有列或指定列中搜索一个自由文本格式的字符串,并返回与该字符串匹配的数据行。所以,FREETEXT语句所执行的功能又称做自由式全文查询。

FREETEXT
语句的语法格式为:FREETEXT({column | * }‘freetext_string’)

其中,column是被搜索列,使用“*”时说明对表中的所有全文索引列进行搜索。Freetext_string参数指出所搜索的自由文本格式字符串。

例如,下面语句使用FREETEXT语句搜索Book表中包含“Successful Life”字符串的数据行:

select title, notes

from book

where freetext(*
‘Successful Life’)

 

原创粉丝点击