SQL
🧠 Day 5: SQL Commands, Data Types, and Table Operations (DDL, DML, TCL, DQL)
🧠 Day 5: SQL Commands, Data Types, and Table Operations (DDL, DML, TCL, DQL)
🏗️ SQL Command Categories
SQL commands are grouped based on their purpose:
| Type | Full Form | Purpose |
|---|---|---|
| DDL | Data Definition Language | Defines and manages database structure |
| DML | Data Manipulation Language | Handles data inside tables |
| TCL | Transaction Control Language | Manages transactions and data integrity |
| DQL | Data Query Language | Retrieves data from tables |
⚙️ 1️⃣ DDL – Data Definition Language
Used to define or change table structure.
| Command | Description |
|---|---|
| CREATE | Creates new tables or databases |
| ALTER | Modifies existing table structure |
| RENAME | Changes table name |
| TRUNCATE | Removes all records but keeps table structure |
| DROP | Deletes table along with structure |
🔹 Syntax:
✏️ 2️⃣ DML – Data Manipulation Language
Used to add, modify, or remove records.
| Command | Description |
|---|---|
| INSERT | Adds new records |
| UPDATE | Modifies existing records |
| DELETE | Removes specific records |
🔹 Syntax:
🔹 Multi-row Insert:
🔒 3️⃣ TCL – Transaction Control Language
Used to manage database transactions.
| Command | Description |
|---|---|
| COMMIT | Saves changes permanently |
| ROLLBACK | Reverts to the last commit or savepoint |
| SAVEPOINT | Creates a checkpoint within a transaction |
🔹 Syntax:
Example Workflow:
🔍 4️⃣ DQL – Data Query Language
Only one key command — SELECT — used to query data.
🧩 SQL Data Types
| Data Type | Description |
|---|---|
| NUMBER (INT, FLOAT, DECIMAL) | Numeric values |
| CHAR(n) | Fixed-length string (static memory) |
| VARCHAR(n) | Variable-length string (dynamic memory) |
| LONG | Up to 35,000 characters |
| DATE | Stores date and time |
| BLOB | Binary Large Object (e.g., images, files) |
| CLOB | Character Large Object (text data) |
🧮 Table Example
🔁 Difference Between DELETE, TRUNCATE, and DROP
| Feature | DELETE | TRUNCATE | DROP |
|---|---|---|---|
| Command Type | DML | DDL | DDL |
| Removes | Specific rows | All rows (keeps structure) | Table + structure |
| WHERE Clause | Allowed | Not allowed | Not applicable |
| Rollback | ✅ Possible | ❌ Not possible | ❌ Not possible |
| Speed | Slower | Faster | Fastest (permanent) |
🔹 Syntax:
✅ Quick Recap
-
DDL: Create and modify database objects
-
DML: Insert, update, delete data
-
TCL: Manage transactions (Commit, Rollback, Savepoint)
-
DQL: Query and retrieve data
-
Data Types: Number, Char, Varchar, Long, Date, Blob, Clob