---
description: Core SQL Statements Guide
---

# Basic SQL Statements Guide

This guide provides a quick overview of essential SQL statements in MariaDB, categorized by their function in data definition, data manipulation, and transaction control. Find brief descriptions and links to detailed documentation for each statement, along with a simple illustrative example sequence.

_(If you need a basic tutorial on how to use the MariaDB database server and execute simple commands, see_ [_A MariaDB Primer_](../server-usage/basics/mariadb-usage-guide-1)_. Also see_ [_Essential Queries Guide_](mariadb-advanced-sql-guide) _for examples of commonly-used queries.)_

### Defining How Your Data Is Stored (Data Definition Language - DDL)

* [**CREATE DATABASE**](../reference/sql-statements/data-definition/create/create-database): Used to create a new, empty database.
* [**DROP DATABASE**](../reference/sql-statements/data-definition/drop/drop-database): Used to completely destroy an existing database.
* **USE**: Used to select a default database for subsequent statements.
* [**CREATE TABLE**](../reference/sql-statements/data-definition/create/create-table): Used to create a new table, which is where your data is actually stored.
* [**ALTER TABLE**](../reference/sql-statements/data-definition/alter/alter-table): Used to modify an existing table's definition (e.g., add/remove columns, change types).
* [**DROP TABLE**](../reference/sql-statements/data-definition/drop/drop-table): Used to completely destroy an existing table and all its data.
* [**DESCRIBE**](../reference/sql-statements/administrative-sql-statements/describe) (or `DESC`): Shows the structure of a table (columns, data types, etc.).

### Manipulating Your Data (Data Manipulation Language - DML)

* [**SELECT**](../reference/sql-statements/data-manipulation/selecting-data/select): Used when you want to read (or select) your data from one or more tables.
* [**INSERT**](../reference/sql-statements/data-manipulation/inserting-loading-data/insert): Used when you want to add (or insert) new rows of data into a table.
* [**UPDATE**](../reference/sql-statements/data-manipulation/changing-deleting-data/update): Used when you want to change (or update) existing data in a table.
* [**DELETE**](../reference/sql-statements/data-manipulation/changing-deleting-data/delete): Used when you want to remove (or delete) existing rows of data from a table.
* [**REPLACE**](../reference/sql-statements/data-manipulation/changing-deleting-data/replace): Works like `INSERT`, but if an old row in the table has the same value as a new row for a `PRIMARY KEY` or a `UNIQUE` index, the old row is deleted before the new row is inserted.
* [**TRUNCATE TABLE**](../reference/sql-statements/table-statements/truncate-table): Used to quickly remove all data from a table, resetting any `AUTO_INCREMENT` values. It is faster than `DELETE` without a `WHERE` clause for emptying a table.

### Transactions (Transaction Control Language - TCL)

* [**START TRANSACTION**](../reference/sql-statements/transactions/start-transaction) (or `BEGIN`): Used to begin a new transaction, allowing multiple SQL statements to be treated as a single atomic unit.
* [**COMMIT**](../reference/sql-statements/transactions/commit): Used to save all changes made during the current transaction, making them permanent.
* [**ROLLBACK**](../reference/sql-statements/transactions/rollback): Used to discard all changes made during the current transaction, reverting the database to its state before the transaction began.

### A Simple Example Sequence

This example demonstrates several of the statements in action:

```sql
-- Create a new database
CREATE DATABASE mydb;

-- Select the new database to use
USE mydb;

-- Create a new table
CREATE TABLE mytable (
    id INT PRIMARY KEY,
    name VARCHAR(20)
);

-- Insert some data
INSERT INTO mytable VALUES (1, 'Will');
INSERT INTO mytable VALUES (2, 'Marry');
INSERT INTO mytable VALUES (3, 'Dean');

-- Select specific data
SELECT id, name FROM mytable WHERE id = 1;

-- Update existing data
UPDATE mytable SET name = 'Willy' WHERE id = 1;

-- Select all data to see changes
SELECT id, name FROM mytable;

-- Delete specific data
DELETE FROM mytable WHERE id = 1;

-- Select all data again
SELECT id, name FROM mytable;

-- Drop the database (removes the database and its tables)
DROP DATABASE mydb;
```

Common Query: Counting Rows

To count the number of records in a table:

```sql
SELECT COUNT(*) FROM mytable; -- Or SELECT COUNT(1) FROM mytable;
```

_(Note: This query would typically be run on an existing table, for example, before it or its database is dropped.)_

<sub>_This page is licensed: CC BY-SA / Gnu FDL_</sub>

{% @marketo/form formId="4316" %}
