Database Management System (DBMS) / SQL(STRUCTURED QUERY LANGUAGE)

Data Definition Language (DDL)

DDL refers to the SQL commands used to define, modify, and remove the structure of database objects such as tables, schemas, and indexes.


๐Ÿงฑ CREATE โ€“ Create New Database Objects

Used to create tables, databases, views, indexes, etc.

๐Ÿ“Œ Example:

-- Step 1: Create the students table
CREATE TABLE students (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  grade CHAR(2)
);
-- Step 2: Insert some sample student data
INSERT INTO students (id, name, grade) VALUES 
(1, 'Swastika', 'A+'),
(2, 'Anya', 'B'),
(3, 'Rahul', 'A'),
(4, 'Neha', 'C');

-- Step 3: View the student records
SELECT * FROM students;

๐Ÿ”ธ Creates a table named students with 4 columns


๐Ÿงฐ ALTER โ€“ Modify an Existing Table

Used to add, modify, or drop columns and constraints.

๐Ÿ“Œ Add a column:

-- Step 1: Create the students table
-- Step 2: Insert some sample student data

ALTER TABLE students ADD email VARCHAR(100);

-- Step 3: View the student records

โ˜˜๏ธWhat this does:

  • Adds a new column named email to the existing students table.

  • The column type is VARCHAR(100) โ†’ it can store email addresses up to 100 characters.

๐Ÿ“Œ Modify a column:

-- Step 1: Create the students table
-- Step 2: Insert some sample student data

-- Step 3: Modify grade column type
ALTER TABLE students 
ALTER COLUMN grade VARCHAR(5);

INSERT INTO students (id, name, grade) VALUES
(5, 'Ishaan', 'FAIL');

-- Step 5: View the student records

๐Ÿ“Œ What this does:

  • Changes the data type of the grade column from CHAR(2) (or whatever it was) to VARCHAR(5).

โœ… Why do this?

  • Allows storing longer grade values like 'A++', 'FAIL', 'B+1', etc.


๐Ÿ—‘๏ธ DROP โ€“ Delete Database Objects

Permanently deletes a table, database, or view

๐Ÿ“Œ Drop a table:

DROP TABLE students;

๐Ÿงน TRUNCATE โ€“ Delete All Records from a Table

Removes all rows from a table, but keeps its structure

๐Ÿ“Œ Example:

TRUNCATE TABLE students;

โšก Faster than DELETE

โŒ Cannot be rolled back in many databases


โœ… Summary of DDL Commands

Command

Purpose

๐Ÿงฑ CREATE

Define a new table or database

๐Ÿงฐ ALTER

Modify structure of an existing table

๐Ÿ—‘๏ธ DROP

Delete table or database completely

๐Ÿงน TRUNCATE

Remove all data, keep table structure