Mariadb Create Table – How to Guide (Step by Step). In this post we will introduce what MariaDB is and its main feature and how to create a table. MariaDB provides a create table statement to the user where we are able to create a table with the assigned name of the table. There are various ways to create a table and where we want to store the table will depend on the user. Let’s introduce MariaDB first!
What is MariaDB
MariaDB is a free, open source and one of the most popular database management system around the world. Its high performance, ease of use and data security makes it a popular database solution.
MariaDB server was primarily designed to ensure that the MySQL codebase was free for everyone. It is a fork of the MySQL database management system and offers data processing capabilities for both small and enterprise tasks.
MariaDB has a pluggable storage engine capabilities that allows developers to use a single database in a more flexible manner. You can concentrate on creating individual tables to suit a specific business need. Databases are containers for objects with a user defined structure. One type of object is a table.
MariaDB table is used to organize data in the form of rows and columns. It is similar to worksheets in the spreadsheet for storage and showing records in the structure format.
MariaDB Database consists of tables which are a containers with a logical structure in a row and column format. Each row represents a record while each column represents a field or property of that record.
Follow this post how in MariaDB to Create Table – How to Guide.
To create a table in MariaDB, first you will need to open a terminal interface and log in to the MariaDB shell. You can do it using the following command:
mysql -u root -p
You will be asked to provide a MariaDB root password. Once you are connected to the MariaDB, you will get the following shell:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 44
Server version: 10.5.12-MariaDB-0+deb11u1 Debian 11
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Next, create a database named books using the CREATE DATABASE statement:
MariaDB [(none)]> CREATE DATABASE books;
Please verify your database using the following command:
MariaDB [(none)]> SHOW DATABASES;
You should see your created database in the following output:
Next, switch the database to books and create a table named books using the CREATE TABLE statement:
MariaDB [(none)]> USE books;
MariaDB [books]> CREATE TABLE books(title VARCHAR(50) NOT NULL,author VARCHAR(30) NOT NULL,published_year INT NOT NULL,PRIMARY KEY(title));
Print the table structure using the following command:
MariaDB [books]> DESCRIBE books;
You will get the following output:
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| title | varchar(50) | NO | PRI | NULL | |
| author | varchar(30) | NO | | NULL | |
| published_year | int(11) | NO | | NULL | |
+----------------+-------------+------+-----+---------+-------+
Following that is to verify the created table using the following command:
MariaDB [books]> SHOW tables;
You should see your table in the following output:
You can also create a table in MariaDB using the script. To do so, first, create a file named books.sql using the following command:
nano books.sql
Add the following statements:
CREATE DATABASE books1;
USE books1;
CREATE TABLE books1(title VARCHAR(50) NOT NULL,author VARCHAR(30) NOT NULL,published_year INT NOT NULL,PRIMARY KEY(title));
INSERT INTO books1 VALUE ("Ulysses", "James Joyse", 1923);
INSERT INTO books1 VALUE ("Catch-22", "Joseph Heller", 1990);
INSERT INTO books1 VALUE ("Robinson Crusoe", "Daniel Defoe", 1719);
INSERT INTO books1 VALUE ("Tom Jones", "Henry Fielding", 1749);
INSERT INTO books1 VALUE ("Emma", "Jane Austen", 1816);
Save and close the file when you are finished. Then, run the books.sql file into the MariaDB using the following command:
mysql -u root -p < books.sql
Provide your MariaDB root password to execute the script.
Next, log in to MariaDB using the following command:
mysql -u root -p
Once you are logged in, switch the database to books1 and verify your books1 table using the following command:
MariaDB [(none)]> USE books1;
MariaDB [(none)]> SELECT * FROM books1;
You will get the following output:
+-----------------+----------------+----------------+
| title | author | published_year |
+-----------------+----------------+----------------+
| Catch-22 | Joseph Heller | 1990 |
| Emma | Jane Austen | 1816 |
| Robinson Crusoe | Daniel Defoe | 1719 |
| Tom Jones | Henry Fielding | 1749 |
| Ulysses | James Joyse | 1923 |
+-----------------+----------------+----------------+
Mariadb Create Table – How to Guide (Step by Step) Conclusion
In the above guide, we explained how to create a table in MariaDB. We also explained how to insert and filter the data by executing queries. I hope you have now enough knowledge to create, delete and manage a table in MariaDB.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.
51vote
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.