MariaDB Database Cheatsheet

MariaDB is a popular open-source relational database management system (RDBMS) that is widely used for building robust and scalable web applications. Whether you are a beginner or an experienced developer, having a cheatsheet at your fingertips can be incredibly helpful for quickly referencing essential commands and syntax. In this blog, we will provide you with a comprehensive MariaDB cheatsheet organized with helpful code snippets.

1. Connecting to MariaDB

Connect to MariaDB Server

mysql -u username -p

Connect to a Specific Database

USE database_name;

2. Working with Databases

Create a Database

CREATE DATABASE database_name;

Show all Databases

SHOW DATABASES;

Delete a Database

DROP DATABASE database_name;

3. Managing Tables

Create a Table

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  ...
);

Show Table Structure

DESCRIBE table_name;

Delete a Table

DROP TABLE table_name;

4. Inserting Data

Insert Single Record

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Insert Multiple Records

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...),
       (value1, value2, ...),
       ...;

5. Querying Data

Select All Records

SELECT * FROM table_name;

Select Specific Columns

SELECT column1, column2 FROM table_name;

Filter Data with WHERE

SELECT * FROM table_name WHERE condition;

6. Updating and Deleting Data

Update Records

UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

Delete Records

DELETE FROM table_name WHERE condition;

7. Indexing

Create an Index

CREATE INDEX index_name ON table_name (column1, column2, ...);

Show Indexes

SHOW INDEXES FROM table_name;

8. User Management

Create a User

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Grant Privileges

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

Revoke Privileges

REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'localhost';

This MariaDB cheatsheet covers essential commands for connecting to the database, managing databases and tables, inserting and querying data, updating and deleting records, working with indexes, and user management. Bookmark this cheatsheet for quick reference, and boost your productivity when working with MariaDB!

FAQ

1. What is MariaDB, and how does it differ from MySQL?

MariaDB is an open-source relational database management system (RDBMS) and a fork of MySQL. It was created by the original developers of MySQL after concerns arose over Oracle’s acquisition of MySQL. MariaDB aims to maintain open-source freedom and includes additional features not found in MySQL.

2. How can I connect to a MariaDB server using the command line?

To connect to a MariaDB server via the command line, use the following syntax:
mysql -u username -p
Replace “username” with your actual username. You will be prompted to enter your password.

3. What is the purpose of an index in MariaDB?

An index in MariaDB is a data structure that improves the speed of data retrieval operations on a database table. It enhances query performance by providing a quick lookup mechanism, allowing the database engine to find and retrieve rows more efficiently.

4. How can I delete a database in MariaDB?

To delete a database in MariaDB, you can use the following SQL command:
DROP DATABASE database_name;
Replace “database_name” with the name of the database you want to delete. Be cautious, as this action is irreversible and will delete all data in the specified database.

5. What is the difference between “SHOW DATABASES” and “USE database_name” in MariaDB?

“SHOW DATABASES” is a command used to display a list of all available databases on the MariaDB server. On the other hand, “USE database_name” is used to switch to a specific database, making it the active database for subsequent queries and operations.