DSPython Logo DSPython

SQL DDL (Data Definition Language)

Master the commands used to define, modify, and delete the structure of your database, including tables, indexes, and constraints.

SQL Intermediate 60 min

🏗️ Topic 1: What is Data Definition Language (DDL)?

**Data Definition Language (DDL)** is the language used to define and manage the **structure** (or schema) of database objects like tables, indexes, and databases.

DDL commands are permanent and **auto-commit** changes—meaning they cannot typically be undone (rolled back) once executed. They modify the *blueprints* of the database.

⚙️ Topic 2: CREATE TABLE & Constraints

The **CREATE TABLE** command is the foundational DDL operation, where you define the columns, their data types, and any associated rules (**Constraints**).

📝 Key Constraints:

  • PRIMARY KEY: Uniquely identifies every row. Cannot be NULL.
  • FOREIGN KEY: Links to the PRIMARY KEY of another table, ensuring **referential integrity**.
  • NOT NULL: Ensures that a column must always contain a value.
  • UNIQUE: Ensures all values in a column are different.
  • DEFAULT: Provides a default value if none is specified.

💻 Example: Creating a Table with Constraints

CREATE TABLE Projects (
    ProjectID INT PRIMARY KEY,
    Title VARCHAR(100) NOT NULL,
    Budget DECIMAL(10, 2),
    StartDate DATE DEFAULT CURRENT_DATE
);

🛠️ Topic 3: Modifying Structure with ALTER TABLE

The **ALTER TABLE** command changes the schema of an existing table without deleting the data already inside it.

Common Operations:

  • Adding Columns: ALTER TABLE TableName ADD NewCol Type;
  • Dropping Columns: ALTER TABLE TableName DROP COLUMN OldCol;
  • Modifying Columns: Changes the data type or constraints of a column.

💻 Example: Schema Modification

-- 1. Add a new column to track Status
ALTER TABLE Projects ADD Status VARCHAR(50) DEFAULT 'Active';

-- 2. Increase the size of the Title column
ALTER TABLE Projects ALTER COLUMN Title VARCHAR(255);

💣 Topic 4: Deleting Objects: DROP vs TRUNCATE

These commands are used for permanent deletion and should be handled with extreme care.

🗑️ Comparison (DDL Hierarchy):

  • DROP TABLE: Deletes the **entire table structure** and all data. The table ceases to exist.
  • TRUNCATE TABLE: Deletes **all data rows** in the table, but keeps the empty structure intact. It's often faster than DELETE.

💻 Example: Deletion Commands

-- Deletes the table permanently
DROP TABLE Projects;

-- Empties the table, keeping the column headers
TRUNCATE TABLE AuditLogs;

📚 Module Summary

  • CREATE: Builds new objects (Tables, Indexes).
  • ALTER: Modifies schema (Add/Drop/Change Columns).
  • DROP: Deletes the object (Table structure + data).
  • TRUNCATE: Deletes only the data rows (Keeps structure).
  • Constraints: Rules like PRIMARY KEY and NOT NULL are defined during CREATE.

🤔 Interview Q&A

Tap on the questions below to reveal the answers.

DROP TABLE deletes the entire table structure (schema) and all data permanently. TRUNCATE TABLE deletes all data rows very quickly but leaves the empty table structure intact for future use.

Yes. DDL operations are generally auto-committed and cannot be rolled back. Once executed, the changes to the database structure are permanent.

A FOREIGN KEY enforces referential integrity. It links a column in one table to the PRIMARY KEY of another table, preventing actions that would destroy the links between the tables (e.g., preventing deletion of a customer who has orders).

The **existing data remains intact**. The new column is simply added to the end of the table, and all existing rows will have a NULL value in that new column unless a DEFAULT value was specified.

🤖
DSPython AI Assistant
👋 Hi! I’m your AI assistant. Paste your code here, I will find bugs for you.