Creating and Using a MySQL Database

MySQLMySQLi Database

Once MySQL has been installed, and the user is aware of how SQL statements need to be entered, a database can be accessed. Let us understand the steps needed to retrieve data from tables in a database −

  • Connect to the server

  • Create a database

  • Create a table

  • Load the data into the table

  • Retrieve data from the table

Before creating a database, it is important to know the databases that are already present in the environment. It can be found using the below query −

Query

mysql> SHOW DATABASES;

Output

+--------------+
| Database     |
+--------------+
| mysql        |
| test         |
| tmp          |
+--------------+

The above query will display the names of all databases that are present in the environment. The ‘mysql’ database tells about the user access privileges. The ‘test’ database is available like a sample workspace for users to experiment on things.

A database can be created using the below statement −

CREATE DATABASE databaseName;

If we wish to access and use a specific database, we can use the following query −

mysql> USE databaseName
Database changed

The ‘USE’ statement doesn’t require a semi-colon. This is similar to the ‘QUIT’ statement. Even if semi-colon is used, it does no harm.

We can create and use a database of our own, but before that, MySQL administrator’s permission is required.

raja
Published on 08-Mar-2021 12:14:46
Advertisements