要重命名主鍵簡單地PK_TableName
:
CREATE PROCEDURE dbo.Rename_PrimaryKeys
@PrintOnly BIT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + CHAR(13) + CHAR(10) + 'EXEC sp_rename '''
+ REPLACE(name, '''', '''''') + ''', ''PK_'
+ REPLACE(OBJECT_NAME(parent_object_id), '''', '') + ''', ''OBJECT'';'
FROM sys.key_constraints
WHERE type = 'PK'
AND name <> 'PK_' + REPLACE(OBJECT_NAME(parent_object_id), '''', '')
AND OBJECTPROPERTY(parent_object_id, 'IsMsShipped') = 0;
PRINT @sql;
IF @PrintOnly = 0 AND @sql > N''
BEGIN
EXEC sp_executesql @sql;
END
END
GO
同意這項計劃FK_TableName_col_col_ReferencedName_col_col
重命名FKS:
CREATE PROCEDURE dbo.Rename_ForeignKeys_WithColumns
@PrintOnly BIT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + CHAR(13) + CHAR(10)
+ 'EXEC sp_rename ''' + REPLACE(name, '''', '''''')
+ ''', ''FK_' + REPLACE(OBJECT_NAME(fk.parent_object_id), '''', '')
+ '_' + STUFF((SELECT '_' + REPLACE(c.name, '''', '')
FROM sys.columns AS c
INNER JOIN sys.foreign_key_columns AS fkc
ON fkc.parent_column_id = c.column_id
AND fkc.parent_object_id = c.[object_id]
WHERE fkc.constraint_object_id = fk.[object_id]
ORDER BY fkc.constraint_column_id
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
+ '_' + REPLACE(OBJECT_NAME(fk.referenced_object_id), '''', '')
+ '_' + STUFF((SELECT '_' + REPLACE(c.name, '''', '')
FROM sys.columns AS c
INNER JOIN sys.foreign_key_columns AS fkc
ON fkc.referenced_column_id = c.column_id
AND fkc.referenced_object_id = c.[object_id]
WHERE fkc.constraint_object_id = fk.[object_id]
ORDER BY fkc.constraint_column_id
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
+ ''', ''OBJECT'';'
FROM sys.foreign_keys AS fk
WHERE OBJECTPROPERTY(parent_object_id, 'IsMsShipped') = 0;
PRINT @sql;
IF @PrintOnly = 0 AND @sql > N''
BEGIN
EXEC sp_executesql @sql;
END
END
GO
外鍵,如果你只是想FK_TableName_ReferencedName
那麼它是一個簡單多了:
CREATE PROCEDURE dbo.Rename_ForeignKeys
@PrintOnly BIT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + CHAR(13) + CHAR(10) + 'EXEC sp_rename '''
+ REPLACE(name, '''', '''''') + ''', ''FK_'
+ REPLACE(OBJECT_NAME(parent_object_id), '''', '')
+ '_' + REPLACE(OBJECT_NAME(referenced_object_id), '''', '')
+ ''', ''OBJECT'';'
FROM sys.foreign_keys
WHERE OBJECTPROPERTY(parent_object_id, 'IsMsShipped') = 0;
PRINT @sql;
IF @PrintOnly = 0 AND @sql > N''
BEGIN
EXEC sp_executesql @sql;
END
END
GO
對於指標,這將重命名任何我索引IX_TableName_Col1_Col2...
。它會忽略主鍵(因爲它們在上面單獨處理),將添加UQ_
到唯一索引/約束(因此IX_UQ_TableName_Col1_Col2...
,將把唯一約束和唯一索引視爲相同,並且將忽略包含的列(注意,忽略包含的列可能會生產,如果你有多餘的指標,只有包含的列不同的命名衝突)
CREATE PROCEDURE dbo.Rename_Indexes
@PrintOnly BIT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + CHAR(13) + CHAR(10)
+ 'EXEC sp_rename ''' + REPLACE(i.name, '''', '''''')
+ ''', ''IX_' + CASE is_unique_constraint WHEN 1 THEN 'UQ_' ELSE '' END
+ REPLACE(OBJECT_NAME(i.[object_id]), '''', '')
+ '_' + STUFF((SELECT '_' + REPLACE(c.name, '''', '')
FROM sys.columns AS c
INNER JOIN sys.index_columns AS ic
ON ic.column_id = c.column_id
AND ic.[object_id] = c.[object_id]
WHERE ic.[object_id] = i.[object_id]
AND ic.index_id = i.index_id
AND is_included_column = 0
ORDER BY ic.index_column_id
FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
+''', ''OBJECT'';'
FROM sys.indexes AS i
WHERE index_id > 0
AND is_primary_key = 0 -- dealt with separately
AND OBJECTPROPERTY(i.[object_id], 'IsMsShipped') = 0;
PRINT @sql;
IF @PrintOnly = 0 AND @sql > N''
BEGIN
EXEC sp_executesql @sql;
END
END
GO
對於默認約束:
CREATE PROCEDURE dbo.Rename_DefaultConstraints
@PrintOnly BIT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + CHAR(13) + CHAR(10)
+ 'EXEC sp_rename ''' + REPLACE(dc.name, '''', '''''')
+ ''', ''DF_' + REPLACE(OBJECT_NAME(dc.parent_object_id), '''','')
+ '_' + REPLACE(c.name, '''', '') + ''', ''OBJECT'';'
FROM sys.default_constraints AS dc
INNER JOIN sys.columns AS c
ON dc.parent_object_id = c.[object_id]
AND dc.parent_column_id = c.column_id
AND OBJECTPROPERTY(dc.parent_object_id, 'IsMsShipped') = 0;
PRINT @sql;
IF @PrintOnly = 0 AND @sql > N''
BEGIN
EXEC sp_executesql @sql;
END
END
GO
最後檢查約束:
CREATE PROCEDURE dbo.Rename_CheckConstraints
@PrintOnly BIT = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + CHAR(13) + CHAR(10)
+ 'EXEC sp_rename ''' + REPLACE(cc.name, '''', '''''')
+ ''', ''CK_' + REPLACE(OBJECT_NAME(cc.parent_object_id), '''','')
+ '_' + REPLACE(c.name, '''', '') + ''', ''OBJECT'';'
FROM sys.check_constraints AS cc
INNER JOIN sys.columns AS c
ON cc.parent_object_id = c.[object_id]
AND cc.parent_column_id = c.column_id
AND OBJECTPROPERTY(dc.parent_object_id, 'IsMsShipped') = 0;
PRINT @sql;
IF @PrintOnly = 0 AND @sql > N''
BEGIN
EXEC sp_executesql @sql;
END
END
GO
注意PRINT
不一定會揭示整個語句,這取決於你在文本和語句的大小結果設置。但它應該足以讓眼睛看到腳本正在做正確的工作。我默認將它們全部設置爲PrintOnly
。
您可以指定更多的規則,例如如果您的索引被過濾或具有INCLUDE列,那麼您是否也要指定這些列,或者只是鍵列?檢查約束和唯一約束呢? –
不能說我有,但我從來沒有使用GUI來創建東西,我總是在腳本中給我的東西名稱。你可以做上面的事情,但我認爲編寫數據庫並編輯它以獲得你想要的內容比編制上述內容更容易。 –
另一個想法是如何處理名稱中帶下劃線的對象。在'Document_Folder'有一個名爲'Folder_Name'的列被'Document's'Folder_Name'列引用的情況下,讀取外鍵的對象名稱會非常令人困惑。 'FK_Document_Folder_Name_Document_Folder_Folder_Name'?我不確定這比系統生成的名稱'FK__Document__Docum__1DE57479'更有用... –