简体   繁体   English

在SQL Server 2012 Express中创建数据库副本

[英]Create copy of database in sql server 2012 express

Hello I have database on which user work. 您好,我有用户在使用的数据库。 I want to create copy of this database for testing purpose. 我想创建该数据库的副本以进行测试。

Can anyone tell how can this be done? 谁能告诉我该怎么做? I tried everything from copy a database within SQL Server Express? 我试过一切,从SQL Server Express中复制数据库?

But Copying files and attaching it destroys first database and trying to restore backup into new database throws error that database is not the same. 但是复制文件并附加它会破坏第一个数据库,并尝试将备份还原到新数据库中,从而引发错误,指出数据库不一样。

Can anyone suggest me solution? 谁能建议我解决方案?

as @Nadeem_MK suggested I created backup and now I'm trying to restore with this script: 正如@Nadeem_MK所建议的,我创建了备份,现在我正尝试使用以下脚本进行还原:

RESTORE DATABASE [Equipment Test]  -- use your new db name
FROM  DISK = N'C:\Backup\ExistingDb.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.mdf',  --logical name and physical name 
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.ldf' --logical name and physical name

and this throws error: 这会引发错误:

Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf' cannot be overwritten.  It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 1

The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf' cannot be overwritten.  It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

As you can see restores is trying to alter files from backup-ed database instead of new database 如您所见,还原正在尝试更改备份数据库(而非新数据库)中的文件

@Update: BackUp Script: @Update:备份脚本:

--To backup Database (Delete the previous backup first)
BACKUP DATABASE Equipment --Database Name
TO DISK = 'C:\Backup\Equipment.bak' WITH INIT --Path of backup location

file locations: 文件位置:

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test_log.mdf

Restore function: 恢复功能:

RESTORE DATABASE [Equipment Test]  -- use your new db name
FROM  DISK = N'C:\Backup\Equipment.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf',  --logical name and physical name 
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name

You can create a backup and restore it using the following script; 您可以使用以下脚本创建备份并还原它;

--To backup Database (Delete the previous backup first)
BACKUP DATABASE ExistingDb --Database Name
TO DISK = 'C:\DBBackup\ExistingDb.bak' WITH INIT --Path of backup location

Then restore it; 然后恢复它;

RESTORE DATABASE NewDbName  -- use your new db name
FROM  DISK = N'C:\DBBackup\ExistingDb.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Reporting' TO N'C:\Databases\MDF\NewDbName.mdf',  --logical name and physical name 
MOVE N'Reporting_log' TO N'C:\Databases\LDF\NewDbName.ldf' --logical name and physical name

Assuming this are the paths of the original database: 假设这是原始数据库的路径:

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf

and the filename of your backup is called 备份的文件名称为

Equipment.bak

Make sure that the filenames for the new database aren't the same as the old one. 确保新数据库的文件名与旧数据库的文件名不同。 That is what the errors you are getting means: 那就是你得到的错误的意思:

The file 'C:\...' cannot be overwritten.  It is being used by database 'Equipment'.

Explanation: Don't use filenames of files you already have! 说明:不要使用已有文件的文件名!

Instead, give these a new name. 而是给这些起一个新的名字。 Like this: 像这样:

RESTORE DATABASE [Equipment Test]  -- use your new db name
FROM  DISK = N'C:\Backup\Equipment.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf',  --logical name and physical name 
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name

This is basically Nadeem_MK's answer, but instead of your original names Equipment.mdf and Equipment_log.ldf , now you should have Equipment2.mdf and Equipment_log.ldf 这基本上是Nadeem_MK的答案,但是现在您应该有了Equipment2.mdfEquipment_log.ldf ,而不是原来的名称Equipment.mdfEquipment_log.ldf

If this works, please also opvote Nadeem_MK's answer, as this is basically what he was saying. 如果这可行,也请反对Nadeem_MK的回答,因为这基本上是他在说的。 I just don't have enough reputation yet to comment on his answer. 我只是没有足够的声誉来评论他的答案。

Simple, Do these steps: 简单,请执行以下步骤:

  1. Take backup of your primary database. 备份主数据库。

  2. Create new database with whatever name you want to give it for testing. 创建新数据库,并使用您想要提供的名称进行测试。

  3. Restore backup of your primary database to your newly created testing database. 将主数据库的备份还原到新创建的测试数据库。

Done!! 完成!

Note: The functionality below is not available in SQL Express I have left the answer here for completeness for future readers only. 注意:下面的功能在SQL Express中不可用。为了完整起见,我在这里留下了答案,仅供以后的读者参考。

The simplest method I can think of is to let Management Studio do all the work! 我能想到的最简单的方法是让Management Studio完成所有工作!

Right-click the database you wish to copy and look for the Copy Database... option under the Tasks menu. 右键单击要复制的数据库,然后在“ 任务”菜单下查找“ 复制数据库...”选项。

复制数据库任务

为什么不编写数据库脚本并运行脚本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM