SQL - CREATE Database



A database is a structured collection of data that is stored in a computer system. They are used to store and retrieve the data efficiently. Databases can be created using different query languages, and SQL is one such language.

CREATE Database Statement

The CREATE DATABASE statement is a DDL (Data Definition Language) statement used to create a new database in SQL. If you are creating your database on Linux or Unix, then database names are case-sensitive, even though SQL keywords are case-insensitive. If you are working on Windows then this restriction does not apply.

Syntax

Following is the syntax to create a database in SQL

CREATE DATABASE DatabaseName;

Here, the DatabaseName is the name of the database that we want to create. The database name can contain any valid identifiers, such as number, letters, or underscores. But a DatabaseName cannot be a keyword available in SQL.

While creating a database, you may encounter an error such as ERROR 1044 (42000): Access denied for user 'krishna'@'localhost' to database 'DatabaseName', this means that you do not have the necessary privileges to create a database. To create a database, you need to have admin previleges.

Example

Following is an example to create a database testDB using SQL CREATE DATABASE statement −

CREATE DATABASE testDB;

List Databases using SQL

Once the database testDB is created, you can check it in the list of databases using SQL command SHOW DATABASES;.

Syntax

SHOW DATABASES;

Output

The output will be displayed as −

Database
master
performance_schema
information_schema
mysql
testDB

Use/Select Databases using SQL

We can now set the testDB as the default database by using the USE statement in SQL.

Syntax

USE testDB;

That's it! we have successfully created a database in SQL. Now, we can create tables and other database objects within this new database.

Advertisements