什么命令可以修改数据库和表的名字?

iamdan 2003-10-17 05:38:58
什么命令可以修改数据库和表的名字?请举例!

是不是 ALTER DATABASE MODIFY NAME=newTab 和ALTER TABEL MODIFY NAME...
有问题呀
...全文
596 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
银狐被占用 2003-10-18
  • 打赏
  • 举报
回复
学习!
yun198183 2003-10-18
  • 打赏
  • 举报
回复
这个这么多人,我都没有机会了
gmlxf 2003-10-18
  • 打赏
  • 举报
回复
用系统存储过程:

重命名数据库名:
exec sp_renamedb 'db1,'db2'
重命名表
exec sp_rename 'table1, 'table2'
重命名列
exec sp_rename 'table.a', 'b', 'column'
hdslah 2003-10-18
  • 打赏
  • 举报
回复
sp_rename
sp_renamedb
---涛声依旧--- 2003-10-18
  • 打赏
  • 举报
回复
sp_renamedb
Changes the name of a database.

Syntax
sp_renamedb [ @dbname = ] 'old_name' ,
[ @newname = ] 'new_name'

Arguments
[@dbname =] 'old_name'

Is the current name of the database. old_name is sysname, with no default.

[@newname =] 'new_name'

Is the new name of the database. new_name must follow the rules for identifiers. new_name is sysname, with no default.

Return Code Values
0 (success) or a nonzero number (failure)

Permissions
Only members of the sysadmin and dbcreator fixed server roles can execute sp_renamedb.

Examples
This example changes the name of the accounting database to financial.

EXEC sp_renamedb 'accounting', 'financial'

sp_rename
Changes the name of a user-created object (for example, table, column, or user-defined data type) in the current database.

Syntax
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]

Arguments
[@objname =] 'object_name'

Is the current name of the user object (table, view, column, stored procedure, trigger, default, database, object, or rule) or data type. If the object to be renamed is a column in a table, object_name must be in the form table.column. If the object to be renamed is an index, object_name must be in the form table.index. object_name is nvarchar(776), with no default.

[@newname =] 'new_name'

Is the new name for the specified object. new_name must be a one-part name and must follow the rules for identifiers. newname is sysname, with no default.

[@objtype =] 'object_type'

Is the type of object being renamed. object_type is varchar(13), with a default of NULL, and can be one of these values.

Value Description
COLUMN A column to be renamed.
DATABASE A user-defined database. This option is required when renaming a database.
INDEX A user-defined index.
OBJECT An item of a type tracked in sysobjects. For example, OBJECT could be used to rename objects including constraints (CHECK, FOREIGN KEY, PRIMARY/UNIQUE KEY), user tables, views, stored procedures, triggers, and rules.
USERDATATYPE A user-defined data type added by executing sp_addtype.


Return Code Values
0 (success) or a nonzero number (failure)

Remarks
You can change the name of an object or data type in the current database only. The names of most system data types and system objects cannot be changed.

When you rename a view, information about the view is updated in the sysobjects table. When you rename a stored procedure, information about the procedure is changed in the sysobjects table.

sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the primary key is also automatically renamed by sp_rename.



Important After renaming stored procedures and views, flush the procedure cache to ensure all dependent stored procedures and views are recompiled.


Stored procedures and views can be dropped and re-created quickly because neither object stores data. For best results renaming textual objects, drop and re-create the object by its new name.

Permissions
Members of the sysadmin fixed server role, the db_owner and db_ddladmin fixed database roles, or the owner of the object can execute sp_rename. Only members of the sysadmin and dbcreator fixed server roles can execute sp_rename with 'database' as the object_type.

Examples
A. Rename a table
This example renames the customers table to custs.

EXEC sp_rename 'customers', 'custs'

B. Rename a column
This example renames the contact title column in the customers table to title.

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'

qgj99 2003-10-18
  • 打赏
  • 举报
回复
我是一个新手 ,想问一下如何建立存储过程,上面用来改变数据库和表的名字的代码在
那里可以写进去,就是在哪里来实现呀!
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
sp_renamedb '旧名','新名'
or
alter database 旧名字 modify name=新名字

A. 重命名表
下例将表 customers 重命名为 custs。

EXEC sp_rename 'customers', 'custs'

B. 重命名列
下例将表 customers 中的列 contact title 重命名为 title。

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'
sdhdy 2003-10-17
  • 打赏
  • 举报
回复
sp_renamedb '旧名','新名'
or
alter database 旧名字 modify name=新名字

重命名表
下例将表 customers 重命名为 custs。

EXEC sp_rename 'dbo.customers', 'custs'


qdubit 2003-10-17
  • 打赏
  • 举报
回复
同意letsflytogether(恨!不能拥有天下所有的财富,然后平分)的说法,已经很详细了,我就不再多说了。
berylw 2003-10-17
  • 打赏
  • 举报
回复
同意楼上的
伍子V5 2003-10-17
  • 打赏
  • 举报
回复
sp_renamedb
更改数据库的名称。

语法
sp_renamedb [ @dbname = ] 'old_name' ,
[ @newname = ] 'new_name'

参数
[@dbname =] 'old_name'

是数据库的当前名称。old_name 为 sysname 类型,无默认值。

[@newname =] 'new_name'

是数据库的新名称。new_name 必须遵循标识符规则。new_name 为 sysname 类型,无默认值。

返回代码值
0(成功)或非零数字(失败)

权限
只有 sysadmin 和 dbcreator 固定服务器角色的成员才能执行 sp_renamedb。

示例
下例将数据库 accounting 改名为 financial。

EXEC sp_renamedb 'accounting', 'financial'




sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。

语法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]

参数
[@objname =] 'object_name'

是用户对象(表、视图、列、存储过程、触发器、默认值、数据库、对象或规则)或数据类型的当前名称。如果要重命名的对象是表中的一列,那么 object_name 必须为 table.column 形式。如果要重命名的是索引,那么 object_name 必须为 table.index 形式。object_name 为 nvarchar(776) 类型,无默认值。

[@newname =] 'new_name'

是指定对象的新名称。new_name 必须是名称的一部分,并且要遵循标识符的规则。newname 是 sysname 类型,无默认值。

[@objtype =] 'object_type'

是要重命名的对象的类型。object_type 为 varchar(13) 类型,其默认值为 NULL,可取下列值。

值 描述
COLUMN 要重命名的列。
DATABASE 用户定义的数据库。要重命名数据库时需用此选项。
INDEX 用户定义的索引。
OBJECT 在 sysobjects 中跟踪的类型的项目。例如,OBJECT 可用来重命名约束(CHECK、FOREIGN KEY、PRIMARY/UNIQUE KEY)、用户表、视图、存储过程、触发器和规则等对象。
USERDATATYPE 通过执行 sp_addtype 而添加的用户定义数据类型。


返回代码值
0(成功)或非零数字(失败)

注释
只能更改当前数据库中的对象名称或数据类型名称。大多数系统数据类型和系统对象的名称不能更改。

重命名视图时,sysobjects 表中有关该视图的信息将得到更新。重命名存储过程时,sysobjects 表中有关该过程的信息将得到更新。

每当重命名 PRIMARY KEY 或 UNIQUE 约束时,sp_rename 都会自动为相关联的索引重命名。如果重命名的索引与 PRIMARY KEY 约束相关联,那么 sp_rename 也会自动重命名主键。



重要 重命名存储过程和视图后,请清空过程高速缓存以确保所有相关的存储过程和视图都重新编译。


由于存储过程和视图都不存储数据,所以这两种对象均可快速删除和重建。重命名文本对象时,要获得最佳结果,应删除并使用其新名称重新创建对象。

权限
sysadmin 固定服务器角色成员、db_owner 和 db_ddladmin 固定数据库角色成员或对象所有者可以执行 sp_rename。只有 sysadmin 和 dbcreator 固定服务器角色成员才能将"database"作为 object_type 来执行 sp_rename。

示例
A. 重命名表
下例将表 customers 重命名为 custs。

EXEC sp_rename 'customers', 'custs'

B. 重命名列
下例将表 customers 中的列 contact title 重命名为 title。

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'
yujohny 2003-10-17
  • 打赏
  • 举报
回复
下例将数据库 accounting 改名为 financial。

EXEC sp_renamedb 'accounting', 'financial'



A. 重命名表
下例将表 customers 重命名为 custs。

EXEC sp_rename 'customers', 'custs'

B. 重命名列
下例将表 customers 中的列 contact title 重命名为 title。

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧