MySQL Create Database

What is database?

A database is collection of objects. Objects can be tables, views, stored procedures, triggers, functions, indexes,…

CREATE DATABASE is the command used to create database in MySQL Server.

Syntax to MySQL Create Database

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name | DEFAULT ENCRYPTION [=] {'Y' | 'N'}

Examples to MySQL Create Database

1. Create database by command line:

CREATE DATABASE r2schools;

2. To know the list of databases in MySQL Server:

show databases;

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | r2schools | | sys | +--------------------+ 5 rows in set (0.00 sec)

3. Switch(select) to new created database:

use r2schools;

4. Lets verify present connected database name:

select database();

mysql>mysql> select database(); +------------+ | database() | +------------+ | r2schools | +------------+ 1 row in set (0.00 sec)

Visual presentation of all above commands:

MySQL Create Database

5. Lets verify the list of tables available in a database:

show tables;

mysql>mysql> show tables; +---------------------+ | Tables_in_r2schools | +---------------------+ | emp | +---------------------+ 1 row in set (0.01 sec)

2. Create database by using MySQL Workbench

1. Open MySQL workbench, then in the query window type the above CREATE DATABA command

or

2. Or click on Create Database button as shown in the below video.