在 SQL Server 2005 中重命名数据库数据文件

发布于 2024-08-19 19:19:49 字数 108 浏览 2 评论 0原文

我需要重命名数据库数据文件。这可以通过 SQL Server Management Studio 实现吗?

仅供参考,我没有底层框的权限。

编辑:我还需要更改文件的位置。

I need to rename a databases data file. Is this possible through SQL Server Management Studio?

FYI, I do not have permissions to the underlying box.

Edit: I also need to change the location of the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

野稚 2024-08-26 19:19:49

是的,您可以执行此操作,只要您有权分离和重新附加数据库,并且只要您找到一种方法来物理重命名磁盘上的文件:

1) 发出这些命令

ALTER DATABASE yourdatabase
MODIFY FILE (NAME = logical_file_name, FILENAME = 'your-new-file-on-disk.mdf' )

(如 DGGenuine在评论中指出:“your-new-file-on-disk.mdf”必须是磁盘上的完整文件名 - 包括路径)

2) 分离数据库

3) 重命名磁盘上的文件

4)再次重新附加数据库

Yes, you can do this, as long as you have the right to detach and re-attach the database, and as long as you find a way to physically rename the files on disk:

1) issues these commands

ALTER DATABASE yourdatabase
MODIFY FILE (NAME = logical_file_name, FILENAME = 'your-new-file-on-disk.mdf' )

(as DGGenuine pointed out in a comment: the 'your-new-file-on-disk.mdf' must be a full file name - including path - on your disk)

2) detach the database

3) rename the files on disk

4) re-attach the database again

濫情▎り 2024-08-26 19:19:49
  1. 对数据库进行完整备份。
  2. 放下它。
  3. 恢复它,在恢复对话框中指定不同的文件名和路径。

您可以在没有底层文件系统权限的情况下执行此操作,因为您没有移动物理文件,而是要求 SQL Server 代表您生成一个新文件并将数据从备份复制到该文件中。

要设置新的文件名和路径,请转到“还原”对话框的“选项”选项卡。您甚至会看到一个文件夹浏览对话框,显示 SQL Server 的文件系统视图,而不是您的视图。

此过程所需的最低权限:

  • 数据库中的 db_backupoperator 角色
  • 服务器中的 dbcreator 角色
  1. Take a full backup of the database.
  2. Drop it.
  3. Restore it, specifying different file names and paths in the restore dialog.

You can do this with no permissions on the underlying file system, because you are not moving a physical file around, you are asking SQL Server to generate a new file on your behalf and copy data into it from the backup.

To set the new filenames and paths, go to the Options tab of the Restore dialog. You even get a folder browse dialog that shows you SQL Server's view of the file system, not yours.

Minimum permissions required for this procedure:

  • db_backupoperator role in the database
  • dbcreator role in the server
你是我的挚爱i 2024-08-26 19:19:49

尝试:

ALTER DATABASE <DBName> 
MODIFY FILE (NAME = logical_file_name, FILENAME = 'new-file-on-disk.mdf' )

这只会改变 SQL Server 文件名的内部定义,不会更改操作系统文件系统中文件的实际名称。

Try:

ALTER DATABASE <DBName> 
MODIFY FILE (NAME = logical_file_name, FILENAME = 'new-file-on-disk.mdf' )

This will only alter SQL Server's internal definition of the filename, it will not change the actual name of the file in the OS file system.

萌面超妹 2024-08-26 19:19:49

您可以使用 sp_detach_db 使数据库脱机。找到文件并重命名它们,移动它们。然后使用 sp_attach_db 从新位置重新附加它们。无论如何,我就是这样做的。

此致,
大学教师

you can use sp_detach_db to take the database offline. find the files and rename them, move them whatever. then use sp_attach_db to reattach them from a new location. that's how i do it anyway.

best regards,
don

秉烛思 2024-08-26 19:19:49

是和否。您可以更改数据库文件对象文件名:

ALTER DATABASE <dbname> MODIFY FILE (NAME=<logicalname>, FILENAME=<newfilename>);

这将更新主目录,以便在下一个数据库打开事件中首先查找新文件名。但在 Transact-SQL 中没有直接的方法来重命名/移动文件。

不过,您可以使用 xp_cmdshell 重命名文件,也可以部署启用 EXTERNAL_ACCESS 的 CLR 程序集来执行文件重命名/移动操作。

Yes and No. You can change the database file object filename:

ALTER DATABASE <dbname> MODIFY FILE (NAME=<logicalname>, FILENAME=<newfilename>);

This will update the master catalog so that at first next database open event the new filename will be looked up. But there is no direct way to rename/move the file in Transact-SQL.

You can though use xp_cmdshell to rename the file, or you can deploy a CLR assembly with EXTERNAL_ACCESS enabled that can do the file rename/move operation.

软糯酥胸 2024-08-26 19:19:49

在 SSMS 中以文本模式运行此命令,以获取将重命名所有 .ndf 文件的 T-SQL 脚本和 DOS 命令。它假设您希望文件的名称与文件组的名称相同,减去我喜欢在文件组上使用的“FG_”前缀。

干杯。

/**********************************************************************

脚本名称:重命名文件名 02.sql

目的:重命名数据库上的多个文件名。

更改历史记录:
2010 年 3 月 19 日下午 4:15 - gmilner:已创建。

****************************************************** ********************/

上设置 NOCOUNT

在DECLARE @DATABASE_NAME VARCHAR(64) SET @DATABASE_NAME = '您的数据库名称_此处'

PRINT '---------------------------------------- -----------------------------------' 打印“在单独的 SSMS 窗口中运行这些” 打印 ' - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------'

中的文件名

-- 创建 ALTER DATABASE 文件以更改 sys /* 注意:“这只会改变 SQL Server 文件名的内部定义, 它不会更改操作系统文件系统中文件的实际名称。” */

选择 '更改数据库'+ @DATABASE_NAME + CHAR(13) + ' 修改文件 (NAME = ' + [名称] + ', ' + CHAR(13) + ' 文件名 = ''' + /* 下面的行仅从整个(旧)文件名中删除路径 因此新文件被映射到与旧文件相同的位置/ SUBSTRING(物理名称,1,PATINDEX('%'+REVERSE(SUBSTRING(REVERSE(物理名称),1,CHARINDEX('\',REVERSE(物理名称))-1))+'%',物理名称)-1) + @DATABASE_NAME + '_' +
REPLACE([名称],'FG_','') + '.ndf'');' /
注意:所有文件组都以“FG”开头,但文件不应以“FG”开头。 / + CHAR(13) / 在每个命令之间放置一个空行 */ 来自 sys.master_files WHERE 数据库 ID = DB_ID(@DATABASE_NAME) AND 物理名称 LIKE '%.ndf';

-- 现在我们需要 DOS BATCH 命令来重命名实际文件。 -- 我们以管理员身份在盒子本身上运行它们

PRINT '------------------------------------ ---------------------------------------' 打印“现在,分离数据库,然后...” 打印“以管理员身份在 DOS 批处理中运行这些” 打印 ' - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------'

SELECT '重命名'' + 物理名称 + '" "' + @DATABASE_NAME + '' +
REPLACE([名称],'FG
','') + '.ndf";' /* 注意:所有文件组都以“FG”开头,但文件不应以“FG”开头 */

FROM sys.master_files 。 WHERE 数据库 ID = DB_ID(@DATABASE_NAME) AND 物理名称 LIKE '%.ndf';

打印 ' - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------' 打印“DOS 批处理运行后,重新连接数据库” 打印 ' - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------'

不设置任何折扣

Run this in SSMS in text mode to get a T-SQL script and DOS commands that will rename all your .ndf files. It assumes you want your files named the same as your filegroups minus a 'FG_' prefix that I like to use on my filegroups.

Cheers.


/**********************************************************************

SCRIPT NAME: Rename FileNames 02.sql

PURPOSE: Rename multiple filenames on the database.

Change History:
03/19/2010 4:15 PM - gmilner: Created.

**********************************************************************/

SET NOCOUNT ON

DECLARE @DATABASE_NAME VARCHAR(64)
SET @DATABASE_NAME = 'YOUR_DATABASE_NAME_HERE'

PRINT '----------------------------------------------------------------------------'
PRINT ' RUN THESE IN A SEPARATE SSMS WINDOW'
PRINT '----------------------------------------------------------------------------'

-- create the ALTER DATABASE files to change the file names in the sys

/*
NOTE: "This will only alter SQL Server's internal definition of the filename,
it will not change the actual name of the file in the OS file system."
*/

SELECT
'ALTER DATABASE '+ @DATABASE_NAME + CHAR(13) +
' MODIFY FILE (NAME = ' + [name] + ', ' + CHAR(13) + ' FILENAME = ''' +
/* the line below strips the path only from the whole (old) filename
so the new files are mapped to the same place as the old /
SUBSTRING(physical_name,1,PATINDEX('%'+REVERSE(SUBSTRING(REVERSE(physical_name),1,CHARINDEX('\',REVERSE(physical_name))-1))+'%' ,physical_name)-1) +
@DATABASE_NAME + '_' +
REPLACE([name],'FG_','') + '.ndf'');' /
NOTE: all filegroups start with 'FG' but files should not. /
+ CHAR(13) /
put in a blank line between each command */
FROM sys.master_files
WHERE database_id = DB_ID(@DATABASE_NAME)
AND physical_name LIKE '%.ndf';

-- Now we need DOS BATCH commandS to rename the actual files.
-- We run them as Administrator on the box itself

PRINT '----------------------------------------------------------------------------'
PRINT ' NOW, DETACH THE DATABASE AND THEN ... '
PRINT ' RUN THESE IN A DOS BATCH AS ADMINISTRATOR'
PRINT '----------------------------------------------------------------------------'

SELECT 'rename "' + physical_name +
'" "' +
@DATABASE_NAME + '' +
REPLACE([name],'FG
','') + '.ndf";' /* NOTE: all filegroups start with 'FG' but files should not. */

FROM sys.master_files
WHERE database_id = DB_ID(@DATABASE_NAME)
AND physical_name LIKE '%.ndf';

PRINT '----------------------------------------------------------------------------'
PRINT ' AFTER THE DOS BATCH IS RUN, REATTACH THE DATABASE'
PRINT '----------------------------------------------------------------------------'

SET NOCOUNT OFF

绝不服输 2024-08-26 19:19:49

使用右键单击 Management Studio 中的数据库时出现的上下文菜单中的“重命名”选项时要小心。此选项不会更改数据库文件名。要更改数据和日志文件的逻辑文件名您还可以使用 Management Studio 界面,但不幸的是有时它不起作用

让我们正确执行...它应该始终有效。

  1. 分离数据库:
    使用 Management Studio,右键单击数据库 >转到“任务”> “分离”,单击确定分离数据库(注意:数据库不能用于分离它)
  2. 重命名物理文件:数据库分离后,物理文件将被解锁,您可以重命名它们使用 Windows 资源管理器:
    在此处输入图像描述
  3. 使用新名称附加数据库: 为此,请使用 T SQL:

    使用[主控]
    创建数据库 [SqlAndMe] 上
    ( FILENAME = N'C:\…\NewName.mdf'),
    ( FILENAME = N'C:\…\NewName_log.LDF')
    FOR ATTACH

  4. 重命名逻辑文件名:执行此 T SQL:

    使用[新名称]
    更改数据库 [新名称]
    修改文件(名称=N'旧名称',新名称=N'新名称')
    更改数据库 [新名称]
    修改文件(名称=N'OldName_log',NEWNAME=N'NewName_log')
    选择名称,物理名称
    FROM [NewName].sys.database_files

它应该可以工作!

Be careful when you use Rename option from context menu that appear when you right click on the database in Management Studio. This option does not change the database file names. To change logical filenames for DATA and LOG files you can also use a Management Studio interface but unfortunately sometimes it does not work.

Let’s do it properly... it should works always.

  1. Detach database:
    Using Management Studio, right-click on database > go to “Tasks” > “Detach”, click OK to detach a database (note: that the DB can not be used to detach it)
  2. Rename Physical files: Once the database is detached the physical files are unlocked and you can rename them using Windows Explorer:
    enter image description here
  3. Attaching database with New Name: For it use T SQL:

    USE [master]
    CREATE DATABASE [SqlAndMe] ON
    ( FILENAME = N’C:\…\NewName.mdf’),
    ( FILENAME = N’C:\…\NewName_log.LDF’)
    FOR ATTACH

  4. Rename Logical file names: Execute this T SQL:

    USE [NewName]
    ALTER DATABASE [NewName]
    MODIFY FILE (NAME=N’OldName’, NEWNAME=N’NewName’)
    ALTER DATABASE [NewName]
    MODIFY FILE (NAME=N’OldName_log’, NEWNAME=N’NewName_log’)
    SELECT name, physical_name
    FROM [NewName].sys.database_files

It should works!

窝囊感情。 2024-08-26 19:19:49

以下是 marc_s 答案的一些示例代码:

将数据库 Test 重命名为 TestSimple 后,我想重命名其文件。
以下内容适用于日志文件。 (对于主文件,NAME = 部分变成了 Test,我用我想要的路径替换了该文件。)

USE [master]
ALTER DATABASE [TestSimple] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [TestSimple]
MODIFY FILE (NAME = Test_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\TestSimple_log.ldf' )
GO
ALTER DATABASE TestSimple SET MULTI_USER
GO

上面更改了 SSMS 查找的文件,但它实际上并没有重命名这些文件。所以我必须通过 Windows 来做到这一点。

Here is some example code for the answer of marc_s:

After renaming my database Test, to TestSimple, I wanted to rename its files.
The following worked for the log file. (For the main file, the NAME = part became Test, and I substituted the path I wanted for that file.)

USE [master]
ALTER DATABASE [TestSimple] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [TestSimple]
MODIFY FILE (NAME = Test_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\TestSimple_log.ldf' )
GO
ALTER DATABASE TestSimple SET MULTI_USER
GO

The above changed what files SSMS looks for, but it does not actually rename the files. So I had to do that via Windows.

忆梦 2024-08-26 19:19:49

重命名数据库后,如果我们还想更改数据和日志文件名称,我们可以使用以下步骤:

1.

USE master;  
GO  
ALTER DATABASE TESTDB
Modify Name = Northwind ;  
GO 

2.

ALTER DATABASE Northwind SET OFFLINE; 

ALTER DATABASE Northwind MODIFY FILE ( NAME = TESTDB, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Northwind.mdf' );
ALTER DATABASE Northwind MODIFY FILE ( NAME = TESTDB_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Northwind_log.ldf' );

3.

更改 Windows Server 中的文件名

4.

ALTER DATABASE Northwind SET ONLINE;  

After we rename the database, if we want to change data and logfile name also, we could use following step:

1.

USE master;  
GO  
ALTER DATABASE TESTDB
Modify Name = Northwind ;  
GO 

2.

ALTER DATABASE Northwind SET OFFLINE; 

ALTER DATABASE Northwind MODIFY FILE ( NAME = TESTDB, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Northwind.mdf' );
ALTER DATABASE Northwind MODIFY FILE ( NAME = TESTDB_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Northwind_log.ldf' );

3.

Change filename in windows server

4.

ALTER DATABASE Northwind SET ONLINE;  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文