SQL Server Database: Create, Alter, Drop & Restore

โšก ฮˆฮพฯ…ฯ€ฮฝฮท ฮฃฯฮฝฮฟฯˆฮท

SQL Server databases are containers that hold tables, views, stored procedures, and other objects. Creating, altering, dropping, and restoring a database can be done through SQL Server Management Studio or with Transact-SQL statements.

  • ๐Ÿ—„๏ธ ฮ’ฮฌฯƒฮท ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ: A SQL Server database stores tables, views, procedures, and functions in one organized, interlinked container.
  • ๐Ÿงฉ System vs User: System databases (master, msdb, model, tempdb, resource) install automatically; user databases are created by you.
  • ๐Ÿ–ฑ๏ธ ฮ”ฮทฮผฮนฮฟฯ…ฯฮณฯŽ: Add a database through the Object Explorer New Database dialog or with the CREATE DATABASE statement.
  • ๏ธ Alter: Rename a database or change its files and options using ALTER DATABASE or the Rename menu.
  • ๐Ÿ—‘๏ธ ฮ ฯ„ฯŽฯƒฮท: Remove a user database with DROP DATABASE or the Delete option; system databases cannot be dropped.
  • ๐Ÿ’พ ฮ•ฯ€ฮฑฮฝฮฑฯ†ฮญฯฯ‰: Rebuild a database from a .bak backup file using the RESTORE DATABASE command.

SQL Server Database Create, Alter, Drop, and Restore

ฮคฮน ฮตฮฏฮฝฮฑฮน ฮท ฮฒฮฌฯƒฮท ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ;

A database is a collection of objects such as ฯ€ฮฏฮฝฮฑฮบฮตฯ‚, views, stored procedures, triggers, and functions. In ฮŸ SQL Server, a database is the organized home where all of these related objects are stored and interlinked.

Consider a few examples from real life:

  • A bookcase is where books reside.
  • A home is where we live.
  • A parking lot is where vehicles are parked, and such examples are countless.

Similarly, a database is a kind of home for all of our tables, views, and stored procedures. Technically, a database stores data in a well-organized manner for easy access and retrieval. In SQL Server, there are two types of databases:

  • System databases: These are created automatically when you install SQL Server. They play a crucial role in the server, especially in ensuring that database objects run correctly. Examples of SQL Server system databases include master, msdb, model, tempdb, and resource.
  • User databases: These are created by database users like you, who have been granted access to create databases.

ฮšฮฑฮฝฯŒฮฝฮตฯ‚ ฮณฮนฮฑ ฯ„ฮท ฮดฮทฮผฮนฮฟฯ…ฯฮณฮฏฮฑ ฮฒฮฌฯƒฮทฯ‚ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ

First, you need to know the basic rules for creating a new database:

  • ฮคฮฑ ฮฟฮฝฯŒฮผฮฑฯ„ฮฑ ฮฒฮฌฯƒฮตฯ‰ฮฝ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฯ€ฯฮญฯ€ฮตฮน ฮฝฮฑ ฮตฮฏฮฝฮฑฮน ฮผฮฟฮฝฮฑฮดฮนฮบฮฌ ฯƒฮต ฮผฮนฮฑ ฯ€ฮฑฯฮฟฯ…ฯƒฮฏฮฑ ฯ„ฮฟฯ… SQL Server.
  • ฮคฮฑ ฮฟฮฝฯŒฮผฮฑฯ„ฮฑ ฮฒฮฌฯƒฮตฯ‰ฮฝ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฮผฯ€ฮฟฯฮฟฯฮฝ ฮฝฮฑ ฮตฮฏฮฝฮฑฮน ฯ„ฮฟ ฯ€ฮฟฮปฯ 128 ฯ‡ฮฑฯฮฑฮบฯ„ฮฎฯฮตฯ‚.
  • The CREATE DATABASE statement must run in auto-commit mode.

The image below outlines the two available approaches for creating a database in SQL Server.

Two ways to create a SQL Server database: Management Studio and Transact-SQL

There are 2 ways to create a database in SQL Server:

ฮ ฯŽฯ‚ ฮฝฮฑ ฮดฮทฮผฮนฮฟฯ…ฯฮณฮฎฯƒฮตฯ„ฮต ฮฒฮฌฯƒฮท ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฯƒฯ„ฮฟ SQL Server Management Studio

Here is a step-by-step process to create a database in SQL Server Management Studio:

ฮ’ฮฎฮผฮฑ 1) Right-click on โ€˜Databaseโ€™ in the โ€˜Object Explorerโ€™ window and then select โ€˜New Database.โ€™

The right-click menu with the New Database option is shown below.

Right-click menu on the Database node in Object Explorer with New Database selected

ฮ’ฮฎฮผฮฑ 2) The โ€˜New Databaseโ€™ screen appears. Enter a โ€˜Database nameโ€™. The โ€˜Logical Nameโ€™ column is auto-populated with:

  • Edu โ€“ Filetype: Rows Data and Filegroup: PRIMARY
  • Edu_log โ€“ Filetype: LOG and Filegroup: โ€˜Not Applicableโ€™

The New Database dialog with the database name and its logical files is shown below.

New Database dialog showing the Edu database name and Edu and Edu_log logical files

ฮ•ฮดฯŽ:

  • A) Edu (Filetype: Rows Data, Filegroup: PRIMARY) is the .mdf data file.
  • B) Edu_log (Filetype: LOG, Filegroup: โ€˜Not Applicableโ€™) is the .ldf log file.

ฮ’ฮฎฮผฮฑ 3) (Optional) For more complex settings, you can navigate to the โ€˜Optionsโ€™ and โ€˜Filegroupsโ€™ pages. At beginner level, creating the database from the General tab will suffice.

The Options and Filegroups pages are shown below.

New Database Options and Filegroups pages for advanced settings

ฮ’ฮฎฮผฮฑ 4) Click on โ€˜Addโ€™ (the OK button) to create the database.

The button that confirms creation is highlighted below.

OK button in the New Database dialog that creates the Edu database

ฮ‘ฯ€ฮฟฯ„ฮญฮปฮตฯƒฮผฮฑ: The โ€˜Eduโ€™ database is created. You can expand the database โ€˜Eduโ€™, which will contain Tables, Views, and so on. These are initially blank until you create new tables, views, etc.

Object Explorer showing the newly created Edu database with its blank folders

ฮ”ฮตฮฏฯ„ฮต ฯ„ฮฟ ฮตฯฯŽฯ„ฮทฮผฮฑ ฯ€ฮทฮณฮฎฯ‚: You can view the source query of the newly created โ€˜Eduโ€™ database. Navigate as follows: Right-click on the database name > Script Database as > CREATE To > New Query Editor Window.

Script Database as CREATE To New Query Editor Window context menu

ฮ ฮฑฯฮฌฮธฯ…ฯฮฟ ฮตฯฯ‰ฯ„ฮฎฮผฮฑฯ„ฮฟฯ‚: The generated CREATE script opens in a new query window, as shown below.

Query editor window displaying the auto-generated CREATE DATABASE script for Edu

ฮ”ฮทฮผฮนฮฟฯ…ฯฮณฮฏฮฑ ฯƒฮตฮฝฮฑฯฮฏฮฟฯ…:

USE [master]
GO

CREATE DATABASE [Edu]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'Edu', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'Edu_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )

ฮ”ฮทฮผฮนฮฟฯ…ฯฮณฮฏฮฑ ฮฒฮฌฯƒฮทฯ‚ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฮผฮต T-SQL

Another method is to write a T-SQL query to create a database and then execute it. Let us look at the simplest database-creation T-SQL query.

ฮฃฯฮฝฯ„ฮฑฮพฮท:

CREATE DATABASE <Database_name>

ฮ•ฯฯŽฯ„ฮทฯƒฮท:

CREATE DATABASE [Edu_TSQL_file]

Click on โ€˜Executeโ€™ to run the query, as shown below.

Execute button in the SSMS toolbar used to run the CREATE DATABASE query

ฮ‘ฯ€ฮฟฯ„ฮญฮปฮตฯƒฮผฮฑ: You can see Edu_TSQL_file created in the SQL Object Explorer.

Object Explorer listing the Edu_TSQL_file database created with Transact-SQL

Now let us look at how to create a database with explicit .mdf and .ldf files. Here, you can give the file location as an explicit part of the query.

ฮฃฯฮฝฯ„ฮฑฮพฮท:

CREATE DATABASE database_name   
[ CONTAINMENT = { NONE | PARTIAL } ]  
[ ON   
      [ PRIMARY ] <filespec> [ ,...n ]   
      [ , <filegroup> [ ,...n ] ]   
      [ LOG ON <filespec> [ ,...n ] ]   
];

ฮ•ฯฯŽฯ„ฮทฯƒฮท:

CREATE DATABASE [Edu_TSQL_file]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'Edu_TSQL_file', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu_TSQL_file.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'Edu_TSQL_file_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu_TSQL_file_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )

The database created with its explicit data and log files appears in Object Explorer, as shown below.

Object Explorer showing the Edu_TSQL_file database created with explicit .mdf and .ldf files

ฮ ฯŽฯ‚ ฮฝฮฑ ฮฑฮปฮปฮฌฮพฮตฯ„ฮต ฯ„ฮท ฮฒฮฌฯƒฮท ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ

Like the CREATE DATABASE query, you can also alter a database. You can rename the database, change a file location or setting, and more.

Basic rules for altering a database:

  • The ALTER DATABASE statement must run in auto-commit mode.
  • ฮ— ALTER DATABASE ฮดฮตฮฝ ฮตฯ€ฮนฯ„ฯฮญฯ€ฮตฯ„ฮฑฮน ฯƒฮต ฯฮทฯ„ฮฎ ฮฎ ฯƒฮนฯ‰ฯ€ฮทฯฮฎ ฯƒฯ…ฮฝฮฑฮปฮปฮฑฮณฮฎ.

There are 2 ways to alter a database in SQL Server:

  • ฮฃฯ„ฮฟฯฮฝฯ„ฮนฮฟ ฮดฮนฮฑฯ‡ฮตฮฏฯฮนฯƒฮทฯ‚ ฮดฮนฮฑฮบฮฟฮผฮนฯƒฯ„ฮฎ SQL
  • ฮฃฯ…ฮฝฮฑฮปฮปฮฑฮณฮฎ SQL

ฮ ฯŽฯ‚ ฮฝฮฑ ฮฑฮปฮปฮฌฮพฮตฯ„ฮต ฯ„ฮท ฮฒฮฌฯƒฮท ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฯƒฯ„ฮฟ SQL Server Management Studio

Below are the steps to alter a database in SQL Server Management Studio. Let us try to alter the name of our pre-created database โ€˜Eduโ€™. The pre-created Edu database is shown below in Object Explorer.

Pre-created Edu database selected in the Object Explorer database list

ฮ’ฮฎฮผฮฑ 1) Rename the database. Right-click on the database name and click on โ€˜Renameโ€™.

Right-click context menu on the Edu database with the Rename option

ฮ’ฮฎฮผฮฑ 2) The database name becomes editable. Enter the new name and press Enter.

Editable Edu database name field ready for a new name

ฮ‘ฯ€ฮฟฯ„ฮญฮปฮตฯƒฮผฮฑ: The database is now renamed as โ€œEdu_Alterโ€ from โ€˜Eduโ€™.

Object Explorer showing the database renamed from Edu to Edu_Alter

ฮ‘ฮปฮปฮฑฮณฮฎ ฮฒฮฌฯƒฮทฯ‚ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฮผฮต Transact-SQL

Now let us alter a database using T-SQL.

ฮฃฯฮฝฯ„ฮฑฮพฮท:

ALTER DATABASE <Databse_name>              
MODIFY NAME = <New Name>

ฮ•ฯฯŽฯ„ฮทฯƒฮท:

ALTER DATABASE Edu_TSQL
MODIFY NAME = Edu_TSQL_Alter;

ฮ•ฮบฯ„ฮตฮปฮญฯƒฯ„ฮต ฯ„ฮฟ ฯ€ฮฑฯฮฑฯ€ฮฌฮฝฯ‰ ฮตฯฯŽฯ„ฮทฮผฮฑ ฮบฮฌฮฝฮฟฮฝฯ„ฮฑฯ‚ ฮบฮปฮนฮบ ฯƒฯ„ฮฟ 'ฮ•ฮบฯ„ฮญฮปฮตฯƒฮท'.

ฮ‘ฯ€ฮฟฯ„ฮญฮปฮตฯƒฮผฮฑ: The database is now renamed as โ€œEdu_TSQL_Alterโ€ from โ€˜Edu_TSQLโ€™, as shown below.

Object Explorer showing the database renamed to Edu_TSQL_Alter after ALTER DATABASE

ฮ“ฮตฮฝฮนฮบฮฎ ฮฃฯฮฝฯ„ฮฑฮพฮท: The full ALTER DATABASE syntax supports many options, including renaming, collation, file options, and the compatibility level.

ALTER DATABASE { database_name  | CURRENT }  
{   MODIFY NAME = new_database_name   
  | COLLATE collation_name  
  | <file_and_filegroup_options>  
  | SET <option_spec> [ ,...n ] [ WITH <termination> ] 
  | SET COMPATIBILITY_LEVEL = { 140 | 130 | 120 | 110 | 100 | 90 }   
} ;

ฮฃฮทฮผฮตฮฏฯ‰ฯƒฮท: The compatibility levels shown above reflect older releases. Current versions add 150 (SQL Server 2019), 160 (SQL Server 2022), and 170 (SQL Server 2025), while still supporting several earlier levels.

Changing the .mdf/.ldf file name

ฮ•ฯฯŽฯ„ฮทฯƒฮท:

Alter DATABASE Edu_TSQL_Alter;
MODIFY FILE ( NAME = Edu_TSQL, NEWNAME = Edu_TSQL_newName );

The renamed logical file is reflected in the database properties, as shown below.

Database properties confirming the renamed logical file Edu_TSQL_newName

Changing the .mdf/.ldf file location

ฮ•ฯฯŽฯ„ฮทฯƒฮท:

Alter DATABASE Edu_TSQL_Alter;
MODIFY FILE ( NAME = Edu_TSQL_NewName, FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL_MS\MSSQL\DATA\New_File\Edu_TSQL_log.ldf' );

The updated file path for the database is shown below.

Database file page showing the new .ldf log file location

ฮ”ฮนฮฑฮณฯฮฑฯ†ฮฎ ฮฒฮฌฯƒฮทฯ‚ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ

There are 2 ways to delete (drop) a database in SQL Server:

  • ฮฃฯ„ฮฟฯฮฝฯ„ฮนฮฟ ฮดฮนฮฑฯ‡ฮตฮฏฯฮนฯƒฮทฯ‚ ฮดฮนฮฑฮบฮฟฮผฮนฯƒฯ„ฮฎ SQL
  • ฮฃฯ…ฮฝฮฑฮปฮปฮฑฮณฮฎ SQL

ฮ ฯŽฯ‚ ฮฝฮฑ ฮฑฯ€ฮฟฯฯฮฏฯˆฮตฯ„ฮต ฯ„ฮท ฮฒฮฌฯƒฮท ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฯƒฯ„ฮฟ SQL Server Management Studio

Following is the process to drop a database in SQL Server Management Studio. Let us try to delete our pre-created database โ€˜Edu_Alterโ€™.

ฮ’ฮฎฮผฮฑ 1) Right-click on the database, click on โ€˜Deleteโ€™, and then click โ€˜OKโ€™.

Delete Object dialog for dropping the Edu_Alter database in SSMS

ฮ‘ฯ€ฮฟฯ„ฮญฮปฮตฯƒฮผฮฑ: โ€˜Edu_Alterโ€™ is deleted from the โ€˜Object Explorerโ€™ database list.

Object Explorer database list after the Edu_Alter database was deleted

ฮ”ฮนฮฑฮณฯฮฑฯ†ฮฎ ฮฒฮฌฯƒฮทฯ‚ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฯ‡ฯฮทฯƒฮนฮผฮฟฯ€ฮฟฮนฯŽฮฝฯ„ฮฑฯ‚ Transact-SQL

Let us try to delete our pre-created database โ€˜Edu_TSQL_Alterโ€™.

ฮฃฯฮฝฯ„ฮฑฮพฮท:

DROP DATABASE <Databse_name>

ฮ•ฯฯŽฯ„ฮทฯƒฮท:

USE master;  
GO  
DROP DATABASE Edu_TSQL_Alter;  
GO

ฮ•ฮบฯ„ฮตฮปฮญฯƒฯ„ฮต ฯ„ฮฟ ฯ€ฮฑฯฮฑฯ€ฮฌฮฝฯ‰ ฮตฯฯŽฯ„ฮทฮผฮฑ ฮบฮฌฮฝฮฟฮฝฯ„ฮฑฯ‚ ฮบฮปฮนฮบ ฯƒฯ„ฮฟ 'ฮ•ฮบฯ„ฮญฮปฮตฯƒฮท'.

ฮ‘ฯ€ฮฟฯ„ฮญฮปฮตฯƒฮผฮฑ: โ€˜Edu_TSQL_Alterโ€™ is deleted from the โ€˜Object Explorerโ€™ database list, as shown below.

Object Explorer database list after the Edu_TSQL_Alter database was dropped with T-SQL

ฮ•ฯ€ฮฑฮฝฮฑฯ†ฮฟฯฮฌ ฮฒฮฌฯƒฮทฯ‚ ฮดฮตฮดฮฟฮผฮญฮฝฯ‰ฮฝ ฯƒฯ„ฮฟฮฝ SQL Server

You can create a database by restoring a database that you had backed up earlier. It can be done by running the RESTORE DATABASE command, which takes the following syntax:

restore Database <database name> from disk = '<Backup file location + filename>

The query should be executed within the query window, just like the previous commands. For example:

restore database Edu from disk = 'C:\Backup\Edu_full_backup.bak'

You can also use the GUI navigation: Right-click on Database > Restore Database > Device > import the backup file > click OK.

ฮฃฯ…ฯ‡ฮฝฮญฯ‚ ฮ•ฯฯ‰ฯ„ฮฎฯƒฮตฮนฯ‚

Creating a database requires CREATE DATABASE, CREATE ANY DATABASE, or ALTER ANY DATABASE permission in the master database. A member of the sysadmin or dbcreator server role, or a suitably granted ฮฃฯฮฝฮดฮตฯƒฮท, can run the statement.

A single SQL Server instance can hold up to 32,767 databases. That figure is a hard architectural limit; in practice, memory, storage, and administration overhead make far smaller numbers advisable for good performance.

No. System databases such as master, model, msdb, tempdb, and resource are required by the engine and cannot be dropped. Only user databases that you create can be deleted with DROP DATABASE or the Delete option.

SIZE sets the initial file size, MAXSIZE sets the largest size the file may grow to, and FILEGROWTH sets how much space is added each time the file expands. They apply to both the data and log files.

COMPATIBILITY_LEVEL controls which query-processing behavior a database uses. Current values include 140 (SQL Server 2017), 150 (2019), 160 (2022), and 170 (2025). ALTER DATABASE SET COMPATIBILITY_LEVEL changes it without moving the database to a new server.

DROP DATABASE permanently deletes the database and its .mdf and .ldf files. Taking a database OFFLINE only stops access while leaving the files on disk, so you can bring it back online later without a restore.

ฮฮฑฮฏ. GitHub Copilot can draft CREATE DATABASE, ALTER DATABASE, and DROP DATABASE statements from natural-language prompts inside SSMS or your editor. Always review generated file paths, sizes, and options before running them on a real server.

AI and machine learning assist provisioning by recommending file sizes and growth settings, forecasting capacity, spotting configuration drift, and flagging risky DROP or ALTER operations. They support the database administrator rather than replacing careful review.

ฮฃฯ…ฮฝฮฟฯˆฮฏฯƒฯ„ฮต ฮฑฯ…ฯ„ฮฎฮฝ ฯ„ฮทฮฝ ฮฑฮฝฮฌฯฯ„ฮทฯƒฮท ฮผฮต: