Create Database in MS SQL Server
Last Updated :
14 Aug, 2024
Databases in Microsoft SQL Server are crucial for managing data, categorized into system databases, which are auto-created and user databases, created by users. In this article, We will learn about the basics of system and user databases along with methods for creating and managing them using T-SQL and SQL Server Management Studio (SSMS).
Create Database in SQL Server
- Creating a database in MS SQL Server is a fundamental task that involves setting up a structured environment to store, manage and retrieve data efficiently.
- Databases can be created using Transact-SQL (T-SQL) scripts or through the user-friendly interface of SQL Server Management Studio (SSMS), allowing users to customize their databases according to specific requirements.
Prerequisites
Databases are collections of objects like tables, views, stored procedures, functions, etc. In MS SQL Server, two sorts of databases are available.
- System databases
- User Databases
1. System Databases
System databases are created automatically once we install the MS SQL Server. Below is a list of system databases:
- Master
- Model
- MSDB
- Tempdb
- Resource (Introduced in the 2005 version)
- Distribution (It’s for the Replication feature only)
2. User Databases
User databases are created by users (DBAs, and testers who have access to create databases). To create a database, the below methods could be used:
- SQL Server Management Studio.
- Transact-SQL.
Method 1: Using T-SQL Script or Restore Database
The following can be the basic syntax for creating a database in MS SQL Server.
Restoring a database using a T-SQL script can be done in SQL Server Management Studio (SSMS) by executing a series of SQL commands. Here are the steps to restore a database using T-SQL script:
- Open a new query window in SSMS by clicking on “New Query” in the toolbar.
- Type the following command to restore a database from a backup file.
- Replace “DatabaseName” with the name of the database you want to restore, and “C:\Path\To\BackupFile.bak” with the path to the backup file on your server.
Syntax:
Create database <yourdatabasename>
Method 2: Using SQL Server Management Studio (SSMS)
Microsoft offers SQL Server Management Studio (SSMS) as a tool for managing SQL Server databases. You can use it to carry out a number of tasks, including managing security, writing and running SQL queries, and creating and modifying tables.
The following are some fundamental actions to use SQL Server Management Studio:
- Connect to a SQL Server instance: Launch SSMS and enter the server’s login information to establish a connection to a SQL Server instance. In accordance with your SQL Server configuration, you can select the authentication method.
- Making a Database: In Object Explorer, select “New Database” from the context menu when you right-click on the “Databases” folder. Give the option a name, then set the other parameters to meet your needs.
- Creating Tables: After creating a database, select “New Table” from the context menu when right-clicking on the Tables folder. Give the table a name and specify the columns and data types for it.
- Writing and running SQL queries: SSMS’s Query Editor can be used to create SQL queries. By selecting “New Query” from the toolbar, you can open a new query window where you can enter your SQL code and hit “Execute” to run it.
- Managing Security: You can control the security of your SQL Server instance using SSMS. You can manage security policies, add users and roles, and set permissions.
Steps for Using SQL Server Management StudioÂ
1. Start SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn’t open, you can open it manually by selecting Object Explorer > Connect> Database Engine.

Â
2. Right-click Databases, and then click New Database.
3. After filling in all fields, select Connect. You can also select Options to change additional connection options. Examples of connection options include the database to connect to, connection timeout value, network protocol, and so on. This article uses default values ​​for all fields.

Â
4. To verify that the SQL Server connection was successful, examine Object Explorer by expanding objects that display the server name, SQL Server version, and user name. These objects vary by server type.

Â
5. To create the database with default values, click New Query.

Â
Otherwise, continue with the following optional steps.
Query:
USE master
GO
IF NOT EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'TutorialDB'
)
CREATE DATABASE [TutorialDB]
GO
6. Execute the query by selecting Execute or selecting F5 on your keyboard.

Â
Limitations
- On a SQL Server instance, a maximum of 32,767 databases can be specified.
Recommendations
- Whenever a user database is created, altered, or deleted, the master database should be backed up.
- Create your database with the most extensive possible data files based on the maximum amount of data you anticipate storing there.
Using Transact-SQL
- Connect to the Database Engine.
- Open New Query.
- The following example should be copied and pasted into the query window before choosing Execute. The database Sales is created in this example. The first file (Sales_dat) becomes the primary file because the keyword PRIMARY isn’t used. The Sales_dat file uses MB and is allocated in megabytes because MB or KB aren’t specified in the SIZE parameter for the file. Because the MB suffix is expressly stated in the SIZE parameter, the Sales_log file is allocated in megabytes.
Conclusion
Effective database management in MS SQL Server requires understanding both system and user databases. By using T-SQL scripts or SSMS, users can efficiently create and manage databases, adhering to best practices to ensure scalability and data integrity.
Similar Reads
List All Databases in SQL Server
In SQL Server, databases are crucial for storing and managing data efficiently. Whether we are managing a large enterprise system or a small application, understanding how to list all the databases on our SQL Server is essential. In this article, we will write SQL queries that help us to retrieve al
3 min read
Delete Database in MS SQL Server
Prerequisite â Introduction of MS SQL Server and Create Database in MS SQL Server System databases can't be deleted, only user databases could be deleted. Data and log files will automatically be deleted from disk with database deletion. To delete a database, the below methods could be used â SQL Se
1 min read
PostgreSQL - Create Database
Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
5 min read
SQL CREATE DATABASE
Creating a database is one of the fundamental tasks in SQL and is the first step in structuring your data for efficient management. Whether you're a developer or a database administrator, understanding the CREATE DATABASE statement is essential. Understanding how to use this command effectively is c
6 min read
CREATE SCHEMA in SQL Server
A schema is a collection of database objects like tables, triggers, stored procedures, etc. A schema is connected with a user which is known as the schema owner. The database may have one or more schema. To create a schema in SQL Server use the 'CREATE SCHEMA' Statement. SQL CREATE SCHEMACREATE SCHE
2 min read
How to Open a Database in SQL Server?
Opening a database in SQL Server is a fundamental task for database administrators and developers. It involves establishing a connection to the server instance and selecting a database to work with. In this article, we will explore two methods to open a database in SQL Server such as using SQL Serve
3 min read
Minimal Permission to Create SQL Database
In SQL Server, the ability to create a database is governed by specific permissions and roles that can be granted to users. Understanding these permissions is essential for ensuring security and maintaining the principle of least privilege in our database environment. In this article, we will learn
4 min read
SQL Server - Database Objects
In SQL Server, database objects are essential components that allow to store and manage data effectively. These objects can range from tables and views to stored procedures and indexes. Understanding the various types of database objects is important for database design, management, and optimization
5 min read
SQL Server Show/List Databases
Listing all databases in SQL Server is a common task for database administrators and developers. SQL Server provides two main methods to solve this such as using SQL commands and using SQL Server Management Studio (SSMS). In this article, we will learn about how to Show/List the SQL Server Databases
4 min read
MySQL Create Database Statement
The MySQL CREATE DATABASE statement is used to create a new database. It allows you to specify the database name and optional settings, such as character set and collation, ensuring the database is ready for storing and managing data. In this article, we are going to learn how we can create database
4 min read