您好,欢迎访问一九零五行业门户网

sql全文索引创建与查询实例

本文章来告诉你如何创建全文索引以及建立后的全文索引的查询操作,有需要了解可以看看。
建立全文索引
在进行全文检索之前,必须先建立和填充全文索引。为了支持全文索引操作,sql server 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 //打开数据库
//打开全文索引支持,启动sql server的服务
 代码如下 复制代码
execute sp_fulltext_database ‘enable’
//建立全文检索目录ft_test
 代码如下 复制代码
execute sp_fulltext_catalog ‘ft_test’, ‘create’
为title列建立全文索引数据元,pk_title为book表中由主键所建立的唯一索引,这个参数是必需的。
 代码如下 复制代码
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’
至此,全文索引建立完毕。
其它网友参考内容
--1. 查看数据库northwind 是否启用 全文索引 select * from sys.databases
use northwind
--2.创建全文目录
 代码如下 复制代码
create fulltext catalog [employee_fulltext]with accent_sensitivity = off
as default
--3. 指定唯一索引
 代码如下 复制代码
create fulltext index on [dbo].[employees]
    key index [pk_employees] on ([employee_fulltext]) with (change_tracking auto)
--4. 添加全文索引列
 代码如下 复制代码
alter fulltext index on [dbo].[employees] add ([address])
go
alter fulltext index on [dbo].[employees] add ([lastname])
go
alter fulltext index on [dbo].[employees] add ([firstname])
go
--5. 设置为可用
 代码如下 复制代码
alter fulltext index on [dbo].[employees] enable
go
sql server 2000提供的全文检索语句主要有contains和freetext。contains语句的功能是在表的所有列或指定列中搜索:一个字或短语;一个字或短语的前缀;与一个字相近的另一个字;一个字的派生字;一个重复出现的字。
contains语句的语法格式为:
 代码如下 复制代码
contains({column | *}), _condition> )
其中,column是搜索列,使用“*”时说明对表中所有全文索引列进行搜索。contains_search_
condition 说明contains语句的搜索内容,其语法格式为:
 代码如下 复制代码
{||||}[{{and|and not|or}}] [...n]
下面就simple_term和prefix_term参数做简要说明:
simple_term是contains语句所搜索的单字或短语,当搜索的是一个短语时,必须使用双引号作为定界符。其格式为:
{‘word’|“ phrase”}
prefix_term说明contains语句所搜索的字或短语前缀,其格式为:
{“word*” | “phrase*”}
例如,下面语句检索book表的title列和notes列中包含“database”或“computer”字符串的图书名称及其注释信息:
 代码如下 复制代码
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’)
其它类似信息

推荐信息