SQL Create Database

The CREATE DATABASE command is used to create a database.

This will add a new database to the server with associated files and filegroups.

To remove a database use DROP DATABASE.

Example

#

This example creates a new database named Inventory.

CREATE DATABASE Inventory

Syntax

Syntax to create a new database.

CREATE DATABASE database-name

Syntax to drop (delete) a database.

DROP DATABASE database-name

Note: Dropping a database will also delete all its content with data and database objects.

Syntax to rename a database.

ALTER DATABASE database-name 
MODIFY NAME = modified-database-name 

For ALTER to work, the database needs to be in single-user mode (see example below).


More Examples

DROP DATABASE

Problem: Remove the Inventory database.
DROP DATABASE Inventory
Result:  database dropped

RENAME DATABASE

Problem: Rename the Inventory database to Distribution.
USE master;  
ALTER DATABASE Inventory SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE Inventory MODIFY NAME = Distribution;
ALTER DATABASE Distribution SET MULTI_USER;

The database is placed in single-user mode, and then back into multi-user mode.

Result:  database renamed

You may also like



Guides


vsn 3.1