How to Create a Table in MariaDB on Ubuntu 22.04


In the earlier post, we checked out the procedure for selecting a specific database in MariaDB on Ubuntu 22.04. Now, moving towards the next topic. Today, we are about to discuss the procedure for creating a table and inserting and selecting data from it.

In MariaDB, we create databases that can comprise multiple tables. After that, you can insert data into these tables, and perform other data manipulation operations like updating or deleting records. More specifically, you can also select or view records of single or multiple tables to analyze data.

The contents of this guide are:

  • How to Create a Table in MariaDB on Ubuntu 22.04?
  • How to Insert Values in a Table in MariaDB on Ubuntu 22.04?
  • How to Use Select Command in MariaDB on Ubuntu 22.04?

How to Create a Table in MariaDB on Ubuntu 22.04?

Tables are created to store and organize data in a more structured way. They help to enforce data integrity and define relationships between different entities as well.

In this part of the guide, we will see how to utilize the MariaDB shell for creating a table.

Step 1: Open the terminal

Hit “CTRL+ALT+T” to open the Ubuntu terminal:

Step 2: Access MariaDB Shell

To open the MariaDB shell, type the mentioned command in the terminal:

mysql -u root -p

Step 3: Select a Database

For selecting a specific database, use the command below:

USE linuxgenie;

Step 4: Create a Table

Utilize the mentioned command for creating a table in MariaDB on Ubuntu 22.04:

CREATE TABLE authors (
author_id INT NOT NULL AUTO_INCREMENT,
author_name VARCHAR(30) NOT NULL,
author_email VARCHAR(40) NOT NULL,
author_phone VARCHAR(20),
PRIMARY KEY (author_id)
);

How to Insert Values in a Table in MariaDB on Ubuntu 22.04?

Inserting data into a table in MariaDB is necessary for managing data in a database. It is used for updating existing data, storing new data, automating data entry, and generating reports.

Once you have created a table, type the mentioned command in the MariaDB shell for inserting values:

INSERT INTO authors (author_name, author_email, author_phone) VALUES
('Karim', '[email protected]', '0737-19234'),
('Adnan', '[email protected]', '0737-59678'),
('Sharqa', '[email protected]', '0737-90876');

How to Use Select Command in MariaDB on Ubuntu 22.04?

When you need to retrieve data from a table in a MariaDB database, the SELECT command is the way to go. This command permits you to specify the columns you want to retrieve, as well as any additional conditions or sorting options that are necessary for your specific use case.

In this section, we will specifically use the SELECT command for retrieving data from the table “authors”.

By executing the command below, all data from the table “authors” will get displayed:

SELECT * FROM authors;

That was all about creating a table in MariaDB on Ubuntu 22.04.

Conclusion

Before creating a table in a database in MariaDB, you have to select a specific database first. To do so, open up the MariaDB shell by typing the “mysql -u root -p” command in the terminal. After that, utilize the “USE linuxgenie;” to select a database. If the table is not created then create it first by using the command mentioned in the guide. Now, you can insert and select data from it whenever you want. That’s how you can create a table in MariaDB on Ubuntu 22.04.

Print Friendly, PDF & Email
Categories