Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views19 pages

DDL Data Definition Language

Uploaded by

amansiddiqui8439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

DDL Data Definition Language

Uploaded by

amansiddiqui8439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

DDL - Data Definition Language

The Complete Journey with One Table: "Students"

🎯 What is DDL? (Very Simple Explanation)


DDL = Data Definition Language

Think Like This:

 You want to build a house (table)


 DDL commands are like construction tools
 CREATE = Build the house
 ALTER = Renovate the house (add rooms, change room size)
 TRUNCATE = Remove all furniture but keep the house
 DROP = Demolish the entire house

Key Point: DDL works with STRUCTURE, not with CONTENT

📋 DDL Commands Summary

Command What It Does Real Life Example

CREATE Build new table Building a new house

ALTER Change table structure Adding/removing rooms in house

TRUNCATE Remove all data, keep structure Emptying house but keeping it

DROP Delete entire table Demolishing the house completely


🏗️Journey Starts Here: CREATE Command
What is CREATE?
CREATE command builds a brand new table with specific columns and data types. Like
building a house with specific rooms.

Why Use CREATE?


When you need a new table to store information that doesn't exist yet.
Real Example: College Management System
Scenario: You work at Delhi University and need to create a student database.

Step 1: CREATE Table


Our Complete Students Table (20 Records):

student_id first_name last_name age course city phone


1 Rahul Sharma 20 Computer Science Delhi 9876543210
2 Priya Singh 19 Mathematics Mumbai 9876543211
3 Amit Kumar 21 Physics Bangalore 9876543212
4 Neha Gupta 20 Chemistry Chennai 9876543213
5 Rohit Jain 22 Computer Science Pune 9876543214
6 Sneha Patel 19 Biology Ahmedabad 9876543215
7 Vikas Yadav 21 Mathematics Hyderabad 9876543216
8 Anjali Mishra 20 Physics Kolkata 9876543217
9 Karan Agarwal 22 Chemistry Jaipur 9876543218
10 Pooja Sharma 19 Computer Science Lucknow 9876543219
student_id first_name last_name age course city phone
11 Arjun Singh 21 Biology Bhopal 9876543220
12 Riya Kumar 20 Mathematics Indore 9876543221
13 Suresh Gupta 22 Physics Nagpur 9876543222
14 Kavya Jain 19 Chemistry Surat 9876543223
15 Manish Patel 21 Computer Science Vadodara 9876543224
16 Divya Yadav 20 Biology Agra 9876543225
17 Harsh Mishra 22 Mathematics Kanpur 9876543226
18 Shruti Agarwal 19 Physics Meerut 9876543227
19 Nikhil Sharma 21 Chemistry Faridabad 9876543228
20 Sakshi Singh 20 Computer Science Ghaziabad 9876543229

Current Status:

 20 students enrolled
 7 columns with complete data
 All student IDs automatically assigned (1 to 20)
🔧 ALTER Command - Modifying Our Table
What is ALTER?
ALTER command changes the structure of existing tables. Like renovating your house - adding
rooms, changing room sizes, removing walls.

Why Use ALTER?


When your requirements change and you need to modify the table structure without losing
existing data.

ALTER Types:
1. ADD - Add new columns
2. MODIFY - Change column properties
3. DROP - Remove columns

ALTER Example 1: Adding New Column


Business Need:

Admin says: "We need to store student email addresses and grades too!"
AFTER ALTER:

student_id first_name last_name age course city phone email


Computer
1 Rahul Sharma 20 Delhi 9876543210 NULL
Science
2 Priya Singh 19 Mathematics Mumbai 9876543211 NULL
3 Amit Kumar 21 Physics Bangalore 9876543212 NULL
... ... ... ... ... ... ... NULL
(All 20 rows now have
NULL in email column)
ALTER Example 2: Adding Grade Column
Effect After Query:

student_id first_name last_name age course city phone email grade


Computer
1 Rahul Sharma 20 Delhi 9876543210 NULL A
Science
2 Priya Singh 19 Mathematics Mumbai 9876543211 NULL A
3 Amit Kumar 21 Physics Bangalore 9876543212 NULL A
... ... ... ... ... ... ... NULL A
(All 20 rows
now have 'A'
grade by
default)

What Happened:

 ✅ Grade column added with default value 'A'


 ✅ All existing students automatically got 'A' grade
 ✅ Table now has 9 columns
 ✅ All 20 students' data still intact

ALTER Example 3: Modifying Column Size


Business Need:

"Course names are too long! 'Computer Science Engineering' doesn't fit in VARCHAR(100)"

Query:
ALTER TABLE Students MODIFY COLUMN course VARCHAR(150);
Effect:

 Course column size changed from VARCHAR (100) to VARCHAR (150)


 All data remains exactly the same
 Now longer course names can be stored
 No visible change in data, only in structure capacity

Table Data: Exactly same as before, but course column can now handle longer text.

ALTER Example 4: Adding Multiple Columns at Once


Business Need:

"We need admission date and fees information"

Effect After Query:

student_i first_nam last_nam grad admission_dat


age course city phone email fees
d e e e e
Computer 987654321 NUL 50000.0
1 Rahul Sharma 20 Delhi A 2024-01-15
Science 0 L 0
Mathematic 987654321 NUL 50000.0
2 Priya Singh 19 Mumbai A 2024-01-15
s 1 L 0
Bangalor 987654321 NUL 50000.0
3 Amit Kumar 21 Physics A 2024-01-15
e 2 L 0
NUL 50000.0
... ... ... ... ... ... ... A 2024-01-15
L 0

What Happened:
 ✅ 2 new columns added simultaneously
 ✅ admission_date: All students got '2024-01-15' as default
 ✅ fees: All students got 50000.00 as default fees
 ✅ Table now has 11 columns total
 ✅ All 20 students' original data preserved

ALTER Example 5: Dropping a Column


Business Need:

"We don't need phone numbers anymore, remove phone column"

Query:
ALTER TABLE Students DROP COLUMN phone;

Effect After Query:

BEFORE DROP:

last_na
student_id first_name age course city phone email grade admission_date fees
me
Comput
987654 NUL
1 Rahul Sharma 20 er Delhi A 2024-01-15 50000.00
3210 L
Science

AFTER DROP:

student_i first_nam last_nam grad admission_da


age course city email fees
d e e e te
Computer NUL 50000.0
1 Rahul Sharma 20 Delhi A 2024-01-15
Science L 0
Mathematic NUL 50000.0
2 Priya Singh 19 Mumbai A 2024-01-15
s L 0
Bangalor NUL 50000.0
3 Amit Kumar 21 Physics A 2024-01-15
e L 0
... ... ... ... ... ... ... ... ... ...

What Happened:
 ❌ Phone column completely removed
 ❌ All phone numbers permanently lost (9876543210, 9876543211, etc.)
 ✅ All other data perfectly preserved
 ✅ Table now has 10 columns (was 11, now 10)
 ⚠️WARNING: This action cannot be undone!

Current Table Status After All ALTER Commands:


Final Table Structure (10 columns):

1. student_id (INT, PRIMARY KEY, AUTO_INCREMENT)


2. first_name (VARCHAR(50), NOT NULL)
3. last_name (VARCHAR(50), NOT NULL)
4. age (INT)
5. course (VARCHAR(150)) - size increased
6. city (VARCHAR(50))
7. email (VARCHAR(100)) - newly added
8. grade (CHAR(2), DEFAULT 'A') - newly added
9. admission_date (DATE, DEFAULT '2024-01-15') - newly added
10. fees (DECIMAL(8,2), DEFAULT 50000.00) - newly added

Removed:

 ❌ phone column (dropped)

🗑️TRUNCATE Command - Emptying the Table

What is TRUNCATE?
TRUNCATE removes all data from table instantly but keeps the table structure. Like emptying a
drawer but keeping the drawer itself.

Why Use TRUNCATE?


When you want to delete all data quickly but plan to use the same table structure again.

Syntax:
TRUNCATE TABLE table_name;

TRUNCATE Example:
Business Need:

"New academic year starting! Remove all current student data but keep the table ready for new
admissions."

BEFORE TRUNCATE (Our 20 Students):

student_id first_name last_name age course city email grade admission_date fees
Computer
1 Rahul Sharma 20 Delhi NULL A 2024-01-15 50000.00
Science
2 Priya Singh 19 Mathematics Mumbai NULL A 2024-01-15 50000.00
3 Amit Kumar 21 Physics Bangalore NULL A 2024-01-15 50000.00
... ... ... ... ... ... ... ... ... ...
student_id first_name last_name age course city email grade admission_date fees
Computer
20 Sakshi Singh 20 Ghaziabad NULL A 2024-01-15 50000.00
Science

Query:
TRUNCATE TABLE Students;

AFTER TRUNCATE:

student_id first_name last_name age course city email grade admission_date fees

(Completely empty table -


all 20 students gone)

What Happened:

 ❌ All 20 students' data completely deleted


 ❌ Rahul, Priya, Amit... all gone
 ✅ Table structure perfectly preserved (10 columns still there)
 ✅ All column properties intact (data types, constraints, defaults)
 ✅ Table ready for new data
 ⚡ Operation completed in milliseconds (very fast)
 🔄 AUTO_INCREMENT reset to 1 (next student will get ID = 1)

Proof Table Structure Still Exists:


DESCRIBE Students;

Output:

Column Type Null Key Default Extra


student_id int NO PRI NULL auto_increment
first_name varchar(50) NO NULL
last_name varchar(50) NO NULL
age int YES NULL
course varchar(150) YES NULL
city varchar(50) YES NULL
email varchar(100) YES NULL
grade char(2) YES A
admission_date date YES 2024-01-15
fees decimal(8,2) YES 50000.00

Structure is 100% intact!

🏚️DROP Command - Destroying the Table


What is DROP?
DROP command completely removes the table including structure and all data. Like
demolishing an entire building - nothing remains.

Why Use DROP?


When you no longer need the table and want to free up database space completely.
Syntax:
DROP TABLE table_name;

DROP Example:
Business Need:

"University is closing the department. We no longer need the Students table at all."

BEFORE DROP:

 Students table exists (even though empty after TRUNCATE)


 Table structure is available
 Can insert new data anytime
 Takes up database space

Query:
DROP TABLE Students;

AFTER DROP:

Complete Destruction:

 ❌ Students table doesn't exist anymore


 ❌ All 10 columns gone forever
 ❌ Table structure completely removed
 ❌ Cannot insert data (no table to insert into)
 ❌ All constraints, indexes, triggers removed

Proof Table is Gone:


SELECT * FROM Students;

Error: Table 'Students' doesn't exist

DESCRIBE Students;

Error: Table 'Students' doesn't exist

The table is completely wiped out!


📊 Complete Journey Summary
What We Did:
Phase 1: CREATE

 Built Students table with 7 columns


 Added 20 student records
 Table ready for use

Phase 2: ALTER (Multiple Changes)

 Added email column (8th column)


 Added grade column with default 'A' (9th column)
 Modified course column size (100 → 150 characters)
 Added admission_date and fees columns (10th & 11th columns)
 Dropped phone column (back to 10 columns)
 Result: Structure evolved, all data preserved

Phase 3: TRUNCATE

 Removed all 20 students' data


 Kept table structure intact
 Table empty but ready for new data

Phase 4: DROP

 Completely destroyed the table


 Nothing remains
 Need to CREATE again to use

🎯 Student Memory Guide


Command What Happens Real Life Example Data Structure
None
CREATE Build new table Building a new house ✅ New
(empty)
Renovating house (add/remove
ALTER Change structure ✅ Safe 🔄 Modified
rooms)
TRUNCATE Empty all data Remove all furniture from house ❌ Gone ✅ Safe
Destroy
DROP Demolish entire house ❌ Gone ❌ Gone
everything
💡 Key Points:
1. CREATE = Start from scratch
2. ALTER = Modify but keep data safe
3. TRUNCATE = Quick empty, structure stays
4. DROP = Complete destruction

Remember:

 ALTER is your friend (keeps data safe)


 TRUNCATE is faster than DELETE for removing all data
 DROP is permanent destruction - be very careful!
 Always backup before TRUNCATE or DROP

You might also like