MS SQL 2008 - Create a copy of the database without the data

前端 未结 4 617
甜味超标
甜味超标 2021-01-31 08:37

In MS SQL Server 2008 R2, how do I create a new database based on the schema of the old one, but without copying any of the data along with it? I am using SQL Server management

4条回答
  •  隐瞒了意图╮
    2021-01-31 09:25

    Summary

    This script was created to make it easier to practice database migration of a major website between very-different database schema. This script will perform the following tasks:

    1. Backup the latest good version of your schema, with data. (Example: Dev Database)
    2. Drop the target database, if it exists
    3. Restore the backup to the target testing database
    4. Delete all data in the target database, despite constraints and foreign keys

    Usage Instructions

    Part 1

    You will probably want to obtain the code for this from within SSMS using the backup dialog. Click on the circled icon to obtain the script for your specific scenario.

    • If you do this, remember to set "overwrite" in the backup options before generating script -- we don't want to append to a backup set.

    enter image description here

    Part 2

    Don't forget to include the USE MASTER command before dropping the destination database. If you run this script for the second time in a single SSMS session, you will get a fatal error if you don't include this command. We also use DROP DATABASE here to not only remove the stale TargetDB, but also delete its files (Note, DB must be "active" for this to work)

    Part 3

    You may want to use the SSMS dialog to generate this initial restore script for you, similar to how you did it for Part 1 for Backup.

    Part 4

    Don't use Truncate here -- it won't work if you have foreign keys

    Source Code (run in SSMS)

    ----------------------------------------------------------------------------------------
    -- CREATE AN EMPTY COPY OF DATABASE
    ----------------------------------------------------------------------------------------
    /* PART 1: Backup the good database */
    BACKUP DATABASE [OriginalDB] 
            TO  DISK = N'd:\backup.bak' WITH NOFORMAT, INIT,  
                NAME = N'OriginalDB-Full Database Backup', SKIP, 
            NOREWIND, NOUNLOAD,  STATS = 33
    GO
    
    /* PART 2: If your destination database already exists, drop it */
    USE master -- Make sure to include this -- it allows you to reuse script in same SSMS session
    DROP DATABASE [migration]
    
    /* PART 3: Restore the backup to the new location */
    RESTORE DATABASE [TargetDB] 
            FROM  DISK = N'D:\backup.bak' WITH  FILE = 1,  
            MOVE N'OriginalDB' TO N'D:\sql data\TargetDB.mdf',  
            MOVE N'OriginalDB' TO N'C:\SQL Data\TargetDB_1.ldf',  
            NOUNLOAD,  STATS = 33
    GO
    
    /* PART 4: Delete all tables' data in the migration testing target */
    PRINT N'Clearing [TargetDB]'
    USE [TargetDB]
    EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"       -- disable all constraints
    EXEC sp_MSForEachTable "DELETE FROM ?"                  -- delete data in all tables
    exec sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"  -- enable all constraints
    ----------------------------------------------------------------------------------------
    -- BLANK DATABASE COPY CREATED, READY FOR TESTING
    ----------------------------------------------------------------------------------------
    

提交回复
热议问题