0% found this document useful (0 votes)
4 views

MySQL-class-2

The document provides an overview of how to create, access, list, and delete databases in MySQL. It explains the use of commands such as CREATE DATABASE, SHOW DATABASES, and DROP DATABASE, along with their syntax and examples. Additionally, it highlights the importance of case sensitivity in database names and the option to use pattern matching for listing databases.

Uploaded by

Candy Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MySQL-class-2

The document provides an overview of how to create, access, list, and delete databases in MySQL. It explains the use of commands such as CREATE DATABASE, SHOW DATABASES, and DROP DATABASE, along with their syntax and examples. Additionally, it highlights the importance of case sensitivity in database names and the option to use pattern matching for listing databases.

Uploaded by

Candy Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

A database is used to store the collection of records in an organized form.

It allows us to hold the data into tables, rows, columns, and indexes to find

the relevant information frequently. We can access and manage the records

through the database very easily.

MySQL implements a database as a directory that stores all files in the form

of a table. It allows us to create a database mainly in two ways:

MySQL Command Line Client

MySQL Workbench
MySQL Command Line Client
We can create a new database in MySQL by using the CREATE DATABASE statement
with the below syntax:

CREATE DATABASE [IF NOT EXISTS] database_name


[CHARACTER SET charset_name]
[COLLATE collation_name];
Parameter Explanation

The parameter descriptions of the above syntax are as follows:


Parameter Description
database_name It is the name of a new database that should be unique
in the MySQL server instance. The IF NOT
EXIST clause avoids an error when we create a
database that already exists.
charset_name It is optional. It is the name of the character set to
store every character in a string. MySQL database
server supports many character sets. If we do not
provide this in the statement, MySQL takes the default
character set.
collation_name It is optional that compares characters in a particular
character set.
Example
Let us understand how to create a database in MySQL with the help of an
example. Open the MySQL console and write down the password, if we
have set during installation. Now we are ready to create a database.
Here, we are going to create a database name "employeedb" using the
following statement:

mysql> CREATE DATABASE employeesdb;


It will look like the below output:
We can review the newly created database using the below query
that returns the database name, character set, and collation of the
database:

mysql> SHOW CREATE DATABASE employeedb;


We can check the created database using the following query:

mysql> SHOW DATABASES;

After executing the above query, we can see all the created
databases in the server.
Finally, we can use the below command to access the database that
enables us to create a table and other database objects.

mysql> USE emplyeedb;

NOTE: All the database names, table names, and table field names are
case sensitive. We must have to use proper names while giving any SQL
command.
MySQL SELECT Database

SELECT Database is used in MySQL to select a particular database


to work with. This query is used when multiple databases are
available with MySQL Server.

You can use SQL command USE to select a particular database.

Syntax:

USE database_name;

Example:

Let's take an example to use a database name "customers".


USE customers;
It will look like this:
It will look like this:

Note: All the database names, table names and table fields name are
case sensitive. You must have to use proper names while giving any
SQL command.
MySQL Show/List Databases

When we work with the MySQL server, it is a common task to show or list the
databases, displaying the table from a particular database, and information of
user accounts and their privileges that reside on the server. In this article, we are
going to focus on how to list databases in the MySQL server.

We can list all the databases available on the MySQL server host using the
following command, as shown below:

mysql> SHOW DATABASES;

Open the MySQL Command Line Client that appeared with a mysql> prompt.
Next, log in to the MySQL database server using the password that you have
created during the installation of MySQL. Now, you are connected to the MySQL
server host, where you can execute all the SQL statements. Finally, run the SHOW
Databases command to list/show databases.
We can see the following output that explains it more clearly:
MySQL also allows us another command to list the databases, which is a SHOW
SCHEMAS statement. This command is the synonyms of the SHOW DATABASES and
gives the same result. We can understand it with the following output:
List Databases Using Pattern Matching

Show Databases command in MySQL also provides an option that allows us


to filter the returned database using different pattern matching with LIKE
and WHERE clause. The LIKE clause list the database name that matches
the specified pattern. The WHERE clause provides more flexibility to list
the database that matches the given condition in the SQL statement.

Syntax

The following are the syntax to use pattern matching with Show Databases
command:

mysql> SHOW DATABASES LIKE pattern;


OR,
mysql> SHOW DATABASES WHERE expression;

We can understand it with the example given below where percent (%) sign
assumes zero, one, or multiple characters:

mysql> SHOW DATABASES LIKE "%schema";


The above statement will give the following output:

Sometimes the LIKE clause is not sufficient; then, we can make a more
complex search to query the database information from the schemata
table in the information schema. The information schema in MySQL is an
information database so that we can use it to get the output using the
SHOW DATABASES command.
mysql> SELECT schema_name FROM information_schema.schemata;
This statement will give the same result as the SHOW DATABASES
command:
Now, we are going to see how we can use the WHERE clause with the
SHOW DATABASES command. This statement returns the database whose
schema name starts with "s":

mysql>SELECT schema_name FROM information_schema.schemata WHERE


schema_name LIKE 's%';

It will give the following output:


MySQL DROP Database

We can drop/delete/remove a MySQL database quickly with the MySQL DROP


DATABASE command. It will delete the database along with all the tables,
indexes, and constraints permanently. Therefore, we should have to be very
careful while removing the database in MySQL because we will lose all the
data available in the database. If the database is not available in the MySQL
server, the DROP DATABASE statement throws an error.

MySQL allows us to drop/delete/remove a database mainly in two ways:

MySQL Command Line Client


MySQL Workbench
MySQL Command Line Client

We can drop an existing database in MySQL by using the DROP DATABASE


statement with the below syntax:

DROP DATABASE [IF EXISTS] database_name;


In MySQL, we can also use the below syntax for deleting the database. It is
because the schema is the synonym for the database, so we can use them
interchangeably.

DROP SCHEMA [IF EXISTS] database_name;

Parameter Explanation
The parameter descriptions of the above syntax are as follows:

Parameter Description
database_name It is the name of an existing database
that we want to delete from the server. It
should be unique in the MySQL server
instance.
IF EXISTS It is optional. It is used to prevent from
getting an error while removing a
database that does not exist.
Example
Let us understand how to drop a database in MySQL with the help of an
example. Open the MySQL console and write down the password, if we have
set during installation. Now we are ready to delete a database.
Next, use the SHOW DATABASES statement to see all available database in
the server:

Suppose we want to remove a database named "mytestdb_copy".


DROP DATABASE mytestdb_copy;
Now we can verify that either our database is removed or not by executing
the following query. It will look like this:

From the above, we can see that the database "mytestdb_copy" is


removed successfully.

You might also like