You are on page 1of 16

MariaDB SQL QUICK REFERENCE

GUIDE

DDL - Data Definition Language


KEYWORDS/CO DESCRIPTION SAMPLE USAGE
MMANDS

CREATE TABLE Create new table in the CREATE TABLE [IF NOT EXISTS]
database table_name(
id INT NOT NULL AUTO_INCREMENT,
column1 VARCHAR(255),
PRIMARY KEY (id)
)

ALTER TABLE… Adds a new ALTER TABLE my_table


ADD column to an ADD new_column INT;
existing table.

ALTER TABLE… Modifies the data ALTER TABLE my_table


MODIFY type or MODIFY column1 VARCHAR(100);
constraints of an
existing column in a
table.

ALTER TABLE… Changes the ALTER TABLE my_table


CHANGE name or data type of CHANGE COLUMN old_name new_name
COLUMN an existing column VARCHAR(100);
in a table.

ALTER TABLE… Removes a ALTER TABLE my_table


DROP COLUMN column from an DROP COLUMN column_to_remove;
existing table.

ALTER TABLE… Renames an existing ALTER TABLE my_table RENAME


RENAME TO table. TO new_table_name;

ALTER TABLE… Adds a constraint to ALTER TABLE my_table


ADD an existing table. ADD CONSTRAINT fk_constraint FOREIGN KEY
CONSTRAINT (column_name) REFERENCES other_table(id);
ALTER TABLE… Drops a foreign ALTER TABLE my_table
DROP FOREIGN key constraint DROP FOREIGN KEY fk_constraint;
KEY from an existing
table.

DROP TABLE Deletes an DROP TABLE my_table;


existing table and its
data.

TRUNCATE Removes all rows TRUNCATE TABLE my_table;


TABLE from an existing
table but retains the
table
structure.

DESCRIBE Provides information DESC my_table;


TABLE about the columns in
a table.

CREATE Creates a new CREATE DATABASE my_database;


DATABASE database.

DROP Deletes an DROP DATABASE my_database;


DATABASE existing database and
all its tables.

USE Specifies which USE my_database;


database to use
within the current
session.
DQL - Data Query Language
KEYWORDS/ DESCRIPTION SAMPLE USAGE
COMMANDS

SELECT Retrieve data from the table SELECT column1, column2 FROM
table_name

WHERE Filters rows based on a WHERE column1 = 'value' AND column2 > 10;
specified condition in
the SELECT statement.

AND Combines multiple WHERE condition1 AND condition2;


conditions in the
WHERE clause, all of
which must be true.

OR Combines multiple WHERE condition1 OR condition2;


conditions in the
WHERE clause, at
least one of which must
be true.

LIKE Performs a pattern WHERE column1 LIKE 'pattern%';


match search within a
column's data.

BETWEEN Specifies a range of WHERE column1 BETWEEN 10 AND 20;


values for a column in a
condition.

<, <=, >, >=, =, Used for comparing WHERE column1 > 5;
!=/<> values in a condition.

ORDER BY Sorts the result set based ORDER BY column1 ASC, column2 DESC;
on one or more columns.
NOT Negates a condition in WHERE NOT condition;
the WHERE clause.

IS NULL Checks if a column has WHERE column1 IS NULL;


a NULL value.

IS NOT NULL Checks if a column does WHERE column1 IS NOT NULL;


not have a NULL value.

DISTINCT Removes duplicate rows SELECT DISTINCT column1 FROM my_table;


from the result set.

You might also like