Skip to main content

MariaDB

Overview

MariaDB is an open-source relational database management system created in 2009 by the original MySQL developers after Oracle acquired Sun Microsystems. It is a community-driven fork of MySQL and fully compatible with it in most use cases, making it a common drop-in replacement.

MariaDB is ACID-compliant and supports multiple storage engines, including InnoDB and Aria. It is widely used for web applications, content management systems such as WordPress and Drupal, and general-purpose OLTP workloads.

CREATE TABLE

Defines a new table with its columns, data types, and constraints.

CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);

ALTER TABLE

Modifies an existing table by adding, removing, or changing columns.

ALTER TABLE users ADD COLUMN age INT;

ALTER TABLE users DROP COLUMN age;

ALTER TABLE users MODIFY COLUMN name VARCHAR(200) NOT NULL;

ALTER TABLE users RENAME COLUMN name TO full_name;

DROP / TRUNCATE

Permanently deletes a table or removes all its rows while keeping the structure.

DROP TABLE users; -- Delete table including all data

TRUNCATE TABLE users; -- Delete all rows, keep structure

INSERT

Adds one or more rows to a table.

INSERT INTO users (name, email)
VALUES ('Alice', 'alice@example.com');

INSERT INTO users (name, email) VALUES
('Bob', 'bob@example.com'),
('Carol', 'carol@example.com');

SELECT

Retrieves rows from one or more tables, with optional filtering, ordering, and pagination.

SELECT id, name, email
FROM users
WHERE created > '2024-01-01'
ORDER BY name ASC
LIMIT 10 OFFSET 20;

Aliases

Assigns temporary names to columns or tables for readability.

SELECT u.name AS user_name, o.total AS order_total
FROM users AS u
JOIN orders AS o ON o.user_id = u.id;

JOINs

Combines rows from multiple tables based on a related column.

-- INNER JOIN — only rows that have a match in both tables
SELECT u.name, o.total
FROM users AS u
INNER JOIN orders AS o ON o.user_id = u.id;

-- LEFT JOIN — all rows from the left table, matched rows from the right
SELECT u.name, o.total
FROM users AS u
LEFT JOIN orders AS o ON o.user_id = u.id;

-- RIGHT JOIN — all rows from the right table, matched rows from the left
SELECT u.name, o.total
FROM users AS u
RIGHT JOIN orders AS o ON o.user_id = u.id;

GROUP BY / HAVING

Groups rows by column value and optionally filters groups after aggregation.

-- GROUP BY aggregates rows by column value
SELECT department_id, COUNT(*) AS headcount
FROM employees
GROUP BY department_id;

-- HAVING filters groups after aggregation (WHERE filters rows before)
SELECT department_id, COUNT(*) AS headcount
FROM employees
GROUP BY department_id
HAVING headcount > 5;

Subqueries

Nests a query inside another query, either as a filter or a derived table.

SELECT name
FROM users
WHERE id IN (SELECT user_id FROM orders WHERE total > 100);

-- Subquery as derived table
SELECT dept, avg_salary
FROM (
SELECT department_id AS dept, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
) AS dept_avg
WHERE avg_salary > 50000;

UPDATE

Modifies existing rows in a table.

UPDATE users
SET name = 'Alice Smith',
email = 'alicesmith@example.com'
WHERE id = 1;

DELETE

Removes rows from a table.

DELETE FROM users WHERE id = 1;

Indexes

Speeds up queries by creating a data structure for faster lookups on one or more columns.

CREATE INDEX idx_users_name ON users (name);

CREATE UNIQUE INDEX idx_users_email ON users (email);

DROP INDEX idx_users_email ON users;

SHOW INDEX FROM users;

Views

Creates a named, reusable query that can be referenced like a table.

CREATE VIEW active_users AS
SELECT id, name, email
FROM users
WHERE active = 1;

DROP VIEW active_users;

Transactions

Groups multiple statements into a single atomic unit that either fully succeeds or fully rolls back.

START TRANSACTION;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;
-- ROLLBACK; -- Undo all changes since START TRANSACTION

Functions

String

Common functions for manipulating text values.

CONCAT(first_name, ' ', last_name) -- Concatenate strings
LOWER(name) -- Lowercase
UPPER(name) -- Uppercase
LENGTH(name) -- Length in bytes
TRIM(name) -- Remove leading/trailing whitespace
SUBSTRING(name, 1, 3) -- Extract substring (1-indexed)
REPLACE(name, 'old', 'new') -- Replace substring

Date / Time

Common functions for working with date and time values.

NOW() -- Current date and time
CURDATE() -- Current date
DATE(created) -- Extract date part from datetime
YEAR(created) -- Extract year
MONTH(created) -- Extract month
DAY(created) -- Extract day
DATE_FORMAT(created, '%d.%m.%Y') -- Format date as string
DATEDIFF(end_date, start_date) -- Difference in days
DATE_ADD(created, INTERVAL 7 DAY) -- Add interval

Aggregate

Functions that compute a single value from a set of rows.

COUNT(*) -- Count all rows
COUNT(email) -- Count non-NULL values
SUM(total) -- Sum
AVG(total) -- Average
MIN(total) -- Minimum
MAX(total) -- Maximum

Conditional

Functions and expressions for returning different values based on conditions.

-- COALESCE — returns the first non-NULL value
SELECT COALESCE(nickname, name) AS display_name FROM users;

-- CASE
SELECT name,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 75 THEN 'B'
ELSE 'C'
END AS grade
FROM results;

-- IF
SELECT IF(active = 1, 'Active', 'Inactive') AS status FROM users;