SQL Server 有关 索引和视图的简单介绍
索引 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]
<span>index</span> index_name
<span>on</span> table_name (column_name ...)
[<span>with</span> <span>fillfactor</span>=x]
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>))
<span>drop</span> <span>index</span> student.idx_stu_name
<span>go</span>
<span>create</span> <span>index</span> idx_stu_name
<span>on</span>
student(name);
--联合<strong>索引</strong>
<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>))
<span>drop</span> <span>index</span> student.idx_uqe_clu_stu_name_age
<span>go</span>
<span>create</span> <span>unique</span> <span>clustered</span> <span>index</span> idx_uqe_clu_stu_name_age
<span>on</span> student(name, age);
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_cid'</span>))
<span>drop</span> <span>index</span> student.idx_cid
<span>go</span>
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_cid'</span>))
<span>drop</span> <span>index</span> student.idx_cid
<span>go</span>
--非聚集<strong>索引</strong>
<span>create</span> <span>nonclustered</span> <span>index</span> idx_cid
<span>on</span>
student (cid)
<span>with</span> <span>fillFactor</span> = 30; --填充因子
--聚集<strong>索引</strong>
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_sex'</span>))
<span>drop</span> <span>index</span> student.idx_sex
<span>go</span>
<span>create</span> <span>clustered</span> <span>index</span> idx_sex
<span>on</span>
student(sex);
--聚集<strong>索引</strong>
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.indexes <span>where</span> name = <span>'idx_name'</span>))
<span>drop</span> <span>index</span> student.idx_name
<span>go</span>
<span>create</span> <span>unique</span> <span>index</span> idx_name
<span>on</span>
student(name);
4、 适合的创建索引的列
当数据库的某一列被频繁的用于数据库查询时,或者该列用于数据库进行排序时可以创建成索引
5、 不适合创建索引的列
如果列中有几个不同的值,或者表中仅包含几行值,则不推荐为其创建索引。因为索引在搜索数据所花的时间比在表中逐行搜索话的时间更长。
Ø 视图
1、 什么是视图
视图就是一个虚拟的数据表,该数据表中的数据记录是有一条查询语句的查询结果得到的。
2、 创建视图准则
创建视图需要考虑一下准则:
# 视图名称必须遵循标识符的规则,该名称不得与该架构的如何表的名称相同
# 你可以对其他视图创建视图。允许嵌套视图,但嵌套不得超过32层。视图最多可以有1024个字段
# 不能将规则和default定义于视图相关联
# 视图的查询不能包含compute子句、compute by子句或into关键字
# 定义视图的查询不能包含order by子句,除非在select 语句的选择列表中还有top子句
下列情况必须指定视图中每列的名称:
# 视图中的如何列都是从算术表达式、内置函数或常量派生而来
# 视图中有两列或多列具有相同名称(通常由于视图定义包含联接,因此来自两个或多个不同的列具有相同的名称)
# 希望视图中的列指定一个与其原列不同的名称(也可以在视图中重命名列)。无论是否重命名,视图列都回继承原列的数据类型
3、 创建视图
--创建<strong>视图</strong>
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.objects <span>where</span> name = <span>'v_stu'</span>))
<span>drop</span> <span>view</span> v_stu
<span>go</span>
<span>create</span> <span>view</span> v_stu
<span>as</span>
<span>select</span> id, name, age, sex <span>from</span> student;
4、 修改视图
<span>alter</span> <span>view</span> v_stu
<span>as</span>
<span>select</span> id, name, sex <span>from</span> student;
<span>alter</span> <span>view</span> v_stu(编号, 名称, 性别)
<span>as</span>
<span>select</span> id, name, sex <span>from</span> student
<span>go</span>
<span>select</span> * <span>from</span> v_stu;
<span>select</span> * <span>from</span> information_schema.views;
5、 加密视图
--加密<strong>视图</strong>
<span>if</span> (<span>exists</span> (<span>select</span> * <span>from</span> sys.objects <span>where</span> name = <span>'v_student_info'</span>))
<span>drop</span> <span>view</span> v_student_info
<span>go</span>
<span>create</span> <span>view</span> v_student_info
<span>with</span> encryption --加密
<span>as</span>
<span>select</span> id, name, age <span>from</span> student
<span>go</span>
--view_definition <span>is</span> <span>null</span>
<span>select</span> * <span>from</span> information_schema.views
<span>where</span> table_name <span>like</span> <span>'v_stu'</span>;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.

Database technology competition: What are the differences between Oracle and SQL? In the database field, Oracle and SQL Server are two highly respected relational database management systems. Although they both belong to the category of relational databases, there are many differences between them. In this article, we will delve into the differences between Oracle and SQL Server, as well as their features and advantages in practical applications. First of all, there are differences in syntax between Oracle and SQL Server.
