SQL Data Manipulation Language (DML)

Data Manipulation Language (DML) is a subset of SQL used to retrieve, insert, update, and delete data in a database. These commands are fundamental to interacting with and managing the data stored within your relational database systems.

Core DML Statements

SELECT: Retrieving Data

The SELECT statement is used to query the database and retrieve one or more records. It's the most frequently used DML statement.

Example: Retrieving all columns and rows from a 'Customers' table

SELECT * FROM Customers;

Example: Retrieving specific columns and filtering rows

SELECT customer_name, email
FROM Customers
WHERE country = 'USA';

INSERT: Adding New Data

The INSERT statement is used to add new records (rows) into a table.

Example: Inserting a single row with specified values

INSERT INTO Customers (customer_name, email, country)
VALUES ('John Doe', 'john.doe@example.com', 'Canada');

Example: Inserting a single row without specifying column names (values must match table order)

INSERT INTO Customers
VALUES ('Jane Smith', 'jane.smith@example.com', 'UK', '2023-01-15'); -- Assumes these are the columns and their order

UPDATE: Modifying Existing Data

The UPDATE statement is used to modify existing records in a table. You must use a WHERE clause to specify which rows to update; otherwise, all rows will be updated.

Example: Updating the email for a specific customer

UPDATE Customers
SET email = 'new.john.doe@example.com'
WHERE customer_name = 'John Doe';

Example: Updating multiple fields for customers in a specific country

UPDATE Customers
SET country = 'USA', last_contact_date = '2024-03-10'
WHERE customer_id = 101;

DELETE: Removing Data

The DELETE statement is used to remove records from a table. Like UPDATE, a WHERE clause is crucial to avoid deleting all records.

Example: Deleting a specific customer

DELETE FROM Customers
WHERE customer_name = 'Jane Smith';

Example: Deleting all records from a table (use with extreme caution!)

DELETE FROM Customers; -- Empties the table

Important Considerations

Common Clauses and Modifiers

DML statements often include clauses that refine the operation:

Further Reading

Understanding and effectively using SQL DML statements is crucial for any developer or database administrator working with relational databases.