MySQL Database Tutorial

Python Programming
3 min readSep 4, 2021

MySQL Database

MySQL Database Tutorial

Before you start creating tables, you need a database, which is the container of tables. The database acts as a central point of administration for the tables in the database. The actual data is stored in the tables, which provide a structured organization for the data and maintain the integrity of that data.

To create tables, either you can create a new database or use an existing database. MySQL provides an empty database by the name test, which you can use to store your tables in. But it is recommended that you create new database before you create a group of some tables pertaining to a specific application. In the coming lines, you are going to learn how you can create databases, opening databases and removing databases in MySQL.

The first step in setting up a MySQL database is to create the actual database object, which serves as a container for the tables in that database.

1. Creating Databases

Creating databases is an easier task relatively. In simplest form the Create Database command takes following syntax.

CREATE DATABASE [IF NOT EXISTS] <database name> ;

The IF NOT EXISTS clause, if used, will first test whether a database by the mentioned name already exists or not. If it does, then create database command is simply ignored, otherwise a database with mentioned name is created.

Following are some example database creation commands :

CREATE DATABASE myDB ;

Creates database having name as MyDB

CREATE DATABASE IF NOT EXISTS myDB ;

Creates a database having name as MyDB, if their is no database by the name MyDB already existing.

2. Opening Databases

Creating database is not enough. Before you create tables in it, you need to open the database. To open a database, you simply need to write the statement as per following syntax.

Syntax

The only thing you need to ensure before opening a database is that it must already exist i.e., it must be already created. To check the names of existing databases, you may write following syntax.

The only thing you need to ensure before opening a database is that it must already exist i.e., it must be already created. To check the names of existing databases, you may write following command :

SHOW DATABASES ;

3. Removing Databases

Sometimes, you need to remove a database when you don’t need it anymore. But before making this decision, do make sure that you don’t need data stored in different tables of the database. This is because, when you drop a database, all its tables also get removed along with the database.

To remove a database, you need to issue a command with following syntax.

Syntax :

DROP DATABASE <database name> ;

Note:

Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database!

DROP DATABASE Example

That is, to drop a database namely myDB, you have to write the code given below :

DROP DATABASE myDB ;

Now that you know about how to create and use databases, you can move on to creating tables and adding data to them.

Note:

Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database!

Read More

MySQL Drop Database

MySQL Create Table Statement

MySQL Commands with Examples Database in MySQL

Originally published at https://kamalsinghstarbooks.xyz.

--

--