Skip to main content

Command Palette

Search for a command to run...

Mariadb tutorial

Updated
•2 min read•View as Markdown

MariaDB Basic Commands

1. Log in to MariaDB

sudo mariadb

Database Operations

2. Create a Database

CREATE DATABASE db1;

3. View All Databases

SHOW DATABASES;

4. Select the Database

USE db1;

5. Confirm the Selected Database

SELECT DATABASE();

Table Operations

6. Create a Table

CREATE TABLE student (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    city VARCHAR(50)
);

7. Insert Records

insert into student (name, age, city) values ('nagul', 32, 'karnataka'),('kanal', 23, 'odissa');

INSERT INTO student (name, age, city)
VALUES
('Nagul', 32, 'Karnataka'),
('Kanal', 23, 'Odisha');

8. View the Data

select * from student;

SELECT * FROM student;

9. Check the Table Structure

describe student;

DESCRIBE student;

10. List All Tables

show tables;

SHOW TABLES;

Backup and Restore

11. Exit MariaDB

EXIT;

12. Back Up the db1 Database

From the Linux terminal:

sudo mysqldump db1 > db1_backup.sql

sudo mysqldump db1 > db1_backup.sql

Verify the backup file:

ls -lh db1_backup.sql

ls -lh db1_backup.sql

13. Create a New Database

Log in to MariaDB:

sudo mariadb

sudo mariadb

Create the database:

create database db2;

CREATE DATABASE db2;

Exit:

exit;

EXIT;

14. Restore the Backup into db2

From the Linux terminal:

sudo mariadb db2 < db1_backup.sql

sudo mariadb db2 < db1_backup.sql

15. Verify the Restore

Log in to MariaDB:

sudo mariadb

sudo mariadb

Select the restored database:

use db2;

USE db2;

List the tables:

Show tables;

SHOW TABLES;

View the restored data:

select * from student;

SELECT * FROM student;