Thursday, August 15, 2013

How to Create Database in SQL Server


How to Create Database in SQL Server


Database is a storage location where we can store our business data. Database uses tables to store our information in a normalizes way so that, We can easily Select, Update and Delete the business data. In this article we will show you, How to Create Database in SQL Server, Renaming Database and Deleting Database in SQL with example.

How to Create Database in SQL Server 2014 with example

Before we start creating new database, Let us see the available databases. Below screenshot will show you the databases available in current instance.
How to Create Database in SQL Server 1
SQL Syntax to create Database is:
In this example, we are going to create new database called New_Database. So, Replace the Database_Name with New_database in SQL query window as shown below
Click on the Execute button to execute the create database command
How to Create Database in SQL Server 2
From the below screenshot you can observe that, Command is executed successfully and you can see the New_Database in our object explorer
How to Create Database in SQL Server 3
TIP: If you didn’t find the newly created database, Please click on the refresh button in object explorer
Let us see what will happen, When we execute the same command again. From the below screenshot you can observe that, it is throwing error saying: New_database already exists. Choose different database name.
How to Create Database in SQL Server 4
NOTE: In an organization, we may or may not have the privileges to know the available databases so, it is always advisable to check whether the database name already exists or not

How to Check whether Database name exists or not

This can be done in two ways:
Following statement will only execute Create Database Statement, if the New_database in not available in system database
SQL CODE
We just replaced the If Not Exists with If Exists and added select statement to display the message. STeps involved in the Following statement are:
  • If the New_database already exists then following query will display a message saying database already exists
  • Create database only execute if the New_database in not available in system database
ANALYSIS
Following statement will check for the database name New_database in system database.
If the database does not exists, then only following create statement will be executed
otherwise, following command will be executed. It’s going to display a message saying that the database already exists
OUTPUT
How to Create Database in SQL Server 5

How to Create Database in SQL Server using GUI

Within the object explorer, Right click on the Databases folder will open the context menu. Please select New database.. option from the context menu as shown below
How to Create Database in SQL Server 8
Once you select New database.. option, following window will be opened. Here we left Owner as default and database name as New_database as shown below.  Click OK to create new database
How to Create Database in SQL Server 9
Let us see what will happen, When we create the database with existing name. From the below screenshot you can observe that, it is throwing error
How to Create Database in SQL Server 10

How to Delete Database in SQL Server

To Delete database in SQL server, we can simple use the following syntax
Syntax for Deleting database or Dropping Database in SQL server is:
In this example we are going to delete New_Database So, within the SQL query window, Please write the following query
Let us see, What will happen when we execute the same command once again:
How to Create Database in SQL Server 6
From the above screenshot you can observe that, it is throwing an error saying: New_database doesn’t exists.
Better approach is check whether the database name exists or not because sometimes, your college or your team leader may deleted the database which you are trying to delete.
ANALYSIS
Following statement will check for the database name New_database in system database.
If the database exists, then only following drop statement will be executed
 OUTPUT
How to Create Database in SQL Server 7

How to Rename Database in SQL Server 2014

To rename database in SQL server, we can simple use the system stored proceduresp_renamedb
Syntax for renaming database in SQL server is:
In this example we are going to rename New_Database with New_Db So, within the SQL query window, Please write the following query
How to Create Database in SQL Server 11
Thank You for Visiting Our Blog