Tutorial on Creating, Altering, and Deleting Databases in SQL Server

Creating a Database in SQL Server

  1. Using SQL Server Management Studio
    1. In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance.
    2. Right-click Databases, and then select New Database.
    3. In New Database, enter a database name.
    4. To create the database by accepting all default values, select OK; otherwise, continue with the following optional steps.
    5. ...
    6. To create the database, select OK.
  2. Using Transact-SQL
    1. Connect to the Database Engine.
    2. From the Standard bar, select New Query.
    3. Copy and paste the following example into the query window and select Execute. This example creates the database Sales.
USE master;
GO

CREATE DATABASE Sales ON
(NAME = Sales_dat,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\saledat.mdf',
    SIZE = 10,
    MAXSIZE = 50,
    FILEGROWTH = 5)
LOG ON
(NAME = Sales_log,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\salelog.ldf',
    SIZE = 5 MB,
    MAXSIZE = 25 MB,
    FILEGROWTH = 5 MB);
GO

Altering a Database in SQL Server

  1. Using Transact-SQL
    1. Connect to the Database Engine.
    2. From the Standard bar, select New Query.
    3. Copy and paste the following example into the query window and select Execute. This example alters the Sales database to add a new filegroup named Sales_fg1.
USE master;
GO
ALTER DATABASE Sales
ADD FILEGROUP Sales_fg1;
GO

Deleting a Database in SQL Server

  1. Using SQL Server Management Studio
    1. In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
    2. Expand Databases, right-click the database to delete, and then select Delete.
    3. To confirm you want to delete the database, select Yes.
  2. Using Transact-SQL
    1. Connect to the Database Engine.
    2. From the Standard bar, select New Query.
    3. Copy and paste the following example into the query window and select Execute. This example removes the Sales database.
USE master ;  
GO  
DROP DATABASE Sales ;  
GO  

Follow Up: After deleting a database

Back up the master database. If master must be restored, any database that has been deleted since the last backup of master will still have references in the system catalog views and may cause error messages to be raised.

Related Articles
Coming Soon