Bird
Raised Fist0
DBMS Theoryknowledge~6 mins

DDL (CREATE, ALTER, DROP) in DBMS Theory - Full Explanation

Choose your learning style10 modes available
Introduction
When working with databases, you often need to build or change the structure where data is stored. This involves creating new tables, changing existing ones, or removing them entirely. Managing these changes clearly and safely is essential for keeping data organized and accessible.
Explanation
CREATE Statement
The CREATE command is used to make new database objects like tables, indexes, or views. When you create a table, you define its columns and the type of data each column will hold. This sets up the structure where data will be stored.
CREATE builds new structures in the database to hold data.
ALTER Statement
ALTER lets you change the structure of an existing database object without deleting it. For example, you can add or remove columns from a table, or change a column's data type. This helps adapt the database as needs evolve without losing stored data.
ALTER modifies existing database structures safely.
DROP Statement
DROP removes database objects completely, such as tables or indexes. When you drop a table, all its data and structure are deleted permanently. This is useful when you no longer need certain data or want to clean up the database.
DROP deletes database objects and their data permanently.
Real World Analogy

Imagine managing a library. Creating a new shelf is like CREATE, adding or removing sections on a shelf is like ALTER, and removing a whole shelf is like DROP. Each action changes how books are stored and organized.

CREATE Statement → Building a new bookshelf to hold books
ALTER Statement → Changing the sections on an existing bookshelf to fit different books
DROP Statement → Removing an entire bookshelf and all its books
Diagram
Diagram
┌─────────────┐      ┌─────────────┐      ┌─────────────┐
│   CREATE    │─────▶│   ALTER     │─────▶│    DROP     │
│ (Make new)  │      │ (Change)    │      │ (Remove)    │
└─────────────┘      └─────────────┘      └─────────────┘
This diagram shows the flow of creating, altering, and dropping database objects.
Key Facts
CREATE → Command to make new database objects like tables or indexes.
ALTER → Command to change the structure of existing database objects.
DROP → Command to delete database objects and their data permanently.
Table → A database object that stores data in rows and columns.
Column → A named data field in a table that holds a specific type of data.
Code Example
DBMS Theory
CREATE TABLE Employees (
  ID INT PRIMARY KEY,
  Name VARCHAR(100),
  Age INT
);

ALTER TABLE Employees ADD Email VARCHAR(100);

DROP TABLE Employees;
OutputSuccess
Common Confusions
ALTER deletes data when changing a table.
ALTER deletes data when changing a table. ALTER changes the structure without deleting existing data unless explicitly dropping columns.
DROP can be undone easily.
DROP can be undone easily. DROP permanently removes objects and data; recovery requires backups.
CREATE can add columns to an existing table.
CREATE can add columns to an existing table. CREATE only makes new objects; ALTER is used to add columns to existing tables.
Summary
CREATE builds new database structures to store data.
ALTER changes existing structures without deleting data.
DROP removes database objects and their data permanently.

Practice

(1/5)
1. What does the CREATE statement do in a database?
easy
A. It makes a new table to store data.
B. It deletes an existing table and its data.
C. It changes the data inside a table.
D. It backs up the database.

Solution

  1. Step 1: Understand the purpose of CREATE

    The CREATE statement is used to make new database objects, such as tables, to store data.
  2. Step 2: Compare with other statements

    DROP deletes tables, ALTER changes structure, but CREATE makes new tables.
  3. Final Answer:

    It makes a new table to store data. -> Option A
  4. Quick Check:

    CREATE = Make new table [OK]
Hint: CREATE always makes new tables, not deletes or changes [OK]
Common Mistakes:
  • Confusing CREATE with DROP which deletes tables
  • Thinking CREATE changes existing data
  • Assuming CREATE backs up data
2. Which of the following is the correct syntax to add a new column named age of type INT to an existing table students?
easy
A. ALTER students ADD age INT;
B. ALTER TABLE students ADD COLUMN age INT;
C. DROP TABLE students ADD age INT;
D. CREATE TABLE students ADD age INT;

Solution

  1. Step 1: Identify correct ALTER syntax

    The correct syntax to add a column is: ALTER TABLE table_name ADD COLUMN column_name datatype;
  2. Step 2: Check each option

    ALTER TABLE students ADD COLUMN age INT; matches the correct syntax. Options B and C misuse CREATE and DROP. ALTER students ADD age INT; misses TABLE keyword.
  3. Final Answer:

    ALTER TABLE students ADD COLUMN age INT; -> Option B
  4. Quick Check:

    ALTER TABLE + ADD COLUMN = ALTER TABLE students ADD COLUMN age INT; [OK]
Hint: ALTER TABLE ... ADD COLUMN ... is the correct form [OK]
Common Mistakes:
  • Omitting TABLE keyword after ALTER
  • Using CREATE or DROP instead of ALTER
  • Forgetting the COLUMN keyword (optional but clearer)
3. Consider the following SQL commands executed in order:
CREATE TABLE employees (id INT, name VARCHAR(50));
ALTER TABLE employees ADD COLUMN salary INT;
What will be the structure of the employees table after these commands?
medium
A. Table with columns: id, name, salary
B. Table with columns: id, salary
C. Table with columns: id, name
D. Table with columns: name, salary

Solution

  1. Step 1: Analyze CREATE TABLE command

    The first command creates a table named employees with columns id and name.
  2. Step 2: Analyze ALTER TABLE command

    The second command adds a new column salary to the employees table.
  3. Final Answer:

    Table with columns: id, name, salary -> Option A
  4. Quick Check:

    CREATE + ALTER ADD COLUMN = id, name, salary [OK]
Hint: ALTER ADD COLUMN adds new columns to existing tables [OK]
Common Mistakes:
  • Ignoring the ALTER command effect
  • Assuming DROP was used to remove columns
  • Thinking ALTER replaces columns instead of adding
4. Identify the error in the following SQL statement:
ALTER TABLE employees DROP salary;
medium
A. Table name is incorrect
B. DROP cannot be used with ALTER TABLE
C. Missing COLUMN keyword after DROP
D. salary column does not exist

Solution

  1. Step 1: Understand DROP syntax in ALTER TABLE

    To remove a column, syntax requires: ALTER TABLE table_name DROP COLUMN column_name;
  2. Step 2: Check the given statement

    The statement misses the COLUMN keyword after DROP, causing syntax error.
  3. Final Answer:

    Missing COLUMN keyword after DROP -> Option C
  4. Quick Check:

    ALTER TABLE DROP COLUMN required [OK]
Hint: DROP COLUMN must be used, not just DROP [OK]
Common Mistakes:
  • Omitting COLUMN keyword after DROP
  • Confusing DROP TABLE with DROP COLUMN
  • Assuming DROP alone removes columns
5. You want to permanently delete a table named orders and all its data from the database. Which SQL command should you use?
hard
A. ALTER TABLE orders DELETE;
B. ALTER TABLE orders DROP;
C. CREATE TABLE orders;
D. DROP TABLE orders;

Solution

  1. Step 1: Understand the purpose of DROP TABLE

    DROP TABLE deletes the entire table and its data permanently from the database.
  2. Step 2: Evaluate the options

    DROP TABLE orders; uses correct syntax. ALTER TABLE orders DROP; and D misuse ALTER. CREATE TABLE orders; creates a table instead of deleting.
  3. Final Answer:

    DROP TABLE orders; -> Option D
  4. Quick Check:

    DROP TABLE = delete table permanently [OK]
Hint: Use DROP TABLE to delete tables permanently [OK]
Common Mistakes:
  • Using ALTER instead of DROP to delete tables
  • Confusing CREATE with DROP
  • Omitting TABLE keyword in DROP statement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard