Home > Database > Mysql Tutorial > SQL Server 有关 索引和视图的简单介绍

SQL Server 有关 索引和视图的简单介绍

WBOY
Release: 2016-06-07 15:10:05
Original
1035 people have browsed it

索引 1、 什么是 索引 索引 就是数据表中数据和相应的存储位置的列表,利用 索引 可以提高在表或 视图 中的查找数据的速度。 2、 索引 分类 数据库中 索引 主要分为两类:聚集 索引 和非聚集 索引 。SQL Server 2005还提供了唯一 索引 、 索引 视图 、全文

索引

    1、 什么是索引

        索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度。

 

    2、 索引分类

        数据库中索引主要分为两类:聚集索引和非聚集索引。SQL Server 2005还提供了唯一索引索引视图、全文索引、xml索引等等。聚集索引和非聚集索引是数据库引擎中索引的基本类型,是理解其他类型索引的基础。

 

    # 聚集索引

        聚集索引是值表中数据行的物理存储顺序和索引的存储顺序完全相同。聚集索引根据索引顺序物理地重新排列了用户插入到表中的数据,因此,每个表只能创建一个 聚集索引。聚集索引经常创建在表中经常被搜索到的列或按顺序访问的列上。在默认情况下,主键约束自动创建聚集索引

 

    # 非聚集索引

        非聚集索引不改变表中数据列的物理存储位置,数据与索引分开存储,通过索引指向的地址与表中的数据发生关系。

        非聚集索引没有改变表中物理行的位置,索引可以在以下情况下使用非聚集索引

        一、如果某个字段的数据唯一性比较高

        二、如果查询所得到的数据量比较少

 

聚集索引和非聚集索引的区别:

聚集索引

非聚集索引

每个表只允许创建一个聚集索引

最多可以有249个非聚集索引

物理的重排表中的数据以符合索引约束

创建一个键值列表,键值指向数据在数据页中的位置

用于经常查找数据的列

用于从表中查找单个值的列

    # 其他类型索引

    除了以上索引,还有以下类型索引

        a、 唯一索引:如果希望索引键都不同,可以创建唯一索引。聚集索引和非聚集索引都可以是唯一索引

        b、 包含新列索引索引列的最大数量是16个,索引列的字节总数的最高值是900。如果当多个列的字节总数大于900,切又想在这些劣种都包含索引是,可以使用包含性列索引

        c、 视图索引:提供视图查询效率,可以视图索引物理化,也就是说将结果集永久存储在索引中,可以创建视图索引

        d、 XML索引:是与xml数据关联的索引形式,是XML二进制blob的已拆分持久表示形式

        e、 全文索引:一种特殊类型的基于标记的功能性功能,用于帮助在字符串中搜索赋值的词

   

    3、 创建索引

    语法

<span>create</span> [<span>unique</span>] [<span>clustered</span> | noclustered]
Copy after login
<span>index</span> index_name
Copy after login
<span>on</span> table_name (column_name ...)
Copy after login
[<span>with</span> <span>fillfactor</span>=x]
Copy after login

    unique唯一索引

    clustered聚集索引

    noclustered非聚集索引

    fillfactor填充因子大小,范围在0-100直接,表示索引页填满的空间所占的百分比。

 

    示例

<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_stu_name'</span>))
Copy after login
    <span>drop</span> <span>index</span> student.idx_stu_name
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>create</span> <span>index</span> idx_stu_name
Copy after login
<span>on</span>
Copy after login
Copy after login
Copy after login
Copy after login
student(name);
Copy after login
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
--联合<strong>索引</strong>
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_uqe_clu_stu_name_age'</span>))
Copy after login
    <span>drop</span> <span>index</span> student.idx_uqe_clu_stu_name_age
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>create</span> <span>unique</span> <span>clustered</span> <span>index</span> idx_uqe_clu_stu_name_age
Copy after login
<span>on</span> student(name, age);
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_cid'</span>))
Copy after login
Copy after login
    <span>drop</span> <span>index</span> student.idx_cid
Copy after login
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_cid'</span>))
Copy after login
Copy after login
    <span>drop</span> <span>index</span> student.idx_cid
Copy after login
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
--非聚集<strong>索引</strong>
Copy after login
<span>create</span> <span>nonclustered</span> <span>index</span> idx_cid
Copy after login
<span>on</span>
Copy after login
Copy after login
Copy after login
Copy after login
student (cid)
Copy after login
<span>with</span> <span>fillFactor</span> = 30;    --填充因子
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
--聚集<strong>索引</strong>
Copy after login
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_sex'</span>))
Copy after login
    <span>drop</span> <span>index</span> student.idx_sex
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>create</span> <span>clustered</span> <span>index</span> idx_sex
Copy after login
<span>on</span>
Copy after login
Copy after login
Copy after login
Copy after login
student(sex);
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
--聚集<strong>索引</strong>
Copy after login
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_name'</span>))
Copy after login
    <span>drop</span> <span>index</span> student.idx_name
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>create</span> <span>unique</span> <span>index</span> idx_name
Copy after login
<span>on</span>
Copy after login
Copy after login
Copy after login
Copy after login
student(name);
Copy after login
Copy after login

   

    4、 适合的创建索引的列

        当数据库的某一列被频繁的用于数据库查询时,或者该列用于数据库进行排序时可以创建成索引

 

    5、 不适合创建索引的列

        如果列中有几个不同的值,或者表中仅包含几行值,则不推荐为其创建索引。因为索引在搜索数据所花的时间比在表中逐行搜索话的时间更长。

 

Ø 视图

    1、 什么是视图

        视图就是一个虚拟的数据表,该数据表中的数据记录是有一条查询语句的查询结果得到的。

 

    2、 创建视图准则

        创建视图需要考虑一下准则:

    # 视图名称必须遵循标识符的规则,该名称不得与该架构的如何表的名称相同

    # 你可以对其他视图创建视图。允许嵌套视图,但嵌套不得超过32层。视图最多可以有1024个字段

    # 不能将规则和default定义于视图相关联

    # 视图的查询不能包含compute子句、compute by子句或into关键字

    # 定义视图的查询不能包含order by子句,除非在select 语句的选择列表中还有top子句

   

    下列情况必须指定视图中每列的名称:

    # 视图中的如何列都是从算术表达式、内置函数或常量派生而来

    # 视图中有两列或多列具有相同名称(通常由于视图定义包含联接,因此来自两个或多个不同的列具有相同的名称)

    # 希望视图中的列指定一个与其原列不同的名称(也可以在视图中重命名列)。无论是否重命名,视图列都回继承原列的数据类型

 

    3、 创建视图

--创建<strong>视图</strong>
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.objects <span>where</span> name = <span>'v_stu'</span>))
Copy after login
    <span>drop</span> <span>view</span> v_stu
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>create</span> <span>view</span> v_stu
Copy after login
<span>as</span>
Copy after login
Copy after login
Copy after login
Copy after login
<span>select</span> id, name, age, sex <span>from</span> student;
Copy after login

   

    4、 修改视图

<span>alter</span> <span>view</span> v_stu
Copy after login
<span>as</span>
Copy after login
Copy after login
Copy after login
Copy after login
<span>select</span> id, name, sex <span>from</span> student;
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>alter</span> <span>view</span> v_stu(编号, 名称, 性别)
Copy after login
<span>as</span>
Copy after login
Copy after login
Copy after login
Copy after login
    <span>select</span> id, name, sex <span>from</span> student
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>select</span> * <span>from</span> v_stu;
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>select</span> * <span>from</span> information_schema.views;
Copy after login

   

    5、 加密视图

--加密<strong>视图</strong>
Copy after login
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.objects <span>where</span> name = <span>'v_student_info'</span>))
Copy after login
    <span>drop</span> <span>view</span> v_student_info
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<span>create</span> <span>view</span> v_student_info
Copy after login
<span>with</span> encryption --加密
Copy after login
<span>as</span>
Copy after login
Copy after login
Copy after login
Copy after login
    <span>select</span> id, name, age <span>from</span> student
Copy after login
<span>go</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
--view_definition <span>is</span> <span>null</span>
Copy after login
<span>select</span> * <span>from</span> information_schema.views 
Copy after login
<span>where</span> table_name <span>like</span> <span>'v_stu'</span>;
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template