sp_rename
2024-08-26 21:33:37
sp_rename
在当前数据库中更改用户创建对象的名称。 此对象可以是表、索引、列、别名数据类型或 Microsoft .NET Framework 公共语言运行时 (CLR) 用户定义类型。
更改对象名的任一部分都可能破坏脚本和存储过程。 我们建议您不要使用此语句来重命名存储过程、触发器、用户定义函数或视图;而是删除该对象,然后使用新名称重新创建该对象。
适用范围:SQL Server(SQL Server 2008 至当前版本),Windows Azure SQL Database(初始版本至当前版本)。
sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]
A.重命名表
下面的示例将 Sales 架构中的 SalesTerritory 表重命名为 SalesTerr。
USE AdventureWorks2012;
GO
EXEC sp_rename 'Sales.SalesTerritory', 'SalesTerr';
GO
B.重命名列
下面的示例将 SalesTerritory 表中的 TerritoryID 列重命名为 TerrID。
USE AdventureWorks2012;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO
C.重命名索引
下面的示例将 IX_ProductVendor_VendorID 索引重命名为 IX_VendorID。
USE AdventureWorks2012;
GO
EXEC sp_rename N'Purchasing.ProductVendor.IX_ProductVendor_VendorID', N'IX_VendorID', N'INDEX';
GO
D.重命名别名数据类型
下面的示例将 Phone 别名数据类型重命名为 Telephone。
USE AdventureWorks2012;
GO
EXEC sp_rename N'Phone', N'Telephone', N'USERDATATYPE';
GO
E.重命名约束
下面的示例重命名 PRIMARY KEY 约束、CHECK 约束和 FOREIGN KEY 约束。 重命名约束时,必须指定约束所属的架构。
USE AdventureWorks2012;
GO
-- Return the current Primary Key, Foreign Key and Check constraints for the Employee table.
SELECT name, SCHEMA_NAME(schema_id) AS schema_name, type_desc
FROM sys.objects
WHERE parent_object_id = (OBJECT_ID('HumanResources.Employee'))
AND type IN ('C','F', 'PK');
GO -- Rename the primary key constraint.
sp_rename 'HumanResources.PK_Employee_BusinessEntityID', 'PK_EmployeeID';
GO -- Rename a check constraint.
sp_rename 'HumanResources.CK_Employee_BirthDate', 'CK_BirthDate';
GO -- Rename a foreign key constraint.
sp_rename 'HumanResources.FK_Employee_Person_BusinessEntityID', 'FK_EmployeeID'; -- Return the current Primary Key, Foreign Key and Check constraints for the Employee table.
SELECT name, SCHEMA_NAME(schema_id) AS schema_name, type_desc
FROM sys.objects
WHERE parent_object_id = (OBJECT_ID('HumanResources.Employee'))
AND type IN ('C','F', 'PK');
name schema_name type_desc
------------------------------------- ------------------ ----------------------
FK_Employee_Person_BusinessEntityID HumanResources FOREIGN_KEY_CONSTRAINT
PK_Employee_BusinessEntityID HumanResources PRIMARY_KEY_CONSTRAINT
CK_Employee_BirthDate HumanResources CHECK_CONSTRAINT
CK_Employee_MaritalStatus HumanResources CHECK_CONSTRAINT
CK_Employee_HireDate HumanResources CHECK_CONSTRAINT
CK_Employee_Gender HumanResources CHECK_CONSTRAINT
CK_Employee_VacationHours HumanResources CHECK_CONSTRAINT
CK_Employee_SickLeaveHours HumanResources CHECK_CONSTRAINT (7 row(s) affected) name schema_name type_desc
------------------------------------- ------------------ ----------------------
FK_Employee_ID HumanResources FOREIGN_KEY_CONSTRAINT
PK_Employee_ID HumanResources PRIMARY_KEY_CONSTRAINT
CK_BirthDate HumanResources CHECK_CONSTRAINT
CK_Employee_MaritalStatus HumanResources CHECK_CONSTRAINT
CK_Employee_HireDate HumanResources CHECK_CONSTRAINT
CK_Employee_Gender HumanResources CHECK_CONSTRAINT
CK_Employee_VacationHours HumanResources CHECK_CONSTRAINT
CK_Employee_SickLeaveHours HumanResources CHECK_CONSTRAINT (7 row(s) affected)
F.重命名统计信息
下面的示例创建一个名为 contactMail1 的统计信息对象,然后使用 sp_rename 将统计信息重命名为 NewContact。重命名统计信息时,必须采用格式 schema.table.statistics_name 指定该对象。
适用范围:SQL Server(SQL Server 2012 到 SQL Server 2014、Windows Azure SQL Database)。 |
CREATE STATISTICS ContactMail1
ON Person.Person (BusinessEntityID, EmailPromotion)
WITH SAMPLE 5 PERCENT; sp_rename 'Person.Person.ContactMail1', 'NewContact','Statistics';
最新文章
- Js 拖动效果
- Reverse Bits
- HTML的表格标签
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法
- Mac OS X中开启或关闭显示隐藏文件
- 从零开始学android开发-字符如何转换整形 string 转化为int
- MySQL for Excel用法
- ubuntu 下的 ftp (gftp)
- iostat 使用说明
- 在CentOS 7上利用systemctl加入自己定义系统服务
- 如何用VB获得Windows各类系统目录
- MySQL数据目录更改及相关问题解决方案
- laravel 事件监听
- FreeType in OpenCASCADE
- 已实现乐观锁功能,FreeSql.DbContext 准备起航
- H3C交换机引发的奇葩故障
- diff 命令用法--如何打补丁【原创--学习笔记】
- 如何将QT的pro图标修改的更显著一些
- C#中利用LightningChart绘制曲线图表
- 【LeetCode】214. Shortest Palindrome
热门文章
- CentOS6.4 配置Tengine
- CentOS6.4 配置DNS服务器
- Codeforces Round #208 (Div. 2) A.Dima and Continuous Line
- 洛谷 P1009 阶乘之和 Label:高精度
- ARC指南3 - @property
- Lucene搜索方法总结
- git统计报告
- The P4 Language Specification v1.0.2 Parser
- Python装饰器与面向切面编程
- 20145317彭垚 《Java程序设计》第8周学习总结