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.

  1. Basic Syntax

    • The basic syntax for the DELETE statement is:
      DELETE FROM table_name
      WHERE condition;
      
    • table_name refers to the name of the table from which you want to delete data.
    • condition specifies which rows should be deleted. If no condition is provided, all rows in the table will be deleted.
  2. 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 books table where the author_id is 5.
    • Deleting All Rows

      • To delete all rows from a table:
        DELETE FROM library;
        
      • This command removes all rows from the library table. Be cautious with this operation, as it clears the entire 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 rentals table where the book_id matches those of books published before the year 2000.
  3. Important Considerations

    • Always use the WHERE clause to specify which rows to delete. Omitting the WHERE clause will result in deleting all rows from the table.
    • It's good practice to run a SELECT query with the same WHERE clause before executing a DELETE statement to confirm which rows will be affected.

Using these commands carefully will help you manage your data effectively while minimizing the risk of accidental data loss.

Tansy SQL Course | DELETE | Chapter 6 | Lesson 5 - Video Thumbnail
Comments(0 comments)

Comments Not Found