MySQL

How to Create Database in MySQL

In this tutorial, we are going to see how to create a database in MySQL. You must have special privileges to create or delete a MySQL database. Assuming you have access to the root user. To create a new database in MySQL, use CREATE DATABASE statement with the following syntax:

CREATE DATABASE [IF NOT EXISTS] databaseName
[CHARACTER SET charsetName]
[COLLATE collationName]

First, specify the name of the database after the CREATE DATABASE clause. The database name must be unique in the MySQL server instance. If you try to create a database with a name that already exists, MySQL generates an error. To avoid this error, you can specify the IF NOT EXISTS option. In this case, MySQL does not generate an error.

Thirdly, specify the charsetName and classification of the new database at the time of creation. If you avoid the “CHARACTER SET” and “COLLATE” clauses, MySQL uses the default character set and collation for the new database.
 

 

Access to MySQL shell

The minimum privilege needed to create a new database is CREATE. To access the MySQL shell, type the following command and enter your MySQL root user password when prompted:

$ mysql -u root -p

 

How to Create Database in MySQL

To see the existing databases on the server to ensure that you are not creating a new existing database, use the command SHOW DATABASES;:
 

 

 
Creating a new MySQL database is also simple, you only need to run one command.

To create a new MySQL database, run the following command, where “my_database” is the name of the database you want to create:

mysql> CREATE DATABASE my_database;


 
Finally, to access the newly created database, use the following command:

mysql> USE my_database;


 
You can now start creating tables and other objects in “my_database” database.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *