Skip to content

Data Definition Language (DDL) πŸš€ΒΆ

Prerequisites: Database Basics (Logic Snippet)

Mentor's Note: DDL is about the Structure, not the data. Think of it as building the house itself before you move the furniture in! πŸ’‘


🌟 The Scenario: The Architect's Blueprint πŸ›οΈΒΆ

Imagine you are building a new luxury apartment complex.

  • CREATE: You design the Blueprint (Number of rooms, floor plan). You are building the structure. πŸ—οΈ
  • ALTER: You decide to add a Balcony to every room later. You are changing the existing structure. πŸ› οΈ
  • DROP: The building is old and dangerous. you Demolish it entirely. πŸ’₯
  • The Result: You are managing the "Container," not the people living inside. βœ…

πŸ“– Key DDL CommandsΒΆ

1. CREATEΒΆ

Used to create a new table or database object.

CREATE TABLE students (
    id NUMBER PRIMARY KEY,
    name VARCHAR2(100),
    age NUMBER
);

2. ALTERΒΆ

Used to modify an existing object (add/remove columns).

ALTER TABLE students ADD (email VARCHAR2(50));

3. DROPΒΆ

Deletes the table and ALL its data permanently.

DROP TABLE old_records;


🎨 Visual Logic: The Schema Lifecycle¢

graph LR
    A[Design πŸ“] -- CREATE --> B[New Table πŸ“‹]
    B -- ALTER --> C[Updated Structure πŸ› οΈ]
    C -- DROP --> D[Deleted / Empty Space πŸ’¨]

πŸ’» Implementation: The Schema LabΒΆ

-- πŸ›’ Scenario: Setting up a library system
-- πŸš€ Action: Defining the book structure

CREATE TABLE books (
    book_id NUMBER(5) PRIMARY KEY,
    title VARCHAR2(200) NOT NULL,
    author VARCHAR2(100),
    price NUMBER(8,2)
);

-- πŸ›οΈ Outcome: A new empty "container" for books.
-- πŸš€ Action: Add a column for Published Year

ALTER TABLE books 
ADD (published_year NUMBER(4));

πŸ“Š Sample Dry RunΒΆ

Command Action Data Inside? Structure Changes?
CREATE Build box πŸ“¦ No New box added.
ALTER Add divider πŸ“ Unaffected Box modified.
DROP Burn box πŸ”₯ DESTROYED Box removed.

πŸ“‰ Complexity AnalysisΒΆ

  • Execution: DDL commands are Auto-Committed in Oracle. This means once you run them, you cannot "Undo" (Rollback) the change! ⚠️ Be very careful.

🎯 Practice Lab πŸ§ͺΒΆ

Task: The Employee Setup

Task: Create a table EMPLOYEES with emp_id, emp_name, and join_date. Then, add a salary column using ALTER. Hint: Use VARCHAR2 for names and DATE for dates. πŸ’‘


πŸ’‘ Interview Tip πŸ‘”ΒΆ

"Interviewers often ask: 'What is the difference between DROP and TRUNCATE?' Answer: DROP deletes the structure + data. TRUNCATE deletes ONLY the data but keeps the empty structure!"


πŸ’‘ Pro Tip: "Good design is a lot like clear thinking made visual." - Edward Tufte


← Back: Subqueries | Next: Data Manipulation (DML) β†’