MySQL Database Tutorial

MySQL Database

MySQL Database Tutorial
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

USE <databasename> ;

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.

USE myDB ;

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

Frequently Asked Questions

What type of database is MySQL?

relational database

MySQL databases are relational.

A relational database stores data in separate tables rather than putting all the data in one big storeroom. The database structures are organized into physical files optimized for speed.

How do I create a database in MySQL?

Create a MySQL Database Using CLI
SSH into your server.
Log into MySQL as the root user.
Create a new database user: GRANT ALL PRIVILEGES ON *. …
Log out of MySQL by typing: \q .
Log in as the new database user you just created: mysql -u db_user -p. …
Create a new database:

Is MySQL a database?

MySQL (/ˌmaɪˌɛsˌkjuːˈɛl/) is an open-source relational database management system (RDBMS). Its name is a combination of “My”, the name of co-founder Michael Widenius’s daughter, and “SQL”, the abbreviation for Structured Query Language.

How do I display a database in MySQL?

The most common way to get a list of the MySQL databases is by using the mysql client to connect to the MySQL server and run the SHOW DATABASES command. If you haven’t set a password for your MySQL user you can omit the -p switch.

Why we use MySQL database?

One of the reasons MySQL is the world’s most popular open source database is that it provides comprehensive support for every application development need. … MySQL also provides connectors and drivers (ODBC, JDBC, etc.) that allow all forms of applications to make use of MySQL as a preferred data management server.

You may also like...

1 Response

  1. Wow, superb blog format! How lengthy have you ever been running a blog for? you make blogging look easy. The overall look of your website is excellent, as well as the content!!

Leave a Reply

Your email address will not be published. Required fields are marked *