How To Create Database and Table In MySQL and MariaDB?
MySQL database is very popular database server used by a lot of small and big companies. In this tutorial we will look the basics of MySQL server like creating database and tables, populating data into tables. MySQL installation can be done with the following tutorial for Linux operating systems.
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 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 list of existing databases and their names.
We will create database but we should chose a different name from existing databases as we listed previous step. We will use
create database command for database creation by providing 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 database to associate the table with the database. Otherwise the table creation will fail because of no database is selected.
Now most important part we will create a table by giving related columns. We will use
CREATE TABLE command with the related column names. In this example we will create table named
persons with fields
CREATE TABLE persons (id int, name varchar(20), surname varchar(20));
Insert Data Into Table
We will insert single row or record with the
INSERT INTO sql. We will insert following values;
INSERT INTO persons VALUES (1,"poftut","com");