Oracle

Chapter 5 - DDL (Data Definition Language)

DROP TABLE

In Oracle, the DROP TABLE command is used to remove a table and all of its data from the database. Once a table is dropped, it cannot be recovered unless a backup exists. It is important to ensure that the table is no longer required before issuing this command. In cases where you only want to delete data without losing the table structure, consider using TRUNCATE instead. Although Oracle does not support IF EXISTS natively for DROP TABLE, you can work around this by using PL/SQL.

Here’s a step-by-step guide for dropping tables related to an author, books, library, membership, and rentals system:

  1. Basic Syntax for DROP TABLE
    The basic syntax for the DROP TABLE command in Oracle is:

    DROP TABLE table_name;
    
  2. Steps to Drop a Table

    • Make sure the table you want to drop is no longer required.
    • Check if the table is referenced by other objects, such as views, triggers, or foreign keys.
    • Use the DROP TABLE command to remove the table from the database.
  3. Impact of Dropping a Table with Foreign Keys

    • Parent Table with Foreign Keys:
      If a table being dropped is referenced by other tables (i.e., it is a parent table in a foreign key relationship), Oracle will not allow the table to be dropped unless the referencing foreign keys are dropped or disabled first.

      • If the table has foreign key constraints from other tables, you must either drop the child tables first or remove the foreign key constraints before dropping the parent table.
    • Child Table with Foreign Keys:
      If you drop a child table that references a parent table, the parent table will not be affected. However, the foreign key relationships will no longer exist.

    • CASCADE CONSTRAINTS Option:
      Oracle provides the CASCADE CONSTRAINTS option to automatically drop all foreign keys that refer to the table being dropped.

      DROP TABLE table_name CASCADE CONSTRAINTS;
      
  4. Dropping a Table with IF EXISTS Clause (Simulation)
    Oracle does not support IF EXISTS natively in the DROP TABLE command. You can simulate this behavior by checking the existence of the table first using a PL/SQL block:

    BEGIN
      IF EXISTS (SELECT 1 FROM all_tables WHERE table_name = 'AUTHORS') THEN
        EXECUTE IMMEDIATE 'DROP TABLE authors';
      END IF;
    END;
    
  5. Example Table Definitions and DROP TABLE Command
    Let’s define a few tables related to author, books, library, membership, and rentals, and then show how to drop them.

    • Table Definition: Authors

      CREATE TABLE authors (
        author_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
        name VARCHAR2(100) NOT NULL,
        birthdate DATE,
        CONSTRAINT pk_authors PRIMARY KEY (author_id)
      );
      
    • Table Definition: Books

      CREATE TABLE books (
        book_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
        title VARCHAR2(200) NOT NULL,
        author_id NUMBER,
        publication_date DATE,
        CONSTRAINT pk_books PRIMARY KEY (book_id),
        CONSTRAINT fk_books_authors FOREIGN KEY (author_id) REFERENCES authors (author_id)
      );
      
    • Dropping the authors Table
      If authors is a parent table and referenced by books, you cannot drop it unless the foreign key constraint is removed. To drop it with foreign keys, you would use:

      DROP TABLE authors CASCADE CONSTRAINTS;
      
    • Dropping the books Table

      BEGIN
        IF EXISTS (SELECT 1 FROM all_tables WHERE table_name = 'BOOKS') THEN
          EXECUTE IMMEDIATE 'DROP TABLE books';
        END IF;
      END;
      
  6. Dropping Multiple Tables in Correct Order
    If your system includes foreign key relationships (like between books and authors), you should drop the child tables first before the parent tables to avoid foreign key issues.

    • Steps:
      1. Drop the rentals table (if it references books).
      2. Drop the books table.
      3. Drop the authors table.

By following these steps and using the CASCADE CONSTRAINTS option, you can effectively manage the removal of tables in Oracle, especially when foreign keys are involved.

Tansy SQL Course | DROP TABLE | Chapter 5 | Lesson 3 - Video Thumbnail
Comments(0 comments)

Comments Not Found