Oracle
Chapter 6 - DML (Data Manipulation Language)
DELETE
The DELETE statement in Oracle SQL is used to remove rows from a table. This operation is permanent and cannot be undone once executed. It's important to use this command carefully to avoid unintentional data loss. Below, you'll find an overview of how to use the DELETE statement, followed by a code example.
Basic Syntax
- The basic syntax for the
DELETEstatement is:DELETE FROM table_name WHERE condition; table_namerefers to the name of the table from which you want to delete data.conditionspecifies which rows should be deleted. If no condition is provided, all rows in the table will be deleted.
- The basic syntax for the
Examples
Deleting Specific Rows
- To delete specific rows based on a condition:
DELETE FROM books WHERE author_id = 5; - This command deletes all rows from the
bookstable where theauthor_idis 5.
- To delete specific rows based on a condition:
Deleting All Rows
- To delete all rows from a table:
DELETE FROM library; - This command removes all rows from the
librarytable. Be cautious with this operation, as it clears the entire table.
- To delete all rows from a table:
Using Subqueries
- You can also use subqueries to delete rows based on criteria from another table:
DELETE FROM rentals WHERE book_id IN ( SELECT book_id FROM books WHERE publication_year < 2000 ); - This command deletes rows from the
rentalstable where thebook_idmatches those of books published before the year 2000.
- You can also use subqueries to delete rows based on criteria from another table:
Important Considerations
- Always use the
WHEREclause to specify which rows to delete. Omitting theWHEREclause will result in deleting all rows from the table. - It's good practice to run a
SELECTquery with the sameWHEREclause before executing aDELETEstatement to confirm which rows will be affected.
- Always use the
Using these commands carefully will help you manage your data effectively while minimizing the risk of accidental data loss.
To gain complete access, login with gmail or outlook, no need of signup. click here


Comments Not Found