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

前端 未结 4 618
甜味超标
甜味超标 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:10

    There is a nice script wizard that can help you. You can try it with Right Click on the database from the object explorer -> Tasks -> Generate Scripts. You can choose which objects from the database you want to export.

    0 讨论(0)
  • 2021-01-31 09:19

    Right click the Database and select Tasks -> Generate Scripts.

    You can you then select all the objects you require or only certain objects.

    There are some Script Options that you should look at:

    • Script USE DATABASE - You should set this to false if you are creating a new database with a different name.
    • Indexes & Triggers are not scripted as default
    • Logins & Object Level Permissions are not scripted as default
    • Generate Script for Dependent objects - this is set to false as default but you may want to change it to true to ensure that all objects are scripted.

    You can either create these to a new Query Window or save them to SQL Files.

    0 讨论(0)
  • 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
    ----------------------------------------------------------------------------------------
    
    0 讨论(0)
  • 2021-01-31 09:26

    In SQL Server Management Studio, you can menu-click on an object and select...

    "Script [Object] As" ... "CREATE to"... "New Query Window"
    

    You would need to create scripts for each object you want to create.

    If you want to do the whole lot, menu-click on the database, and select

    "Tasks" ... "Generate Scripts" and follow the wizard, as per this article:

    0 讨论(0)
提交回复
热议问题