博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实用T-SQL之生成当前索引数据库中的外键上
阅读量:6209 次
发布时间:2019-06-21

本文共 4155 字,大约阅读时间需要 13 分钟。

       下面的T-SQL可以生成索引在当前数据库的外键上, 可以帮助我们找回外键上丢失的索引,查看索引命名是否规范。

 

-- declare memory tableDECLARE @INDEX_TABLE TABLE(    primary_key INT IDENTITY(1,1) NOT NULL,     schema_name NVARCHAR(100),     table_name NVARCHAR(100),     column_name NVARCHAR(100),    new_index_name NVARCHAR(100))-- feed memory table by all foreign key without index in databaseINSERT INTO @INDEX_TABLE    SELECT        S.name as [Schema name],        object_name(T.object_id) AS [Table name],        C.name AS [Column name],        ''    FROM         sys.columns C        LEFT JOIN sys.tables T ON (C.object_id = T.object_id)        LEFT JOIN sys.schemas S ON (S.schema_id = T.schema_id)        LEFT JOIN sys.foreign_key_columns FKC ON (FKC.parent_object_id = C.object_id AND C.column_id = FKC.parent_column_id)        LEFT JOIN sys.foreign_keys FK ON (FKC.constraint_object_id = FK.object_id)        LEFT JOIN sys.index_columns IC ON (IC.object_id = C.object_id AND IC.column_id = C.column_id)            LEFT JOIN sys.indexes I ON (I.index_id = IC.index_id  AND I.object_id = C.object_id)    WHERE         T.object_id is not null        AND FK.name IS NOT NULL        AND I.name IS NULL    ORDER BY         S.name,        object_name(T.object_id),        C.name DECLARE @loop_counter INTDECLARE @item_counter INTSET @loop_counter = ISNULL((SELECT COUNT(*) FROM @INDEX_TABLE),0)SET @item_counter = 1DECLARE @schema_name VARCHAR(100)DECLARE @table_name VARCHAR(100)DECLARE @column_name VARCHAR(100)DECLARE @query NVARCHAR(1000)DECLARE @index_name VARCHAR(200)WHILE @loop_counter > 0 AND @item_counter <= @loop_counterBEGIN            -- get one row from memory table        SELECT             @schema_name = schema_name,             @table_name = table_name,            @column_name = column_name        FROM             @INDEX_TABLE        WHERE             primary_key = @item_counter    -- prepare querySET @index_name = 'IX_' + @table_name + '_' + @column_nameSET @query = 'CREATE NONCLUSTERED INDEX [' + @index_name + '] ON ['+ @schema_name+ '].[' + @table_name + ']     (        [' + @column_name + '] ASC    ) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]    'EXEC sp_executesql @queryUPDATE @INDEX_TABLE SET     new_index_name = @index_nameWHERE     primary_key = @item_counter    SET @item_counter =  @item_counter + 1END-- present all FKey's with new indexSELECT     schema_name,     table_name,     column_name,    new_index_nameFROM     @INDEX_TABLE

 


以AdvantureWorks数据库为例,执行后返回

HumanResources

Employee

ContactID

IX_Employee_ContactID

Person

StateProvince

TerritoryID

IX_StateProvince_TerritoryID

Production

Product

ProductModelID

IX_Product_ProductModelID

Production

Product

ProductSubcategoryID

IX_Product_ProductSubcategoryID

Production

Product

SizeUnitMeasureCode

IX_Product_SizeUnitMeasureCode

Production

Product

WeightUnitMeasureCode

IX_Product_WeightUnitMeasureCode

Production

ProductSubcategory

ProductCategoryID

IX_ProductSubcategory_ProductCategoryID

Production

WorkOrderRouting

LocationID

IX_WorkOrderRouting_LocationID

Purchasing

PurchaseOrderHeader

ShipMethodID

IX_PurchaseOrderHeader_ShipMethodID

Purchasing

VendorAddress

AddressTypeID

IX_VendorAddress_AddressTypeID

Sales

CustomerAddress

AddressTypeID

IX_CustomerAddress_AddressTypeID

Sales

Individual

ContactID

IX_Individual_ContactID

Sales

SalesOrderDetail

SpecialOfferID

IX_SalesOrderDetail_SpecialOfferID

Sales

SalesOrderHeader

BillToAddressID

IX_SalesOrderHeader_BillToAddressID

Sales

SalesOrderHeader

ContactID

IX_SalesOrderHeader_ContactID

Sales

SalesOrderHeader

CreditCardID

IX_SalesOrderHeader_CreditCardID

Sales

SalesOrderHeader

CurrencyRateID

IX_SalesOrderHeader_CurrencyRateID

Sales

SalesOrderHeader

ShipMethodID

IX_SalesOrderHeader_ShipMethodID

Sales

SalesOrderHeader

ShipToAddressID

IX_SalesOrderHeader_ShipToAddressID

Sales

SalesOrderHeader

TerritoryID

IX_SalesOrderHeader_TerritoryID

Sales

SalesPerson

TerritoryID

IX_SalesPerson_TerritoryID

 

希望对您开发有帮助,您可以感兴趣的文章:

 

作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-。

你可能感兴趣的文章
[转载]服务器和应用系统迁移方案
查看>>
TESTUSERB 仅能对TESTUSERA 用户下的某些表增删改查、有些表仅能对某些列update,查询TESTUSERB 用户权限,获取批量赋予语句。...
查看>>
Algs4-1.3.12在可迭代Stack用例中写一个copy栈方法
查看>>
前端css:“圣杯布局”
查看>>
Django单表的增删改查
查看>>
mybatis plus的条件构造器
查看>>
为什么是List list = new ArrayList(),而不直接用ArrayList(转)
查看>>
Spring包导入错误
查看>>
用WIN7批处理循环获取的机的内存和CPU信息
查看>>
ajax 检测用户名是否可用
查看>>
【转】Hive的insert操作
查看>>
第九篇、微信小程序-button组件
查看>>
UITapGestureRecognizer 的用法
查看>>
python之路--MySQL 库,表的详细操作
查看>>
Android-Dialog
查看>>
每天一道博弈论之“星空”
查看>>
ECshop在文章列表页调用文章简介
查看>>
JS 的execCommand 方法 做的一个简单富文本
查看>>
PHP根据传入的经纬度,和距离范围,返回所有在距离范围内的经纬度的取值范围...
查看>>
poj 3581 Sequence(后缀数组,离散化)详解
查看>>