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
Before creating a database listing existing databases is beneficial. So we can get a list of existing databases and their names.
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
create database persons;
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.
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
CREATE TABLE persons (id int, name varchar(20), surname varchar(20));
Insert Data Into Table
We will insert a single row or record with the
INSERT INTO sql. We will insert the following values;
INSERT INTO persons VALUES (1,"poftut","com");
1 thought on “How To Create a Database and Table In MySQL and MariaDB?”