
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL CREATE DATABASE Statement
A database is like a container that stores all the important elements of your project suh as tables, data, views, stored procedures, etc. This is usually the first step when you start with a new project or application that needs organised data storage.
For example, if you are developing a website or mobile app that manages user accounts, orders, messages, or any kind of information, you need a dedicated space to store and organize that data. That space is the database.
The SQL CREATE DATABASE Statement
The CREATE DATABASE statement in SQL is used to create a new database in your database management system (DBMS).
It is part of SQL's Data Definition Language (DDL), which is used to define and manage database objects like tables, databases, indexes, and more.
Syntax
Following is the syntax to create a database in SQL:
CREATE DATABASE database_name;
Here:
- CREATE DATABASE: It is the SQL command to create a new database.
- database_name: It is the name you want to give to your database. It should follow proper naming rules.
Example
Following is an example to create a database testDB using SQL CREATE DATABASE statement:
CREATE DATABASE testDB;
After executing the above command in MySQL database, we get the following output:
Query OK, 1 row affected (0.02 sec)
How to Verify the Database Was Created
You can check if the database was created by listing all existing databases using the SHOW DATABASES command as shown below:
SHOW DATABASES;
After executing the above command on MySQL server, if you see testDB in the list, then your database has been created successfully:
Database |
---|
information_schema |
library |
mysql |
performance_schema |
sys |
testdb |
Since testdb is listed, it confirms that the database has been successfully created.
How to Select and Use a Database
Before you can create tables or insert data into a database, you must tell the system which database to use. This is important because a database server (like MySQL or SQL Server) can host multiple databases at once. If you don't select a specific one, your commands may not know where to apply changes
Syntax
Following is the basic syntax to select and use a database:
USE database_name;
Example
In the following command, we select the database named testDB and set it as the active database for the current session:
USE testDB;
We get to see the output as shown below:
Database changed
Removing a Created Database
You may need to delete (remove) a database if you created it by mistake, don't need it anymore, or want to make a new one from the beginning. To do this, you can use the DROP DATABASE statement, which permanently deletes the database and all its contents.
Syntax
Following is the basic syntax to remove a database that was accidentally created:
DROP DATABASE database_name;
Example
In the following command, we delete the database named testDB:
DROP DATABASE testDB;
Following is the output obtained:
Query OK, 0 rows affected (0.04 sec)
SQL CREATE DATABASE with IF NOT EXISTS Clause
If you try to create a database that already exists, SQL will return an error. To avoid this, you can use the IF NOT EXISTS clause with the CREATE DATABASE statement.
This clause tells SQL to create the database only if it doesn't already exist, helping you avoid errors during execution.
Syntax
Following is the syntax to create a database only if it does not already exist, using the IF NOT EXISTS clause in SQL:
CREATE DATABASE IF NOT EXISTS database_name;
Example
The following command creates a database named testDB only if it doesn't already exist:
CREATE DATABASE IF NOT EXISTS testDB;
Following is the output of the above code:
Query OK, 1 row affected, 1 warning (0.00 sec)
Rules for Naming a Database
When naming your database in SQL, it is important to follow standard rules to avoid errors:
- Use only letters, numbers, and underscores (_).
- Don't use spaces or special characters like @, #, %, etc.
- Stick to lowercase or snake_case format (e.g., school_system).
- Make sure the database name is unique in your system.
Example: Valid Database Names
Following are some examples of names that follow the proper rules:
- CustomerDB
- employee_data
- inventory2025
Example: Invalid Database Names
Following are some examples of names that break the naming rules and should be avoided:
- 123database (starts with a number)
- user data (contains a space)
- db@info (contains a special character)