数据库为原名称为DB_BEIJING,需要修改成DB_SHANGHAI
nzperfect 2012.12.19
*/
--判断是否存在同名的数据库,以防止误删除
USE master
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'DB_BEIJING')
BEGIN
RAISERROR('请注意:数据库已存在!',15,1)
RETURN
--DROP DATABASE DB_BEIJING
END
GO
USE master
GO
--创建测试数库
CREATE DATABASE [DB_BEIJING]
ON PRIMARY
(
NAME = N'DB_BEIJING',
FILENAME = N'X:\DATA\DB_BEIJING.mdf' ,
SIZE = 3072KB ,
FILEGROWTH = 1024KB
)
LOG ON
(
NAME = N'DB_BEIJING_log',
FILENAME = N'W:\Log\DB_BEIJING_log.ldf' ,
SIZE = 1024KB ,
FILEGROWTH = 1024KB
)
GO
--以下为修改过程
--step 1 : 修改数据库名称
USE master
GO
ALTER DATABASE DB_BEIJING SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
EXEC master..sp_renamedb 'DB_BEIJING','DB_SHANGHAI'
GO
ALTER DATABASE DB_SHANGHAI SET MULTI_USER
GO
--step 2 : 查看修改名称后的数据库逻辑名及物理文件名
USE master
GO
SELECT name AS [Logical Name], physical_name AS [DB File Path],type_desc AS [File Type],
state_desc AS [State] FROM sys.master_files
WHERE database_id = DB_ID(N'DB_SHANGHAI')
GO
/*
Logical NameDB File PathFile TypeState
DB_BEIJINGX:\DATA\DB_BEIJING.mdfROWSONLINE
DB_BEIJING_logW:\Log\DB_BEIJING_log.ldfLOGONLINE
*/
--step 3 : 修改数据库逻辑文件名称
USE master
GO
ALTER DATABASE DB_SHANGHAI SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE DB_SHANGHAI MODIFY FILE (NAME=N'DB_BEIJING', NEWNAME=N'DB_SHANGHAI')
GO
ALTER DATABASE DB_SHANGHAI MODIFY FILE (NAME=N'DB_BEIJING_log', NEWNAME=N'DB_SHANGHAI_log')
GO
ALTER DATABASE DB_SHANGHAI SET MULTI_USER
GO
--step 4 : 修改数据库物理文件名称之前先打开xp_cmdshell支持
USE master
GO
sp_configure 'show advanced options',1
GO
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE WITH OVERRIDE
GO
--step 5 : 重命名数据库物理文件名称
USE [master]
GO
ALTER DATABASE DB_SHANGHAI SET OFFLINE WITH ROLLBACK IMMEDIATE
GO
EXEC xp_cmdshell 'RENAME "X:\DATA\DB_BEIJING.mdf", "DB_SHANGHAI.mdf"'
GO
EXEC xp_cmdshell 'RENAME "W:\Log\DB_BEIJING_log.ldf", "DB_SHANGHAI_log.ldf"'
GO
--step 6 : 将数据库逻辑名称指向新的物理文件,并将数据库online
USE [master]
GO
ALTER DATABASE DB_SHANGHAI MODIFY FILE (NAME =DB_SHANGHAI, FILENAME = 'X:\DATA\DB_SHANGHAI.mdf')
GO
ALTER DATABASE DB_SHANGHAI MODIFY FILE (NAME =DB_SHANGHAI_log, FILENAME = 'W:\LOG\DB_SHANGHAI_log.ldf')
GO
ALTER DATABASE DB_SHANGHAI SET ONLINE
--step 7 : 查看全部修改完成后的数据库情况
USE master
GO
SELECT name AS [Logical Name], physical_name AS [DB File Path],type_desc AS [File Type],
state_desc AS [State] FROM sys.master_files
WHERE database_id = DB_ID(N'DB_SHANGHAI')
GO
/*
Logical NameDB File PathFile TypeState
DB_SHANGHAIX:\DATA\DB_SHANGHAI.mdfROWSONLINE
DB_SHANGHAI_logW:\LOG\DB_SHANGHAI_log.ldfLOGONLINE
*/
--step 8 : 关闭xp_cmdshell支持
USE master
GO
sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'show advanced options',0
GO
RECONFIGURE WITH OVERRIDE
GO
用alter语句进行修改。语法:alter table 表名 modify 字段名 字段类型(字段长度)说明:如果是date等没有长度的类型,字段长度部分可以省略。如:目前test表属性如下要将name列的字段类型改为date类型,可用如下语句:alter table test modify name date此时可见name列的类型已经更新成功。注意事项:如果表中有数据尽量不要使用此语句,会造成数据丢失,应在备份的情况下进行修改。方法如下:\x0d\x0a1、打开要修改的数据库文件。\x0d\x0a2、打开sql查询\x0d\x0a3、在sql中输入修改语句。要用update这个语句。update语句的具体用法如下:\x0d\x0aSQL Server UPDATE语句的语法如下:\x0d\x0aupdate set = where \x0d\x0a\x0d\x0a下面是语法选项简介:\x0d\x0a表的名称。该表包含了要修改值的列\x0d\x0a要修改数据的列的名称\x0d\x0a要输入到列中的新值\x0d\x0a这是UPDATE语句中最重要的部分。\x0d\x0a通过指定一个好的搜索条件,你能够限定表内被修改的行数。如果你不指定搜索条件,SQLServer会用新值修改表内的所有行\x0d\x0a4、修改完成后,关闭数据库文件即可。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)