How to Create Database in SQL Server

In this tutorial, we will show you how to create a Database in SQL Server. There are two ways by which you can create a Database in SQL Server. One is using the GUI Tools of the SQL Server Management Studio. The other option is to use the Create database SQL Query.

Connect to the database using SSMS

Open and Connect to SQL Server using the management studio. If you do not have the SQL server installed, then you must install the SQL Server & also install SQL Server Management Studio

Using Create database statement

The simplified syntax of the Create Database statement is as follows.

Where database_name is the name of the database. The name of the database must be adhere to rules of naming identifiers

For Example to create a database with the name Sales run the following command

To Create the database

  1. Open the Query Editor by clicking on the Toolbar → New Query or from the menu File → New → Query with Current Connection.
  2. Enter the Query Create database Sales
  3. Execute the query either by pressing F5 key or click on Execute option in the toolbar.
  4. Commands completed Successfully message appears in the message window.
  5. Right, Click on the Database folder in Object Explorer.
  6. Click on Refresh.
  7. You will see the newly created database Sales
Create Database in SQL Server Using SQL Query

Create Database Using SQL Server Management Studio

The another way to create database is to use the GUI Tools. Follow these steps.

  1. Select the Database node in object explorer
  2. Right Click and click on the new database option
  3. In the New Database dialog box, enter the name of the database as Sales
  4. Click ok
Create Database using GUI Tools in SSMS
Create Database using GUI Tools in SSMS

Database Name must be Unique

The database name must be unique in an Instance. If you try to create a database with the same name server will throw error.

View Currently installed Database

You can view the list of Installed database either from the SSMS or from using the Query

Using SSMS

  1. Open SSMS and Open the Object Explorer ( Menu → View → Object Explorer )
  2. Connect to an instance of the SQL Server Database Engine ( Connect → Database Engine )
  3. Expand that instance.
  4. Go to the folder databases and expand it to view the list of databases.
  5. Expand the System databases and you will see the master, model, msdb & tempDb databases

Using Query

  1. Open the Query (Toolbar → New Query ) or (Menu → File → New → Query with Current Connection)
  2. Enter the query, which is mentioned below
  3. Click on Toolbar → Eexcute or Press F5

The databases (master, model, msdb & tempDb) are automatically created by the SQL Server, when we install the SQL Server.

Location of the Database in the disk

The SQL Server Creates at least one data file & one transaction log file, when you Create a new database. You can find out the location of the files either from the SSMS or using the SQL Query

Using SQL Query

Location of database file in SQL Server Using Query
Location of database file in SQL Server Using Query

Using SSMS

  1. Select Database
  2. Right Click and click on Properties
  3. Click on Files in Database Properties Window
Select Database Properties in SSMS
Select Database Properties in SSMS
Location of database file in SQL Server Using SSMS
Location of database file in SQL Server Using SSMS

Syntax of Create Database

Here is the complete syntax of the Create Database statement. You can set many options like location of the data files, File Groups, File size, Growth Size, Max Size etc. Will cover all these in our future tutorials.

References

Create Database API

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top