SQL Commands - Create, Drop and Alter Database in SQL Server 2008
Creating and Altering a Database
In previous article I explained about Difference between MS SQL 2005 & SQL Server 2000 - SQL Server Tutorials. Here in this article I will explain how to create, alter and drop database in SQL Server 2008.
A SQL Server database can be created, altered and dropped
- Graphically using SQL Server Management Studio (SSMS) or
- Using a Query
To Create the database using a Query
Eg. Create Database Sample1
To Select the database using a Query
Whether, you create a database graphically using the designer or, using a query, the following two files get generated:
- .MDF file - Data File (Contains actual data)
- .LDF file - Transaction log file (Used to recover the database)
To Alter a database, once it's created
Eg. alter database sample1 modify name = sample2
Alternatively, you can also use system stored procedure
Eg. sp_renamedb 'sample2','sample3'
Deleting or Dropping a Database
To Delete or Drop a database
Eg. drop database sample3
Dropping a database, deletes the .LDF and .MDF files.
You can't drop a database , if it is currently in use. You get an error starting - Cannot drop database <NewDatabaseName> because it is currently in use.
So, if other users are connected, you need to put the database in single user mode and them drop the database.
Eg. Alter database Sample3 Set Single_User with Rollback Immediate
With Rollback Immediate option, will rollback all incomplete transactions and closes the connection to the database.
Note: System databases cannot be dropped
SQL Commands - Create, Drop and Alter Database in SQL Server 2008 Reviewed by Ravi Kumar on 11:42:00 PM Rating: