0% found this document useful (0 votes)
9 views

SQL Server Database_ Create, Alter, & Drop Database in SQL

Uploaded by

HoaHoang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Server Database_ Create, Alter, & Drop Database in SQL

Uploaded by

HoaHoang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

SQL Server Database: Create, Alter, & Drop Database in SQL


By : Fiona Brown Updated June 28, 2024

Comarch
hcramo What is Database?

ramoC
Get Your Free
A Database is a collection of objects such as tables, views, stored
procedures, triggers, functions, etc.

AI Loyalty Consider a with a few examples in real life:

Ebook We have Bookcase where Books resides,


We have homes where we live,
We have parking lots where vehicles are parked & examples are
Download
countless.

Similarly, we have DATABASE which is a kind of home for all our tables, views, stored procedures, etc which are
interlinked.

Technically, Database store the data in a well-organized manner for easy access and retrieval. In SQL Server,
there are two types of databases:

1. System Databases: The system databases are created automatically for you when you install the SQL Server.
They play a crucial role in the server, especially in ensuring that database objects run correctly. Examples of
SQL Server system databases include: Master, MSDB, Model, Tempdb, Resource

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 1/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

2. User Databases: The user databases are created by the database users like you who have been granted
access to create databases

Table of Content:

Rules to Create a Database


First, we need to know the basic rules for creating new DB:

Database names must be unique within an instance of SQL Server.


Database names can be a maximum of 128 characters.
The CREATE DATABASE statement must run in an auto-commit mode.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 2/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Rules to Create a Database

There are 2 ways to create Database in SQL server.

1. SQL Server Management Studio


2. Transact-SQL

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 3/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

How To Create Database in SQL Server Management Studio


Here is a step by step process to create a database in SQL server management studio:

Step 1) Right Click on ‘Database’ from ‘Object Explorer’ window and then select ‘New Database.’

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 4/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Step 2) Below ‘New Database’ screen will appear. Enter ‘Database name’.Note that: ‘Logical name’ column will
be auto-populated with:

1. Edu | Filetypes: Rows and Filegroups: PRIMARY


2. Edu_log | Filetypes: LOG and Filegroups: ‘Not Applicable’

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 5/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Here:
A) Edu | Filetypes: Rows and Filegroups: PRIMARY is .mdf file

B) Edu_log | Filetypes: LOG and Filegroups: ‘Not Applicable’ is .ldf file

Step 3) (Optional) For more Complex settings, we can navigate to ‘Options’ and ‘Filegroups’.At Beginner level,
creating Database from General Tab will suffice.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 6/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Step 4) Click on ‘Add’.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 7/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Result: ‘Edu’ Database Created.

We can expand Database – ‘Edu’ which will contain Tables, View, etc. These are initially blank until the user
creates new Table, views, etc.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 8/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

View the Source Query:


You can view the source query of the newly created ‘Edu’ Database as:Navigation: Right Click on Database
name>Script Database as> CREATE To> New Query Editor Window.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 9/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Query Window:

Create Script:

USE [master]
GO

CREATE DATABASE [Edu]


https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 10/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Edu', FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH =
65536KB )
LOG ON
( NAME = N'Edu_log', FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH =
65536KB )

Create Database with T-SQL


Another method is to write the T-SQL query to Create a Database and execute it.

Let’s have a look at most Simple Database Creation T-SQL query.

Syntax:

CREATE DATABASE <Database_name>

Query:

CREATE DATABASE [Edu_TSQL_file]

Click on ‘Execute’
https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 11/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Result: We can see Edu_TSQL created in SQL Object Explorer.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 12/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

RELATED ARTICLES
→ SSRS Tutorial: What is SQL Server Reporting Services?
→ SQL Variables: SQL Server Declare, Set and Select Variable
→ Substring() in SQL Server: How to use Function with Example
→ Top 40 SSIS Interview Questions and Answers (2024)

Let’s have a look when we want to Create Database with .mdf and .ldf file. Here, we can give the location as an
implicit part of our query.

Syntax:

CREATE DATABASE database_name


[ CONTAINMENT = { NONE | PARTIAL } ]
[ ON
[ PRIMARY ] <filespec> [ ,...n ]
[ , <filegroup> [ ,...n ] ]
[ LOG ON <filespec> [ ,...n ] ]
];

Query:

CREATE DATABASE [Edu_TSQL_file]


CONTAINMENT = NONE

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 13/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

ON PRIMARY
( NAME = N'Edu_TSQL_file', FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu_TSQL_file.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED,
FILEGROWTH = 65536KB )
LOG ON
( NAME = N'Edu_TSQL_file_log', FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL14.SQL_MS\MSSQL\DATA\Edu_TSQL_file_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB ,
FILEGROWTH = 65536KB )

How to Alter Database


Like Create Database Query we can also Alter Database. We can rename database name, change file location and
setting, etc.

Basic rules for Altering new DB:

The ALTER DATABASE statement must run in an auto-commit mode.


ALTER DATABASE is not allowed in an explicit or implicit transaction.

There are 2 ways to Alter Database in SQL server.


https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 14/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

1. SQL Server Management Studio


2. Transact-SQL.

How To Alter Database in SQL Server Management Studio


Below are the steps to alter database in SQL server management studio:

Let’s try to Alter Name of our Pre-Created Database ‘Edu’.

Step 1) Rename the Database


Right click on Database name. Click on ‘Rename’.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 15/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Step 2) Enter the New Database Name


Database name will be editable. Enter the new Name and Press Enter.
https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 16/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Result: The Database is now renamed as “Edu_Alter” from ‘Edu.’

Alter Database with Transact-SQL


Now Let’s Alter Database using T-SQL

Syntax:

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 17/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

ALTER DATABASE <Databse_name>


MODIFY NAME = <New Name>

Query:

ALTER DATABASE Edu_TSQL


MODIFY NAME = Edu_TSQL_Alter;

Run the above query by clicking on ‘Execute’.

Result: The Database is now renamed as “Edu_TSQL_Alter” from ‘Edu_TSQL’.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 18/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

General Syntax:

ALTER DATABASE { database_name | CURRENT }


{ MODIFY NAME = new_database_name
| COLLATE collation_name
| <file_and_filegroup_options>
| SET <option_spec> [ ,...n ] [ WITH <termination> ]
| SET COMPATIBILITY_LEVEL = { 140 | 130 | 120 | 110 | 100 | 90 }
} ;

Changing .mdf/.ldf file name


https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 19/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Query:

Alter DATABASE Edu_TSQL_Alter;


MODIFY FILE ( NAME = Edu_TSQL, NEWNAME = Edu_TSQL_newName );

Changing .mdf/.ldf file location


Query:

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 20/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Alter DATABASE Edu_TSQL_Alter;


MODIFY FILE ( NAME = Edu_TSQL_NewName, FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL14.SQL_MS\MSSQL\DATA\New_File\Edu_TSQL_log.ldf' );

Delete Database
There are 2 ways to Delete Database in SQL server.

1. SQL Server Management Studio


2. Transact-SQL.

How To Drop Database in SQL Server Management Studio


Following is the process to drop a database in SQL server management studio:

Let’s try to Delete our Pre-Created Database ‘Edu_Alter.’


https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 21/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Step 1) Right click Database. Click on ‘Delete’ and then ‘OK.’

Result: ‘Edu_Alter’ is deleted from ‘Object Explorer’ Database list.

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 22/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Delete Database using Transact-SQL


Let’s try to Delete our Pre-Created Database ‘Edu_TSQL_Alter.’

Syntax:

DROP DATABASE <Databse_name>

Query:

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 23/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

USE master;
GO
DROP DATABASE Edu_TSQL_Alter;
GO

Run the above query by clicking on ‘Execute.’

Result: ‘Edu_TSQL_Alter’ is deleted from ‘Object Explorer’ Database list.

Restore Database in SQL Server


https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 24/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

You can create a database by restoring a database you had backed up earlier. It can be done by running the
restore database command which takes the following syntax:

restore Database <database name> from disk = '<Backup file location + filename>

The query should be executed within the query window just like out previous command. For example:

restore database Edu from disk = 'C:\Backup\Edu_full_backup.bak'

You can also use the GUI Navigation: Right Click Database>Restore Databases>Device> Import file>Click Ok.

Summary
We can use both SQL Management GUI and T-SQL to perform all the three operations; Create, Alter and
Delete Database.
A maximum of 32,767 databases can be specified on an instance of SQL Server.
System Databases cannot be deleted.
Create, Alter & Drop: All operations are case insensitive. We can use both upper and lower case as a syntax.

You Might Like:

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 25/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

SQL
SQL Server What is SQL
Architecture Server?
(Explained)
(Explained) Introduction,…
SQL Server
Server Top 50 SQL Server
CREATE, ALTER, Interview
DROP
DROP Table… Questions…

Prev Report a Bug Next

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 26/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Do you think
AI is future of
Work?
Yes No

Top Tutorials

About
About Us
Advertise with Us
Contact Us

Python Testing Hacking


https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 27/28
12/6/24, 6:32 PM SQL Server Database: Create, Alter, & Drop Database in SQL

Career Suggestion
SAP Career Suggestion
Tool
Software Testing as a
Career
Xero
RemotePC SAP Java SQL
QuickBooks

Interesting
eBook
Blog
Quiz
SAP eBook
Privacy Manager
Selenium Build Website VPNs

English © Copyright - Guru99 2024 Privacy Policy | Affiliate Disclaimer |


ToS | Editorial Policy

https://www.guru99.com/sql-server-database-create-alter-drop-restore.html 28/28

You might also like