SQL Server: Moving a Database



Three mechanisms for moving a database (within the same server, or between servers):
  1. Backup and Restore
  2. Copy Database Wizard
  3. Detach/Attach

Detaching/Attaching databases
  • Data files and transaction logs can be detached and then reattached to the same or different SQL Server instance.
  • You may transfer databases across 32-bit and 64-bit environments.

Note that:
  • Make sure you have a backup of the database before attaching it to a newer version server.
  • Once you attach a SQL Server 2000 or 2005 database to a 2008 SQL Server, you will not be able to attach it back into previous version servers.
  • After attaching a database to a new server, you will need to add users from the old server, if they are to continue to have access to the database.

You CANNOT detach a dataabse when:
  • The database is replicated AND published. Before detaching you need to disable publishing (run sp_replicationdboption)
  • Database contains a database snapshot.
  • Database is being mirrored.
  • While there are database sessions.
  • Database is suspect. Needs to be put into emergency mode.
  • Database is a system database.

Moving a database:

(a) Detach the database as follows:
    -- (1) Set database in single_user mode
USE MASTER;
ALTER DATABASE AdventureWorks2008R2
    SET single_user;
GO
    --(2) Check access mode 
SELECT name, user_access_desc
    FROM sys.databases
    WHERE name = 'adventureworks2008r2';
    --(3) Detach database. Set option to skip update statistics for true.
    -- By default, the detach operation retains any out-of-date optimization statistics  
    -- If you want updated optimization statistics to be gathered, set the parameter to 'false'
            .
EXEC sp_detach_db 'AdventureWorks2008R2', 'true';

    --(4) check that the database is no longer listed in the catalog
SELECT name     FROM sys.databases;

(b) MOVE/COPY data and log files to the new location
(c) Re-attach the database as follows:
USE MASTER;
GO
EXEC sp_attach_db @dbname = 'AdventureWorks2008R2' ,
    @filename1 = 'c:\mssqldrive\e\AdventureWorks2008R2_Data.mdf',
    @filename2 = 'c:\mssqldrive\e\AdventureWorks2008R2_Log.ldf';
    -- (2) Check that the database is in the catalog and the new file locations:

EXEC ssp_helpdb Adventureworks2008r2; 
name db_size owner dbid created status compatibility_level -------------------- --------- -------------- ---- ---------- ----------------- -------------------- AdventureWorks2008R2 181.88 MB Pholder\sqltest 9 May 10 2010 Status=ONLINE,... 100 name fileid filename filegroup size ... usage ------------------------- ------ ------------------------------------------ --------- -------- ------ AdventureWorks2008R2_Data 1 g:\sqldata\..\AdventureWorks2008R2_Data.mdf PRIMARY 184192 KB ... data only AdventureWorks2008R2_Log 2 g:\sqldata\..\AdventureWorks2008R2_Log.ldf NULL 2048 KB ... log only

Attaching a database
  • Use sp_attach_db only if the database was previously detached with sp_detach_db, or copied.
  • If you need to specify more than 16 files, use instead
    • CREATE DATABASE dbname FOR ATTACH or
    • CREATE DATABASE dbname FOR_ATTACH_REBUILD_LOG
(more information: here)

SQL Server: Database Configuration Options


Configuration Options
  1. Autogrowth
  2. Recovery Model
  3. Compatibility Level
  4. Auto Shrink
  5. Restrict Access


Autogrowth - Property of each database file.
  • Control how the file expands (FILEGROWTH clause).
  • By default, each file assume values set in the model database.
  • Configure automatic growth rate (percent or fixed-size)
  • Configure maximum file size. Once reached, database goes into read-only mode.
  • Full database generates Event ID 3758.
  • Can be checked with sp_dbhelp, sp_dbhelp dbnamed, sp_helpfile filename
  • If all files have a maxsize, the database has a maxsize.


Recovery model
Determines how the transaction log is used: how transactions are logged, whether the transaction log is backed up, and what restore options are available.
Three values: { SIMPLE | FULL | BULK-LOGGED }


SIMPLE recovery model
  • Minimal logging.
  • Minimizes administrative overhead for the Transaction Log (TL).
  • Backups may need to be performed more often.
  • Here the use of differential backups can help reduce the backup overhead.
  • TL truncated after each checkpoint. During a checkpoint the dirty pages are written from bugger memory to the data file and the TL is truncated.
  • Transaction logs(TL) cannot be backed up or restored.
  • You also cannot restore individual data pages.
  • Recovery possible only to the end of the last backup.
When to use simple recovery model?
  • test environments, development, read-only production scenarios.

BULK-LOGGED recovery model
  • Minimal logging performed for bulk actions: Logs contain only the information that a bulk operation has occurred, but not the data changes incurred with such operation.
  • These include:
    • SELECT INTO,
    • Some INSERT INTO that use SELECT statement (OPENROWSET (bulk...). TABLOCK hint)
    • BULK INSERT operations,
    • Write actions performed by the BCP program
    • UPDATE with WRITE clause
    • CREATE, ALTER or DROP INDEX
  • Intended as temporary to be used when large bulk operations need to be performed in the database.

Restore possible?
  • In Bulk-Logged mode, a backup of the transaction logs also includes all data extents marked as updated by a bulk operation. This guarantees that a restore can be performed, even though not all necessary change information is available in the transaction logs.
  • Point-in-time recovery: possible ONLY if no bulk action have occurred since the last full backup.

why use Bulk-logged model?
  • Allows for high-performance bulk copy operations.
  • If many or large bulk operations are to be performed, this configuration minimizes the use of transaction logs (improving performance with less write operations) while still guaranteeing data availability if a restore is needed.

FULL recovery model
  • Full logging.
  • Logs never truncated.
  • Can be used to recover to an arbitrary point in time.

To view the Recovery model:

(a) Use the Object Explorer on SMSS. Select database; right-click and choose database properties.
(b) Use sp_helpdb [dbname]
(c) Query sys.databases
USE MASTER;
GO
SELECT name, recovery_model_desc 
FROM sys.databases;
name recovery_model_desc ------- -------------- master SIMPLE tempdb SIMPLE model FULL msdb SIMPLE TESTSQL FULL MyDB FULL

(d) To view the recovery mode of the available backup sets use msdb.dbo.backupset catalog table

i.e Change the recovery model of TESTSQL database to perform bulk copy operation:
-- (1) Backup database
BACKUP DATABASE TESTSQL TO DISK='C:\mssqlbackup\TESTSQL.bak'
GO
Processed 304 pages for database 'TESTSQL', file 'TESTSQL' on file 1. Processed 3 pages for database 'TESTSQL', file 'TESTSQL' on file 1. BACKUP DATABASE successfully processed 307 pages in 0.397 seconds (6.022 MB/sec).
-- (2) Change recovery model to BULK_LOGGED
USE MASTER;
GO
SELECT name, recovery_model_desc FROM sys.databases;
name recovery_model_desc ------- -------------- master SIMPLE tempdb SIMPLE model FULL msdb SIMPLE TESTSQL BULK_LOGGED MyDB FULL
--(3) Perform BULK operation

--(4) Change recovery model back to FULL
ALTER DATABASE TESTSQL SET recovery FULL

--(5) Backup database to include new data
BACKUP DATABASE TESTSQL TO DISK='C:\mssqlbackup\testsql.bak'
GO

Compatibility Level.
  • Database-specific.
  • Provides only partial backward compatibility.
  • Default 100.
  • ALTER DATABASE dbname SET COMPATIBILITY_LEVEL = { 80 | 90 | 100 }
  • ALTER DATABASE Replaces sp_dbcmptlevel procedure
  • Do not change compatibility level while users are connected to the database.
  • (changes may affect query plan compilation)
  • Set database to single-user access mode before altering compatibility level.
  • i.e. ALTER DATABASE SET SINGLE_USER
Further reading and examples:
  • Books online





  • Database Size: Auto Shrink
    • Available, but perhaps not a good idea to use.
    • Its disk and processor intensive: file reorganization.
    • Just don't: Auto_shrink will increase Index fragmentation enormously.
    • If you need to shrink, do it manually (DBCC SHRINKFILE and DBCC SHRINKDATABASE).
    • When AUTO_SHRINK is on, both data and log files can be automatically shrunk. Files are shrunk when more than 25% of the file contains free space. Files are then shrunk to a size where 25% of the file is unused.
    • Check a good discussion here.
    -- To set AUTO_SHRINK on|off: 
    ALTER DATABASE SET AUTO_SHRINK { ON | OFF }
    
    To check the status of AUTO_SHRINK:
    SELECT name, is_auto_shrink_on  FROM sys.databases;
    go
    
    name       is_auto_shrink_on
    master     0
    tempdb     0
    model      0
    msdb       0
    TESTSQL    0
    
    alter database TESTSQL
       set auto_shrink ON;
    go
    
    select name, is_auto_shrink_on
    from sys.databases;
    go
    
    name       is_auto_shrink_on
    master     0
    tempdb     0
    model      0
    msdb       0
    TESTSQL    1
    

    Restrict Access.

    Specify which users may access the database.
    SINGLE_USER
    • Used for maintenance actions.
    • Any single user may connect, but only one user is accepted at a time.
    • To switch to single_user mode when other sessions are active, you need to disconnect them first.
    -- The command below allow 6 minutes for current sessions to complete. 
    -- After this any transaction will be rolled back: 
    
     ALTER DATABASE Sales
       SET SIGLE_USER
       WITH ROLLBACK AFTER 360;
    

    MULTI_USER
    • Default and normal state for production.
    • Allows multiple user access.

    RESTRICTED
    • Only members of the db_owner, dbcreator, or sysadmin roles.







    Configuring SQL Server: Data and Log files


    1. Configure data files
    2. Configure log files
    3. Configure filegroups






    Files (three types:)
    Primary data files: (.mdf) Secondary data files: (.ndf) Log files: (.ldf)
    Mandatory. Is the database "starting point".
    Holds pointers to other files in the database.
    Contain schema (structure) and database properties information
    Contains startup information for the database catalog.
    May (but should not) also contain objects and user data.
    Keep only catalog objects in the primary file: reduce disk contention.
    These are all data files, other then the primary.
    Optional and user-defined.
    Contains objects and user data.
    Can (should) be distributed across disks/devices to improve access performance.
    A database may have up to 32,766 secondary data files.
    AT LEAST one log file is needed for each database.
    Used for recovery.
    Default setting: logs in the same disk in which datafiles are created.
    Should be stored in separate disks, unless the database is read-only (no contention, then).



    When does the DB Engine uses file location info from primary file (.mdf)?
    • when attaching a database using CREATE DATABASE stmt with either FOR ATTACH or FOR ATTACH_REBUILD_LOG options.
    • When upgrading from SQL Server 2000 or 7.0
    • When restoring the master database.
    Note: SQL Server x Oracle
    • In an Oracle database, the list of datafile locations is maintained in the control file, and the information is used for database startup and recovery.
    • In this sense, the primary data file of a SQL Server database, holding file location info, performs a role similar to that performed by Oracle's control file.

    • The locations of all the files in a database are recorded in the primary file AND in the master database.
    • Most of the time, SQLServer DB Engine uses info from the master database.
    • File Logical name: used to refer to the physical files in all Transact-SQL stmts.
    • File Physical (OS) name:
    • NTFS File system recommended.
    • Each instance has its own default directory.
    • Shared files for ALL INSTANCES are located at:
    • disk:\Program Files\Microsoft SQL Server\100\


    Data file pages
    • Pages as numbered sequentially (0,1,..)
    • Each file has a unique File ID number
    • To find a page: File ID + page number
    • First page of file: header page.
    • Other system info pages:
      • database boot page: info about the database attributes
      • AIM (allocation information map

    (source: SQl Server books online)

    Filegroups
    Datafiles can be grouped in filegroups for easier administration and better performance.
    Filegroup is a logical structure: database objects can be allocated to filegroups, and can be splitted across various filegroups.

    Note: SQL Server x Oracle
    • SQL Server: filegroups ==> Oracle: tablespaces
    • An Oracle database, TABLESPACESare logical structures similar to filegroups.
    • Oracle: You assign database objects to a TABLESPACE.
    • SQL Server: You assign database objects to a FILEGROUP.
    • A TABLESPACE may have multiple datafiles, split across different disks or storage devices, for performance improvement.
    • TABLESPACEScan also be backed up or restored as a unit.
    • One object (i.e. table, index) CAN span multiple TABLESPACES, when it is PARTITIONED.
    • Oracle: Default Tablespace => SQL Server: Default filegroup
    • Handling temporary objects:
      • Oracle: Temporary Tablespace => SQL Server: Tempdb database

    Primary filegroups:
    • Contains the primary datafile + any secondary datafile not allocated to a filegroup.
    • All system tables are allocated to the primary filegroup.
    • Primary filegroup equivalent to the Oracle SYSTEM TABLESPACE.

    Secondary (user-defined) filegroup:
    • User-defined.
    • A databaes may have up to 32,766 secondary filegroups.
    • Created by using FILEGROUP in a CREATE DATABASE or ALTER DATABASE
    • Log files are never part of a filegroup.
    • Each file belongs to ONLY ONE filegroup.
    • If a filegroup has more than one data file, SQL Server distributes the data across the various files.
    • SQL Server keeps the amount of data in each file proportional to the file size.

    Filegroup properties:
    • Can be set or changed from SSMS or using ALTER DATABASE statement
    • Default filegroup: One filegroup is designated the DEFAULT filegroup.
    • Members of the db_owner database role can switch the default filegroup.
    • A filegroup can be made READ-ONLY
    • Tables (and other objects) are stored in filegroups, not in files.

    Create database MyDB
    (1) Define primary filegroup (logical name, physical name, file size, growth policy)
    (2) Define secondary filegroup
    USE MASTER;
    GO
        -- create db with default data filegroup and one log file.
        -- specify growth increment and max fize for primary data file.
    CREATE DATABASE MyDB
                ON PRIMARY
        ( name = 'MyDB_primary',
                    filename =
            'c:\Program Files\Micros...\MSSQL...\MSSQL\data\MyDB_Prm.mdf',
                    size=4mb,
                    maxsize=10mb,
                    filegrowth=1mb),
                filegroup MyDB_fg1
        ( name = 'MyDB_FG1_Dat1',
                    filename =
            'c:\Program Files\Micros...\MSSQL...\MSSQL\data\MyDB_FG1_1.ndf',
                    size=1mb,
                    maxsize=10mb,
                    filegrowth=1mb),
        ( name = 'MyDB_FG1_Dat2',
                    filename =
            'c:\Program Files\Micros...\MSSQL...\MSSQL\data\MyDB_FG1_2.ndf',
                    SIZE = 1MB,
                    MAXSIZE=10MB,
                    FILEGROWTH=1MB)
                LOG ON
        ( name='MyDB_archlog1',
                    filename =
            'c:\Program Files\Micros...\MSSQL...\MSSQL\data\MyDB_archlog1.ldf',
                    size=1mb,
                    maxsize=10mb,
                    filegrowth=1mb);
    GO
    
        -- NOTE: In a production database, the transaction logs SHOULD be stored on a physical disk OTHER THAN 
        -- the one in which the data files are located.

    (3) Alter database define default filegroup.
      -- define the default filegroup
    ALTER DATABASE MyDB
                modify filegroup MyDB_FG1 DEFAULT;
    GO
    

    (4) Add a new data file to a filegroup
      ALTER DATABASE MyDB
        ADD FILE
          ( name = 'MyDB_FG1_Dat3',
            filename = 
             'c:\Program Files\Micros...\MSSQL...\MSSQL\data\MyDB_FG1_3.ndf',
            size=1mb,
            maxsize=10mb,
            filegrowth=1mb)
         TO filegroup MyDB_FG1;    

    (5) Create table EMP on MyDB
    (6) Check table information in the database
      -- create table in the user-defined filegroup
      -- Table EMP created in the dbo schema.
      USE MyDB;
      CREATE TABLE emp
       ( empid INT PRIMARY KEY,
         name CHAR(20))
      ON MyDB_FG1;
      GO   
    
      EXEC sp_columns @table_name='emp', @table_owner='dbo'
      GO
    

    (7) Create a new filegroup
      -- Create a new filegroup
      ALTER DATABASE MyDB
        ADD filegroup MyDB_FG2;
        
      ALTER DATABASE MyDB
         ADD FILE  
         ( name = 'MyDB_FG2_Dat1',
           filename = 
             'c:\Program Files\Micros...\MSSQL...\MSSQL\data\MyDB_FG2_1.ndf',
           size=1mb,
           maxsize=10mb,
           filegrowth=1mb)       
        TO filegroup MyDB_FG2;;    

    sys.databases
    sys.filegroups
    sys.database_files
    sys.master_files
    sys.data_spaces

    Now, check the database catalog for information on:
    (a) Information on databases: sys.databases
    USE MyDB;
    GO
    
    SELECT name, create_date, user_access_desc,
           state_desc, recovery_model_desc
    FROM sys.databases;
    name create_date user_access_desc state_desc recovery_model_desc ------- ---------------------- ---------------- ---------- -------------- master 2003-04-08 09:13:36.390 MULTI_USER ONLINE SIMPLE tempdb 2011-10-06 14:56:52.290 MULTI_USER ONLINE SIMPLE model 2003-04-08 09:13:36.390 MULTI_USER ONLINE FULL msdb 2010-04-02 17:35:08.970 MULTI_USER ONLINE SIMPLE TESTSQL 2011-10-08 10:43:13.347 MULTI_USER ONLINE FULL MyDB 2011-10-08 19:12:38.427 MULTI_USER ONLINE FULL

    (b) Information on filegroups: sys.filegroups
    USE MyDB;
    GO
    
    SELECT * FROM sys.filegroups;
    name data_space_id type type_desc is_default filegroup_guid log_filegroup_id is_read_only ---------- ------------- ---- ---------------- ---------- ------------------------------------ ---------------- ------------ PRIMARY 1 FG ROWS_FILEGROUP 0 NULL NULL 0 MyDB_fg1 2 FG ROWS_FILEGROUP 1 803DD155-A6A1-4CB5-B183-3FB16D561B13 NULL 0 MyDB_FG2 3 FG ROWS_FILEGROUP 0 D167E2CC-DBB6-45B2-8197-2FF1EA827940 NULL 0 (3 row(s) affected)

    (c) Information on data files: sys.database_files
    USE MyDB;
    GO
    
    SELECT name, type_desc, physical_name, state_desc, size
    FROM sys.database_files;
    name type_desc physical_name state_desc size ------------- --------- ------------------------------------------------------------------ ---------- ------ MyDB_primary ROWS c:\Program Files\Micros...\MSSQL10...\MSSQL\data\MyDB_Prm.mdf ONLINE 512 MyDB_archlog1 LOG c:\Program Files\Micros...\MSSQL10...\MSSQL\data\MyDB_archlog1.ldf ONLINE 128 MyDB_FG1_Dat1 ROWS c:\Program Files\Micros...\MSSQL10...\MSSQL\data\MyDB_FG1_1.ndf ONLINE 128 MyDB_FG1_Dat2 ROWS c:\Program Files\Micros...\MSSQL10...\MSSQL\data\MyDB_FG1_2.ndf ONLINE 128 MyDB_FG1_Dat3 ROWS c:\Program Files\Micros...\MSSQL10...\MSSQL\data\MyDB_FG1_3.ndf ONLINE 128 MyDB_FG2_Dat1 ROWS c:\Program Files\Micros...\MSSQL10...\MSSQL\data\MyDB_FG2_1.ndf ONLINE 128

    (d) To list the names of the files of all databases in a server:sys.master_files
    SELECT DB_NAME(database_id) AS 'db_name', name AS 'logical filename', 
           physical_name, type_desc
    FROM sys.master_files;
    db_name logical filename physical_name type_desc ---------- ---------------- ------------------------------------------------------- ---------- master master C:\Progr...\MSSQL10...\MSSQL\DATA\master.mdf ROWS master mastlog C:\Progr...\MSSQL10...\MSSQL\DATA\mastlog.ldf LOG tempdb tempdev C:\Progr...\MSSQL10...\MSSQL\DATA\tempdb.mdf ROWS tempdb templog C:\Progr...\MSSQL10...\MSSQL\DATA\templog.ldf LOG model modeldev C:\Progr...\MSSQL10...\MSSQL\DATA\model.mdf ROWS model modellog C:\Progr...\MSSQL10...\MSSQL\DATA\modellog.ldf LOG msdb MSDBData C:\Progr...\MSSQL10...\MSSQL\DATA\MSDBData.mdf ROWS msdb MSDBLog C:\Progr...\MSSQL10...\MSSQL\DATA\MSDBLog.ldf LOG TESTSQL2008 TESTSQL2008 C:\Progr...\MSSQL10...\MSSQL\DATA\InsideTSQL2008.mdf ROWS TESTSQL2008 TESTSQL2008_log C:\Progr...\MSSQL10...\MSSQL\DATA\InsideTSQL2008_log.LDF LOG MyDB MyDB_primary c:\Progr...\MSSQL10...\MSSQL\data\MyDB_Prm.mdf ROWS MyDB MyDB_archlog1 c:\Progr...\MSSQL10...\MSSQL\data\MyDB_archlog1.ldf LOG MyDB MyDB_FG1_Dat1 c:\Progr...\MSSQL10...\MSSQL\data\MyDB_FG1_1.ndf ROWS MyDB MyDB_FG1_Dat2 c:\Progr...\MSSQL10...\MSSQL\data\MyDB_FG1_2.ndf ROWS MyDB MyDB_FG1_Dat3 c:\Progr...\MSSQL10...\MSSQL\data\MyDB_FG1_3.ndf ROWS

    (e) check data spaces:
    USE MyDB;
    SELECT name, type_desc FROM sys.data_spaces;
    name type_desc -------- -------------- PRIMARY ROWS_FILEGROUP MyDB_fg1 ROWS_FILEGROUP MyDB_FG2 ROWS_FILEGROUP

    Database files and filegroups
    (figure modified from here.)