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 database is a collection of objects such as ใใผใใซ, views, stored procedures, triggers, and functions. In SQLใตใผใใผ, 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.
There are 2 ways to create a database in SQL Server:
- SQL Server Management Studio
- Transact SQL
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.
ในใใใ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.
ใใใซ๏ผ
- 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.
ในใใใ4๏ผ Click on โAddโ (the OK button) to create the database.
The button that confirms creation is highlighted below.
็ตๆ๏ผ 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.
ใฝใผใน ใฏใจใชใ่กจ็คบใใพใใ 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.
ใฏใจใชใฆใฃใณใใฆ: The generated CREATE script opens in a new query window, as shown below.
ในใฏใชใใใไฝๆใใพใ:
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.
็ตๆ๏ผ You can see Edu_TSQL_file created in the SQL Object Explorer.
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.
ใใผใฟใใผในใๅคๆดใใๆนๆณ
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 Server Management Studio
- Transact 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.
ในใใใ1๏ผ Rename the database. Right-click on the database name and click on โRenameโ.
ในใใใ2๏ผ The database name becomes editable. Enter the new name and press Enter.
็ตๆ๏ผ The database is now renamed as โEdu_Alterโ from โEduโ.
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.
ไธ่ฌ็ใชๆงๆ: 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.
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.
ใใผใฟใใผในใฎๅ้ค
There are 2 ways to delete (drop) a database in SQL Server:
- SQL Server Management Studio
- Transact 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โ.
็ตๆ๏ผ โEdu_Alterโ is deleted from the โObject Explorerโ database list.
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.
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.





















