How To Create a Database and Table In MySQL and MariaDB?


MySQL database is a very popular database server used by a lot of small and big companies. In this tutorial we will look at the basics of MySQL server like creating databases and tables, populating data into tables. MySQL installation can be done with the following tutorial for Linux operating systems.

How to Install Mariadb/Mysql Server in Linux Fedora?

The following operations can be done in MariaDB without any problem. MariaDB is the fork of the MySQL database and it is compatible with MySQL.

Connect To MySQL Database

There are different tools to manage MySQL servers. We can use  GUI tools like MySQL tools, Heidi, Toad, or command-line tools provided by MySQL package. In this tutorial, we will use the command-line tools provided by MySQL. First, we use mysql command to connect database

$ mysql -u root -p
Connect To MySQL Database
Connect To MySQL Database

List Databases

Before creating a database listing existing databases is beneficial. So we can get a list of existing databases and their names.

show databases;
List Databases
List Databases

Create Database

We will create a database but we should choose a different name from existing databases as we listed the previous step. We will use create database command for database creation by providing a database name. In the example, we create a database named person .

create database persons;
Create Database
Create Database

Select Database

After database creation, we will create a table. But we should select the database to associate the table with the database. Otherwise, the table creation will fail because no database is selected.

use persons;
Select Database
Select Database

Create Table

Now the most important part we will create a table by giving related columns. We will use the CREATE TABLE command with the related column names. In this example, we will create a table named persons with fields id , name , surname .

CREATE TABLE persons (id int, name varchar(20), surname varchar(20));
Create Table
Create Table

Insert Data Into Table

We will insert a single row or record with the INSERT INTO sql. We will insert the following values;

  • id 1
  • name poftut
  • surname com
INSERT INTO persons VALUES (1,"poftut","com");
Insert Data Into Table
Insert Data Into Table

LEARN MORE  What Is Relational Database Management System (RDMS)?

1 thought on “How To Create a Database and Table In MySQL and MariaDB?”

Leave a Comment