5. Working With Database

 1. To create a database

        CREATE DATABASE DatabaseName;

    Following two files gets created after creating the database

    a. MDF file - Data file ( It contains actual File)

    b. LDF file - Transaction Log File ( It is used to recover the database)


2. To alter a database after creating 

        ALTER DTAABASE DatabaseName MODIFY NAME = "NewDatabaseName";

  •       Alternatively we can use system stored procedure

                 EXECUTE SP_RENAMEDB 'OldDatabaseName', 'NewDatabaseName'

3. To Delete or Drop a Database

        DROP DATABASE Database that you want to Drop

  •       Dropping a database, deletes both  the LDF and MDF Files 
  •       System Database Cannot be dropped
  •       Currently used database cannot be dropped we need to put in single mode by using following  query 

       ALTER DATABASE DatabaseName SET SINGLE USER WITH ROllBACK IMMEDIATE; 


 4. To Create backup of the database

            BACKUP DATATBASE DATABASE_NAME

            TO DISK="PATH BAK FILE NAME"


 5. To Restore the Database

            RESTORE DATATBASE DATABASE_NAME

            FROM DISK="PATH BAK FILE NAME"

                    


Comments