SQL DML (Data Manipulation Language) statements are used to interact with and manipulate data stored in a database.
DML statements are responsible for querying, inserting, updating, and deleting data within database tables.
SQL DML statements:
SELECT: The SELECT statement is used to retrieve data from one or more tables. You can specify the columns you want to retrieve and use various clauses to filter and sort the data.
SELECT first_name, last_name FROM employees WHERE department_id = 2 ORDER BY last_name;
INSERT INTO: The INSERT INTO statement is used to add new records (rows) to a table.
INSERT INTO employees (first_name, last_name, department_id) VALUES ('John', 'Doe', 3);
UPDATE: The UPDATE statement is used to modify existing records in a table based on a specified condition.
UPDATE employees SET department_id = 4 WHERE employee_id = 101;
DELETE: The DELETE statement is used to remove records from a table based on a specified condition.
DELETE FROM employees WHERE department_id = 5;
MERGE: Some database systems, such as Oracle, support the MERGE statement, which allows you to perform insert, update, or delete operations on a target table based on the results of a source query.
MERGE INTO target_table AS T USING source_table AS S ON T.id = S.id WHEN MATCHED THEN UPDATE SET T.column1 = S.column1 WHEN NOT MATCHED THEN INSERT (column1, column2) VALUES (S.column1, S.column2);
INSERT INTO SELECT: This statement allows you to insert data into a table by selecting it from another table or using a query.
INSERT INTO new_employees (first_name, last_name) SELECT first_name, last_name FROM temp_employees WHERE department_id = 1;
TRUNCATE TABLE: The TRUNCATE TABLE statement is used to remove all rows from a table, but it retains the table structure. It is more efficient than the DELETE statement when you want to remove all data from a table.
TRUNCATE TABLE employees;
These are some of the essential SQL DML statements for manipulating data in a database.
The specific syntax and capabilities of DML statements may vary slightly depending on the database management system (DBMS) you are using.
Always consult the documentation of your DBMS for precise details and options.