Data Manipulation Language (DML) is a subset of SQL (Structured Query Language) used to interact with and manipulate data stored within a database. DML commands are primarily focused on retrieving, inserting, updating, and deleting data records within database tables. Here are the main types and examples of DML commands:
1. SELECT:
The SELECT statement retrieves data from one or more tables in a database.
Basic SELECT:
This command retrieves all records from a table.
SELECT * FROM employees;
SELECT with Conditions:
You can use conditions to filter the results
SELECT first_name, last_name
FROM employees
WHERE department = 'HR';
2. INSERT:
The INSERT
statement is used to add new records to a table.
INSERT INTO:
This command adds a new record to the
employees
table.- INSERT INTO employees (employee_id, first_name, last_name, department)VALUES (1, 'John', 'Doe', 'Sales');
. UPDATE:
The UPDATE statement is used to modify existing records in a table.
UPDATE:
This command updates the salary for employees in the Sales department.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
DELETE:
The DELETE statement is used to remove records from a table.
DELETE FROM:
This command deletes all records for employees with the last name 'Smith.'
DELETE FROM employees
WHERE last_name = 'Smith';
MERGE:
The MERGE statement is used to perform conditional insert, update, or delete operations in a single statement. It's particularly useful for handling data synchronization.
MERGE INTO:
This example demonstrates merging data from a source_table into a target_table:------
MERGE INTO target_table AS T
USING source_table AS S
ON T.id = S.id
WHEN MATCHED THEN
UPDATE SET T.value = S.value
WHEN NOT MATCHED THEN
INSERT (id, value) VALUES (S.id, S.value);
0 Comments