Open In App

Create Database in MS SQL Server

Last Updated : 12 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Introduction of MS SQL Server

Databases are a collection of objects like tables, views, stored procedures, functions, etc. In MS SQL Server, two sorts of databases are available.

  1. System databases
  2. User Databases

System Databases

System databases are created automatically once we install the MS SQL Server. Below is a list of system databases –

  1. Master
  2. Model
  3. MSDB
  4. Tempdb
  5. Resource (Introduced in the 2005 version)
  6. Distribution (It’s for the Replication feature only)

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 –

  1. SQL Server Management Studio.
  2. 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:

  1. Open a new query window in SSMS by clicking on “New Query” in the toolbar.
  2. Type the following command to restore a database from a backup file.
  3. 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

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

 

5. 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

  1. On a SQL Server instance, a maximum of 32,767 databases can be specified.

Recommendations

  1. Whenever a user database is created, altered, or deleted, the master database should be backed up.
  2. Create your database with the most extensive possible data files based on the maximum amount of data you anticipate storing there.

Using Transact-SQL

  1. Connect to the Database Engine.
  2. Open New Query.
  3. 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.
IMG1

 



Previous Article
Next Article

Similar Reads

Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Backup Database in MS SQL Server
Prerequisite – Create database in MS SQL Server To Create a Full Database Backup, the below methods could be used - SQL Server Management Studio. Transact-SQL. Restriction : Backups created on a newer version of SQL Server cannot be restored in previous versions of SQL Server. Facts to know : When the database size increases, full database backups
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 Server Management Studio. Transact-SQL. These are ex
1 min read
How to Restore SQL Server Database From Backup?
A Database is defined as a structured form of data that is stored database a computer or data in an organized manner and can be accessed in various ways. It is also the collection of schemas, tables, queries, views, etc. Databases help us with easily storing, accessing, and manipulating data held on a computer. The Database Management System allows
2 min read
How to Import and Export SQL Server Database?
SQL Server is very popular in Relational Database and it is used across many software industries. In MS SQL Server, two sorts of databases are available. System databasesUser DatabasesIn this, we will learn to export and import SQL Server databases using Microsoft SQL Server. Exporting and Importing stands as a backup plan for developers. Step 1: O
2 min read
Check whether a Table exists in SQL Server database or not
Before creating a table, it is always advisable to check whether the table exists in the SQL Server database or not. Checking for table existence before creation helps in avoiding duplication errors, ensures data integrity, and enables efficient database management. There are multiple methods in SQL Server to check if a table already exists in a da
3 min read
SQL Server Query to Find All Permissions/Access for All Users in a Database
Permissions in SQL Server determine what actions a user can perform on database objects. There can be multiple users in a database, so keeping track of granted permissions is necessary to keep the database safe. In this article, we will look at methods to find all permissions/access for all users in a database. How to Find All Permissions/Access fo
2 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
Article Tags :