MySQL database queries
MySQL database queries
Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform operations on the database known as database queries. SQL has several queries for each work on data, and each query is used at a specific time and place.
The work of each query is different in MySQL. In this article, we will discuss the queries used for the database.
Create MySQL database
Here we will learn to create a database in MySQL. The database is like a giant box where many data are stored and divided into tables, columns, and rows.
MySQL understands SQL, so we will discuss the language used to create a database in MySQL. We will also run the syntax in MySQL to check the output.
The syntax used to create a database is:
CREATE DATABASE database_name;
Note: SQL command is not case sensitive to write in any way, but the statement should be correct.
The above statement is used to create a database in MySQL. In this, the‘database_name’ is the name of the database(name can be anything virtual or objective according to your choice).
For example: Let's create a database having the name SCHOOL in MySQL and check the result.
We cansee that by using the above syntax, the database SCHOOL has been created successfully.
Show MySQL database
After creating a database in MySQL, we have to check whether the database is created or not. We can do this by using another statement that isa show statement. After using this statement, all the database that has been created will be shown in a list.
Syntax to show the database:
SHOW DATABASES;
For example, we can check whether the above database SCHOOL is created or not as below.
In this image, we can see that the database SCHOOL is available in a list. It means the database is created successfully.
Use MySQL database
We can have several databases in MySQL, and we have to select any existing database to operate in it. Any operation that has to be performed in the table should be used only when the database is selected.
To select a database, we use the below syntax:
USE database_name;
For example, We have a database SCHOOL. To create a table or perform any other operation, we first need to select it. Let's use the syntax in MySQL and see the result.
Here we see that the database has been changed and ready to operate.
Delete MySQL database
This statement is used to delete the database from MySQL. After executing this statement, all the data, including tables, will be deleted permanently. MySQL provides the DROP statement to delete the database.
Syntax to delete the database:
DROP DATABASE database_name;
For example, we have a database SCHOOL. Let's delete this database using the below syntax in MySQL.
Here we see that the DROP statement deletes the database SCHOOL. If we want to use this database again, it shows the error.
Note: We must be careful while using the DROP statement because we cannot get it back once the database is deleted. This statement deletes the whole database permanently.