Chapter 4: Creating and Managing Tables

Up until now, we’ve created databases (like empty filing cabinets). Now we’re going to fill them with actual tables — the spreadsheets inside the cabinet where all your data lives.

Today we’re going to learn four very important commands:

  • CREATE TABLE → Build a new table (define columns, data types, rules)
  • DROP TABLE → Delete a table completely
  • ALTER TABLE → Change an existing table (add, modify, or drop columns)
  • TRUNCATE TABLE → Empty a table (delete all rows but keep the structure)

I’ll explain each one like we’re sitting together, with lots of real-life examples, warnings, and best practices. Let’s pretend we’re building an online bookstore database called bookshop. Ready? Let’s go!

1. CREATE TABLE – Building Your First Table

This is the command you use to create a new table and define its structure (columns, data types, constraints).

Basic Syntax:

SQL

Real-life example – A simple books table:

SQL

Explanation of each part:

  • book_id INT AUTO_INCREMENT PRIMARY KEY → This is the primary key (unique identifier for each row). AUTO_INCREMENT means it automatically gives 1, 2, 3… every time you add a book.
  • VARCHAR(200) → Variable-length string, max 200 characters
  • NOT NULL → This column cannot be left empty
  • DECIMAL(8,2) → Up to 8 digits total, 2 after decimal (perfect for money: ₹12,345.67)
  • UNIQUE → No two books can have the same ISBN
  • BOOLEAN DEFAULT TRUE → Yes/No field, defaults to true (available)

Even better version (with modern best practices):

SQL

Why these extras?

  • IF NOT EXISTS → Safe — won’t error if table already exists
  • UNSIGNED → Only positive numbers (good for IDs, years)
  • CHAR(13) → Fixed length for ISBN (always 13 characters)
  • TIMESTAMP … ON UPDATE → Automatically records when row was created/updated
  • ENGINE=InnoDB → Best storage engine for transactions (MySQL/PostgreSQL default)
  • utf8mb4 → Supports emojis, Hindi, Marathi, etc.

2. DROP TABLE – Deleting a Table (Be Very Careful!)

This command completely deletes the table and all its data — no recycle bin!

Syntax:

SQL

Safe version:

SQL

Example:

SQL

Warning: Just like DROP DATABASE, there is no UNDO. In production, many teams remove DROP TABLE permission from regular users — only DBAs can do it. Always think twice: “Do I really want to lose this data forever?”

3. ALTER TABLE – Modifying an Existing Table

Life changes — sometimes you need to add a new column, change a data type, rename something, or drop a column. ALTER TABLE does all that.

Common uses:

A. Add a new column

SQL

B. Modify (change) a column

SQL

C. Rename a column

SQL

D. Drop (delete) a column

SQL

E. Add a foreign key (linking tables) – we’ll cover this more in constraints later

SQL

F. Change table name

SQL

Full example – Let’s improve our books table step by step:

SQL

4. TRUNCATE TABLE – Emptying a Table (Fast Delete All Rows)

TRUNCATE deletes all rows from a table but keeps the table structure (columns, indexes, constraints).

Syntax:

SQL

Example:

SQL

TRUNCATE vs DELETE – Important Differences

Feature TRUNCATE TABLE DELETE FROM table_name;
Deletes all rows? Yes Yes
Keeps table structure? Yes Yes
Resets AUTO_INCREMENT? Yes (back to 1) No (keeps current value)
Can use WHERE clause? No Yes
Triggers fire? Usually no Yes
Speed Much faster (drops & recreates table internally) Slower (logs every row)
Can rollback? No (in most cases) Yes (inside transaction)
Use when… You want to completely reset data You want to delete selectively

Best Practice: Use TRUNCATE only when you’re sure you want to wipe everything and start fresh (e.g., resetting test data).

Full Workflow Example (Copy-Paste This!)

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *