MySQL Create Database

Database is a repository of tables which belong to a specific domain. It can be a collection of tables for storing data for a bank, a college, an online shopping platform, a hospital etc. MySQL Create Database is used to create a blank database and you can add tables in it.

Syntax

CREATE DATABASE database_name;

Example

CREATE DATABASE employee_db;

You can give any name for database name. A database name must not have spaces but you can add underscore in a database name.

Database names are case sensitive. Employee_DB is different than employee_db.

Allowing access on Database

A database created with MySQL CREATE DATABASE statement must be given access to other users who need to use it. A user who creates a database is Owner of it and can give access privileges to other users. This can be done with GRANT statement.

GRANT [ALL|SELECT|INSERT|DELETE] ON database_name TO user_name;

Example

GRANT all ON employee_db TO Yames;

Use a Database

A user may have created a number of databases for different applications. Only one of them can be used at a time.  A database is to be created only once to use it as many times afterwards.

You need to make a specific database default to execute all the queries on the database. So, after you have created a database it can be set as default by using “USE” statement.

USE database_name;

Example

USE employee_db

Using the MySQL Create Database a user can create a database only when she has permission CREATE DATABASE on the database. You will get an “access denied for user username” error message when a user does not have the required permission.

If you want to know which databases you have the privilege on, use the statement SHOW DATABASES. This statement will display the names of all those databases after executing this statement. 

If you forget which database you were using you can do so with SELECT DATABASE() statement. You can read about these statements in detail here.