DML stands for “Data Manipulation Language.” It is a subset of SQL (Structured Query Language) used to manipulate data stored in a database. DML statements are used to retrieve, insert, update, and delete data in a database. Here are some common DML statements:
- SELECT: Used to retrieve data from one or more tables. You can specify the columns you want to retrieve and use conditions to filter the data:
SELECT first_name, last_name
FROM employees
WHERE department_id = 1;
- INSERT: Used to add new records into a table:
INSERT INTO employees (first_name, last_name, department_id)
VALUES ('John', 'Doe', 2);
- UPDATE: Used to modify existing data in a table based on specified conditions:
UPDATE employees
SET department_id = 3
WHERE last_name = 'Doe';
- DELETE: Used to remove rows from a table based on specified conditions:
DELETE FROM employees
WHERE last_name = 'Doe';
- MERGE (or UPSERT): Combines the INSERT, UPDATE, and DELETE operations into a single statement. It inserts a row if it doesn’t exist or updates it if it does:
MERGE INTO employees AS target
USING (VALUES (1, 'Alice', 'Smith', 4)) AS source (id, first_name, last_name, department_id)
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET first_name = source.first_name, last_name = source.last_name, department_id = source.department_id
WHEN NOT MATCHED THEN
INSERT (id, first_name, last_name, department_id) VALUES (source.id, source.first_name, source.last_name, source.department_id);
DML statements are fundamental for interacting with the data stored in a database. They allow you to perform a wide range of operations, from querying data to modifying its content, which is essential for maintaining accurate and up-to-date information in the database.