Data Manipulation Language (DML) in Oracle SQL
Data Manipulation Language (DML) is a subset of SQL used to retrieve, insert, update, and delete data in a database. Understanding these commands is crucial for managing and manipulating data effectively. The primary DML commands are SELECT
, INSERT
, UPDATE
, and DELETE
.
1. SELECT
The SELECT
statement is used to retrieve data from one or more tables. It allows you to specify which columns to retrieve, filter data using conditions, and sort the results. The basic syntax is:
SELECT column1, column2 FROM table_name WHERE condition;
For example, to retrieve all employees' first and last names from the "Employees" table:
SELECT FirstName, LastName FROM Employees;
2. INSERT
The INSERT
statement is used to add new rows of data into a table. You can insert data into specific columns or all columns. The basic syntax is:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
For example, to insert a new employee into the "Employees" table:
INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
VALUES (101, 'John', 'Doe', TO_DATE('2023-01-15', 'YYYY-MM-DD'));
3. UPDATE
The UPDATE
statement is used to modify existing data in a table. You can update one or more columns based on a condition. The basic syntax is:
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
For example, to update the salary of an employee with ID 101 in the "Employees" table:
UPDATE Employees SET Salary = 50000 WHERE EmployeeID = 101;
4. DELETE
The DELETE
statement is used to remove rows from a table based on a condition. The basic syntax is:
DELETE FROM table_name WHERE condition;
For example, to delete an employee with ID 101 from the "Employees" table:
DELETE FROM Employees WHERE EmployeeID = 101;
Understanding and mastering these DML commands is essential for effectively managing data in an Oracle database. By using SELECT
to retrieve data, INSERT
to add new data, UPDATE
to modify existing data, and DELETE
to remove data, you can perform a wide range of data manipulation tasks.