๐ Introduction
When working with databases in Java (JDBC) or any application, you interact using SQL commands ๐งพ
๐ These commands are broadly divided into:
- ๐๏ธ DDL (Data Definition Language) โ Structure of database
- ๐งพ DML (Data Manipulation Language) โ Data inside database
๐ฏ Why Learn DDL & DML?
๐ Because:
- DDL โ Creates and manages tables ๐๏ธ
- DML โ Performs CRUD operations ๐
๐ Both are essential for real-world applications ๐ฏ
๐๏ธ PART 1: DDL (Data Definition Language)
๐ง What is DDL?
๐ Used to define and manage database structure
๐น 1. CREATE
๐ Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype
);
โ Example
CREATE TABLE student (
id INT,
name VARCHAR(50)
);
๐ Explanation
- Creates a new table
- Defines columns and data types
๐น 2. ALTER
๐ Syntax
ALTER TABLE table_name
ADD column_name datatype;
โ Example
ALTER TABLE student
ADD age INT;
๐ Explanation
- Modifies existing table
- Adds new column
๐น 3. DROP
๐ Syntax
DROP TABLE table_name;
โ Example
DROP TABLE student;
๐ Explanation
- Deletes entire table permanently โ
๐น 4. TRUNCATE
๐ Syntax
TRUNCATE TABLE table_name;
โ Example
TRUNCATE TABLE student;
๐ Explanation
- Deletes all records
- Table structure remains
โ ๏ธ DDL Key Points
โ๏ธ Changes database structure
โ๏ธ Auto-commit (cannot rollback easily)
โ๏ธ Fast execution
๐งพ PART 2: DML (Data Manipulation Language)
๐ง What is DML?
๐ Used to manipulate data inside tables
๐ CRUD Operations using DML
๐ข 1. CREATE โ INSERT
๐ Syntax
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
โ Example
INSERT INTO student (id, name)
VALUES (1, 'Ajay');
๐ Explanation
- Adds new row into table
๐ต 2. READ โ SELECT
๐ Syntax
SELECT column1, column2
FROM table_name;
โ Example
SELECT * FROM student;
๐น With Condition
SELECT * FROM student
WHERE id = 1;
๐ Explanation
- Retrieves data
- WHERE filters records
๐ก 3. UPDATE
๐ Syntax
UPDATE table_name
SET column1 = value1
WHERE condition;
โ Example
UPDATE student
SET name = 'Rahul'
WHERE id = 1;
๐ Explanation
- Modifies existing data
โ ๏ธ Dangerous Query
UPDATE student SET name = 'Test';
๐ Updates ALL rows โ
๐ด 4. DELETE
๐ Syntax
DELETE FROM table_name
WHERE condition;
โ Example
DELETE FROM student
WHERE id = 1;
๐ Explanation
- Deletes specific records
โ ๏ธ Dangerous Query
DELETE FROM student;
๐ Deletes ALL data โ
โ๏ธ DDL vs DML (Important Comparison)
| Feature | DDL | DML |
|---|---|---|
| Full Form | Data Definition Language | Data Manipulation Language |
| Purpose | Structure | Data |
| Commands | CREATE, ALTER, DROP | INSERT, SELECT, UPDATE, DELETE |
| Rollback | Not easy | Possible |
๐คฏ Common Confusions
๐ค TRUNCATE vs DELETE
| TRUNCATE | DELETE |
|---|---|
| Faster | Slower |
| Removes all data | Removes selective data |
| No WHERE | Uses WHERE |
๐ค DROP vs DELETE
- DROP โ removes table โ
- DELETE โ removes data only
๐ผ Interview Questions
โ What is DDL?
๐ Used to define database structure
โ What is DML?
๐ Used to manipulate data
โ Difference between TRUNCATE and DELETE?
๐ TRUNCATE removes all data, DELETE can filter
๐ฏ Best Practices
โ๏ธ Always use WHERE in UPDATE & DELETE
โ๏ธ Backup before DROP/TRUNCATE
โ๏ธ Use proper data types
โ๏ธ Test queries first
๐ Conclusion
Understanding both DDL and DML is essential for database programming ๐
๐ In this blog, you learned:
- โ๏ธ Database structure creation (DDL)
- โ๏ธ Data manipulation (DML)
- โ๏ธ CRUD operations with SQL
- โ๏ธ Common mistakes & best practices
๐ก Final Thought
๐ DDL builds the structure ๐๏ธ
๐ DML runs the application logic ๐
๐ฅ One-Line Summary
๐ โDDL defines database structure, while DML performs CRUD operations on data.โ
๐ป Happy Coding! ๐
Master SQL โ Master Databases ๐ฅ