English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to create a database in a relational database management system (such as MySQL, SQL Server, etc.) using SQL.
Before we process any data, we must first create a database. We assume that you already have a MySQL or SQL Server available to use and have all necessary privileges. If not, please checkIntroduction Guide.
The SQL CREATE DATABASE statement is used to create a database.
The basic syntax for creating a database can be given as follows:
CREATE DATABASE database_name;
The following SQL statement creates a database nameddemodatabase:
CREATE DATABASE demo;
Creating a database does not select it for use. Therefore, we must select the target database with the USE statement before proceeding. For example, the USE demo; command willDemonstrationThe database is set as the target database for all future commands.
Note:In UNIX, database and table names are case-sensitive, so you must always refer to your database demo, not Demo, DEMO, or anything else. However, SQL keywords are not case-sensitive, like CREATE DATABASE and create database.
Let's use the command line tool to create a database in MySQL.
To call the MySQL command line, we must first log in to the MySQL server. To log in as the root user, please type the following command in the terminal and press Enter. The system will prompt you to enter a password. Enter the password and press Enter key, if correct, mysql> will appear prompt, through which you can issue SQL statements and view the results.
shell> mysql -u root -p
Now, execute the following command to create a database nameddemodatabase.
mysql> CREATE DATABASE demo;
If the database is created successfully, you will see output similar to the following:
Query OK, 1 row affected (0.03 sec)
If you try to create a database that already exists, you will receive an error message. To avoid this, you can use the optional clause IF NOT EXISTS in MySQL, as shown below:
mysql> CREATE DATABASE IF NOT EXISTS demo;
Type the following command and then press Enter. You will see the output"Database Changed". Now, ourDemonstrationThe database has been selected as the default database for all future operations.
mysql> USE demo;
Tip:To view a list of existing databases on the MySQL server, you can execute the following statement in the command line: SHOW DATABASES;