How to Create a Database in SQL Server Using Query?

In this SQL Server tutorial, I will show you how to create a database in SQL server using query.

You will understand the syntax to create a database in SQL Server. Also, you will learn about where to execute SQL Query on SQL Server Management Studio. Finally, you will create a database with examples.

Create Database in SQL Server Using Query

To create a database in SQL Server using a query, you can use the SQL Server Management Studio (SSMS), which means you can also run the query in SQL Server Management Studio that you run on the command line.

Create Database in SQL Server Using Query on SSMS

Open SQL Server Management Studio and connect to SQL Server instance. To run the query, SQL Server provides a Query window where you can execute the SQL server query to interact with databases.

There are different ways to open the New Query window in SQL Server. The simplest way is to press ‘CTRL + N’ from your keyboard, and it opens the New Query window.

Other ways are shown in the below picture.

Create Database in SQL Server Using Query on SSMS Opening New Query

In the above picture, you can select the New Query option from the toolbar of SSMS or select your SQL Server instance, right-click on it, and then select the New Query from the options.

When you follow any of the ways to open New Query, you see the Query Editor window area, as shown in the below picture.

Create Database in SQL Server Using Query on SSMS New Query Window

To create a database in SQL Server using a query, you need to follow the below syntax:

CREATE DATABASE new_database_name;

Where,

  • CREATE DATABASE: It is the command to create a new database on SQL Server.
  • new_database_name: The name of the database that you want to create; remember to always follow best practices while specifying the database name.

Now go to your Query Editor area and write the below query to create a database named ‘Products’.

CREATE DATABASE Products;
Create Database in SQL Server Using Query on SSMS

To execute the query ‘CREATE DATABASE PRODUCTS’, click on the Execute button as shown in the above picture or just press F5 from your keyboard.

As soon as you execute the query, it shows a message ‘Commands completed successfully’, which means the database is created in SQL Server as shown in the below picture.

Create Database in SQL Server Using Query on SSMS Executing Query

To check the created database ‘Products’, expand the Database node from the Object Explorer panel, and you will see the newly created database as shown in the below picture.

Create Database in SQL Server Using Query on SSMS Viewing Database

If you don’t see the newly created database, then refresh the Object Explorer by clicking on the reload icon, as shown in the above picture.

Conclusion

In this SQL Server tutorial, you learn how to create a database using a query in SQL Server Management Studio.

You may also like: