sql - SQL Server默认创建非聚集索引

标签 sql sql-server indexing clustered-index

是的,它是 this重复 。但我只需要澄清this article作者:Pinal Dave,内容如下:

Scenario 4: Primary Key defaults to Clustered Index with other index defaults to Non-clustered index

In this case we will create two indexes on the both the tables but we will not specify the type of the index on the columns. When we check the results we will notice that Primary Key is automatically defaulted to Clustered Index and another column as a Non-clustered index.

-- Case 4 Primary Key and Defaults
USE TempDB
GO
-- Create table
CREATE TABLE TestTable
(ID INT NOT NULL PRIMARY KEY,
Col1 INT NOT NULL UNIQUE)
GO
-- Check Indexes
SELECT OBJECT_NAME(OBJECT_ID) TableObject,
[name] IndexName,
[Type_Desc]
FROM sys.indexes
WHERE OBJECT_NAME(OBJECT_ID) = 'TestTable'
GO
-- Clean up
DROP TABLE TestTable
GO

最佳答案

唯一自动创建的索引:

  • 主键上的聚集索引(除非您另外指定 - 如果您将主键定义为非聚集,则将创建非聚集索引)

  • UNIQUE CONSTRAINT应用于一列(或一组列)时的唯一非聚集索引

关于sql - SQL Server默认创建非聚集索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16746875/

相关文章:

MYSQL GROUP BY 使用全文搜索不返回任何结果

具有两种不同连接和计数的 MYSQL 查询

mysql - 没有 join 语句的双重选择的 sql

C# SMO 数据库不记录创建

mysql - 事件记录查找 - 尽管添加了索引,find_by_inventory_product_id 仍间歇性缓慢

varchar 上的 SQL 索引

indexing - ElasticSearch 索引 Confluence 页面

mysql - 哪个sql语句应该是正确的?

sql - 更好的 Linq GroupBy

sql - 使用sql查询老化结构