zoukankan      html  css  js  c++  java
  • 创建索引和主键

    一、      主建的创建与删除

    1.       创建表时同时创建主键(加primary key)

    Create Table Book

    (

      ID int identity(1,1) primary key,

      Name nvarchar(50) not null,

      StudentID int not null

    )

    2.       用SQL语句单独创建主键

    1)创建主键同时会自动在该主键上创建聚集索引

    语句:alter table [表名] add constraint PK_Book_ID primary key (主键列名)

    例如:alter table Book add constraint PK_Book_ID primary key (ID)

    2)创建主键但不在该主键上创建聚集索引(但会自动创建非聚集索引)

    语句:alter table [表名] add constraint PK_Book_ID primary key (主键列名)

    例如:alter table Book add constraint PK_Book_ID primary key (ID)

    3.       用SQL语句删除主键

    语句:alter table [表名] drop constraint [主键名]

    例如:alter table Book drop constraint [PK_Book_ID]

    二、      索引的创建与删除

    1.       创聚集索引

    语句:create clustered index [聚集索引名]  on [表名](要创建聚集索引的列名asc|desc) with(drop_existing = on)

    例如:create clustered index IX_CLU_Book_ID on Book(ID)

    2.       创非集索引

    语句:create index [非聚集索引名]  on [表名](要创建非聚集索引的列名 asc|desc) with(drop_existing = on)

    例如:create index IX_ Book_Name on Book(Name)

    3.       创复合索引

    语句:create index [复合索引名]  on [表名](列名1 asc|desc, 列名2 asc|desc) with(drop_existing = on)

    例如:create index IX_com_Book_IDName on Book (ID,Name)

    4.       创建唯一索引

    语句:create unique index index_name on table_name (column ASC|DESC[,.....])with (drop_existing = on)

    例如:create unique index IX_Book_ID on Book (ID)

    5.       创建覆盖索引

    语句:create index index_name on table_Name (columnName ASC|DESC[,......]) include(column_Name_List)with (drop_existing = on)

    例如:create index ix_cov_Book_ID_Name on Book (ID) include(Name)

    说明:覆盖索引它只是非聚集索引的一种特别形式,是把数据加到非聚集索引上。

    覆盖索引和普通非聚集索引的区别:

             1)非聚集索引不包涵数据,通过它找到的只是文件中数据行的引用(表是堆的情况下)或是聚集索引的引用,SQL Server要通这个引用去找到相应的数据行。

             2)正因为非聚集索引它没有数据,才引发第二次查找。

             3)覆盖索引就是把数据加到非聚集索引上,这样就不需要第二次查找了。这是一种以空间换性能的方法。

    6.       筛选索引

    语句:create index index_name on table_name(columName) where boolExpression

    例如:create index IX_Book_ID on Book(ID) where ID>100 and ID< 200

    说明:只对热点数据加索引,如果大量的查询只对ID 由 100 ~ 200 的数据感兴趣,就可以这样做。

          1)可以减小索引的大小

          2)为据点数据提高查询的性能

    7.       删除索引

    语句:drop index table_Name.Index_Name

    例如:drop index Book. IX_ Book_Name

    8.       查看表中索引存储过程

     execute sp_helpindex @objname = '表名'

    总结:

         BTree 索引有聚集与非聚集之分。

         就查看上到聚集索引性能比非聚集索引性能要好。

         非聚集索引分,覆盖索引,唯一索引,复合索引(当然聚集索引也有复合的,复合二字,只是说明索引,引用了多列),一般非聚集索引就查看上,非聚集索引中覆盖索引的性能比别的非聚集索引性能要好,它的性能和聚集索引差不多,可是它会用更多的磁盘空间。

    最后说一下这个

         1)with (drop_existing = on|off),加上这个的意思是如果这个索引还在表上就drop 掉然后在create 一个新的。特别是在聚集索引上用使用这个就可以不会引起非聚集索引的重建。

         2)with (online = on|off) 创建索引时用户也可以访问表中的数据,

         3)with(pad_index = on|off fillfactor = 80); fillfactor 用来设置填充百分比,pad_index 只是用来连接fillfactor 但是它又不能少。

         4)with(allow_row_locks = on|off   |   allow_page_locks = on |off);  是否允许页锁 or 行锁

         5)with (data_compression = row | page );  这样可以压缩索引大小

  • 相关阅读:
    暴力枚举 --- 多方法求解
    图论 --- 骑士周游问题,DFS
    数论 --- 同余定理
    数论 --- 筛法求素数进一步优化
    求大素数
    codeforces --- Round #244 (Div. 2) B. Prison Transfer
    codeforces --- Round #244 (Div. 2) A. Police Recruits
    线段树 --- (区间维护+逆推)
    线段数 --- (单点更新、求逆序对)
    线段树 --- (单点更新、区间求和、模板题)
  • 原文地址:https://www.cnblogs.com/guoxibaijv/p/6491899.html
Copyright © 2011-2022 走看看