๐Ÿš€ SQL Commands in Databases: DDL & DML with CRUD Operations (Complete Guide)


๐ŸŒŸ 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)


FeatureDDLDML
Full FormData Definition LanguageData Manipulation Language
PurposeStructureData
CommandsCREATE, ALTER, DROPINSERT, SELECT, UPDATE, DELETE
RollbackNot easyPossible

๐Ÿคฏ Common Confusions


๐Ÿค” TRUNCATE vs DELETE

TRUNCATEDELETE
FasterSlower
Removes all dataRemoves selective data
No WHEREUses 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 ๐Ÿ”ฅ

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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