MySQL-class-2
MySQL-class-2
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
MySQL implements a database as a directory that stores all files in the form
MySQL Workbench
MySQL Command Line Client
We can create a new database in MySQL by using the CREATE DATABASE statement
with the below syntax:
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.
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
Syntax:
USE database_name;
Example:
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:
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
Syntax
The following are the syntax to use pattern matching with Show Databases
command:
We can understand it with the example given below where percent (%) sign
assumes zero, one, or multiple characters:
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":
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: