Class 12 Informatics Practices Chapter 05 ยท 26 min read

๐Ÿ–ฅ๏ธ MySQL SQL Revision Tour

Unit 2 ยท Ch 5 Oct 05, 2026
Chapter Quiz Share

In Class 11 you learnt the basics of databases and SQL. This chapter is a revision tour of that material โ€” the relational data model, creating tables with constraints, and inserting, querying, updating and deleting data, then altering and dropping tables โ€” before the advanced topics that follow, such as Joins and Python connectivity.

๐Ÿ’ก How to use these notes

Even if you remember some of this from Class 11, read it all, paying attention to the "small details" such as NULL handling, constraint syntax, and DELETE vs DROP. Type out every SQL command yourself.


5.1 ๐ŸŒŸ Introduction

Let's refresh the big picture before diving into commands:

graph TD
    DB["๐Ÿ—„๏ธ Database\nOrganised collection\nof related data"]
    DBMS["โš™๏ธ DBMS\nSoftware to manage\ndatabases"]
    RDBMS["๐Ÿ”— RDBMS\nDBMS that stores data\nin TABLES"]
    SQL["๐Ÿ—ฃ๏ธ SQL\nLanguage to talk\nto RDBMS"]
    MYSQL["๐Ÿฌ MySQL\nA popular RDBMS\nsoftware"]

    DB --> DBMS --> RDBMS --> SQL
    RDBMS --> MYSQL

    style DBMS fill:#2196F3,color:#fff
    style RDBMS fill:#9C27B0,color:#fff
    style MYSQL fill:#4CAF50,color:#fff
๐Ÿ—„๏ธ

Database

Organised collection of related data

Student recordslibrary catalogue
โš™๏ธ

DBMS

Software to manage databases

MySQLOraclePostgreSQL
๐Ÿ”—

RDBMS

DBMS storing data as linked TABLES

MySQL is an RDBMS!
๐Ÿ—ฃ๏ธ

SQL

The language to talk to RDBMS

CREATESELECTINSERT...

5.2 ๐Ÿ“ Relational Data Model

The Relational Model organises all data into simple 2D tables โ€” rows and columns, just like an Excel sheet!

     RollNo    Name      Marks
     ------    ----      -----
       1       Amit       85      โ† This entire row = ONE Tuple
       2       Neha       90
       3       Raj        78

     โ†‘
  This column = ONE Attribute

Key Terminology โ€” Memorise These! ๐Ÿง 

Term Simple Meaning Example
Relation Fancy word for a Table The Student table itself
Tuple Fancy word for a Row One student's complete record
Attribute Fancy word for a Column 'Marks' column
Degree Number of columns 3 columns โ†’ Degree = 3
Cardinality Number of rows 3 students โ†’ Cardinality = 3
graph LR
    T["Table = Relation"]
    R["Row = Tuple"]
    C["Column = Attribute"]

    style T fill:#FF9800,color:#fff
    style R fill:#2196F3,color:#fff
    style C fill:#4CAF50,color:#fff

The Key Family โ€” Primary, Candidate, Alternate, Foreign ๐Ÿ”‘

๐Ÿ”‘

Primary Key

Uniquely identifies EVERY row; cannot be NULL or duplicate

RollNo โ€” no two students share it
๐Ÿฅˆ

Candidate Key

ANY column that COULD be chosen as primary key

RollNoAdmission Number โ€” both are "candidates"
๐Ÿฅ‰

Alternate Key

A Candidate Key NOT chosen as primary

If RollNo is PKAdmission Number becomes Alternate
๐Ÿ”—

Foreign Key

Links to another table's Primary Key

DeptId in Student tablelinking to Department table

Analogy: Imagine choosing a team captain (Primary Key) from a group of eligible players (Candidate Keys). The ones NOT chosen are still "eligible" โ€” they're your Alternate Keys!

๐Ÿ“‹ Board Exam Tip

"Define Primary Key, Candidate Key, and Alternate Key." โ€” 3-mark question. Primary Key: Uniquely identifies each row; can never be NULL. Candidate Key: Any column(s) eligible to become the primary key. Alternate Key: A candidate key that was NOT selected as primary key.


5.3 ๐Ÿฌ MySQL โ€” A Popular RDBMS

MySQL is free, open-source RDBMS software used worldwide โ€” from small school projects to giant companies!

5.3.1 MySQL and SQL ๐Ÿ—ฃ๏ธ

Important distinction: MySQL is the software (the engine). SQL is the language you use to talk to it (the steering wheel).

graph LR
    YOU["๐Ÿ‘ค You type\nSQL commands"]
    MYSQL["๐Ÿฌ MySQL Server\n(understands SQL,\nstores your data)"]
    RESULT["๐Ÿ“Š Result\n(your data,\nback to you)"]

    YOU -->|"SQL"| MYSQL --> RESULT

    style MYSQL fill:#4CAF50,color:#fff

5.3.2 Common MySQL Data Types ๐Ÿ“ฆ

Every column needs a data type โ€” this decides what kind of value it can hold.

Category Data Type Description Example
Whole Numbers INT Integer values 25, 100, -5
Decimal Numbers DECIMAL(M,D) Exact decimals โ€” M=total digits, D=after decimal DECIMAL(5,2) โ†’ 123.45
Fixed Text CHAR(n) Always uses exactly n characters CHAR(10) for Pincode
Flexible Text VARCHAR(n) Uses only as much space as needed (up to n) VARCHAR(30) for Name
Date DATE Format is ALWAYS 'YYYY-MM-DD' '2026-07-15'
๐Ÿ“ฆ

CHAR(n)

Fixed box โ€” always n-size, even if data is shorter

Good for PincodeGender (fixed length)
๐ŸŽ’

VARCHAR(n)

Flexible bag โ€” uses only what's needed

Good for NameAddress (varies a lot)
๐Ÿ“‹ Board Exam Tip

"Differentiate between CHAR and VARCHAR." โ€” 2-mark question asked constantly! CHAR(n) is fixed-length โ€” always reserves n characters (padded with spaces if shorter). VARCHAR(n) is variable-length โ€” uses only as much space as the actual data needs, up to n characters. CHAR is slightly faster; VARCHAR saves memory.


5.4 ๐Ÿ”“ Accessing Database in MySQL

Before creating any table, you need to CREATE and SELECT a database:

-- accessing_database.sql
CREATE DATABASE SchoolDB;    -- Create a brand-new database

USE SchoolDB;                -- Select it for use (MUST do this before creating tables!)

SHOW DATABASES;              -- See all databases on the server
Command Purpose
CREATE DATABASE name; Makes a new, empty database
USE name; Switches to that database โ€” REQUIRED before any table work
SHOW DATABASES; Lists all databases available
DROP DATABASE name; Permanently deletes a database (careful!)
โš  Forgetting USE!

If you skip USE SchoolDB;, MySQL doesn't know WHICH database you want to work in, and gives the error: "No database selected". Always run USE first!


5.5 ๐Ÿ—๏ธ Creating Tables in MySQL

The CREATE TABLE command builds a new table with named columns and data types.

-- create_table.sql
CREATE TABLE Student (
    RollNo   INT,
    Name     VARCHAR(30),
    Stream   VARCHAR(20),
    Fee      DECIMAL(10,2)
);

General Syntax:

CREATE TABLE table_name (
    column1_name  datatype,
    column2_name  datatype,
    ...
);
๐Ÿ“‹ Board Exam Tip

"Write SQL to create a table Student with RollNo (integer), Name (30-char string), Marks (decimal)." โ€” 3-mark question. Answer:

CREATE TABLE Student (
    RollNo INT,
    Name   VARCHAR(30),
    Marks  DECIMAL(5,2)
);

5.6 ๐Ÿ“ฅ Inserting Data into Table

The INSERT INTO command adds new rows (records) to a table.

Method 1 โ€” All Columns (must match table order EXACTLY):

-- insert_all_columns.sql
INSERT INTO Student VALUES (1, 'Rohan', 'Science', 2500.00);

Method 2 โ€” Specific Columns (Recommended! โญ):

-- insert_specific_columns.sql
INSERT INTO Student (RollNo, Name) VALUES (2, 'Priya');
-- Other columns (Stream, Fee) become NULL automatically

Inserting Multiple Rows at Once:

-- insert_multiple.sql
INSERT INTO Student VALUES
    (3, 'Karan', 'Commerce', 2200.00),
    (4, 'Sita',  'Science',  2800.00),
    (5, 'Meera', 'Arts',     1900.00);
Method When to use
All columns (no names) Quick, but MUST match exact table column order
Specific columns (named) Safer โ€” order doesn't matter, can skip columns
โš  String and Date values need single quotes!

INSERT INTO Student VALUES (1, Rohan, ...) โŒ โ€” missing quotes around 'Rohan' causes an ERROR! INSERT INTO Student VALUES (1, 'Rohan', ...) โœ… โ€” always quote text/date values.


5.7 ๐Ÿ” Making Simple Queries through SELECT Command

SELECT is the most-used SQL command โ€” it retrieves (fetches) data from tables.

Our reference Student table for ALL examples below:

RollNo Name Stream Fee
1 Rohan Science 2500.00
2 Priya Science NULL
3 Karan Commerce 2200.00
4 Sita Science 2800.00
5 Meera Arts 1900.00

5.7.1 Selecting All Data โญ

-- select_all.sql
SELECT * FROM Student;

The * (asterisk) means "give me ALL columns". This shows the entire table.


5.7.2 Selecting Particular Rows ๐ŸŽฏ

Use the WHERE clause to filter and show only specific rows that match a condition.

-- select_particular_rows.sql
SELECT * FROM Student WHERE Stream = 'Science';

Output:

RollNo  Name    Stream    Fee
1       Rohan   Science   2500.00
2       Priya   Science   NULL
4       Sita    Science   2800.00
-- Another example:
SELECT * FROM Student WHERE Fee > 2000;

Only rows that SATISFY the condition are shown โ€” this is called filtering!


5.7.3 Selecting Particular Columns ๐Ÿ“‹

Instead of * (all columns), name only the columns you want:

-- select_particular_columns.sql
SELECT Name, Fee FROM Student;

Output:

Name    Fee
Rohan   2500.00
Priya   NULL
Karan   2200.00
Sita    2800.00
Meera   1900.00

Combine columns AND rows together:

SELECT Name, Fee FROM Student WHERE Stream = 'Science';

5.7.4 Eliminating Redundant Data โ€” DISTINCT Keyword ๐Ÿงน

DISTINCT removes duplicate values, showing each unique value only once.

-- distinct_demo.sql
SELECT DISTINCT Stream FROM Student;

Output:

Stream
Science
Commerce
Arts

Even though "Science" appears 3 times in the table, DISTINCT shows it only ONCE!


5.7.5 Selecting from all the Rows โ€” ALL Keyword ๐Ÿ“Š

ALL is the OPPOSITE of DISTINCT โ€” and it's actually the DEFAULT behaviour (you rarely type it explicitly).

-- all_keyword_demo.sql
SELECT ALL Stream FROM Student;
-- Same as: SELECT Stream FROM Student;
-- Shows Science, Science, Commerce, Science, Arts (ALL rows, WITH duplicates)
Keyword Shows Duplicates? Is it the Default?
ALL โœ… Yes โœ… Yes (automatic)
DISTINCT โŒ No โŒ No (must be typed)

5.7.6 Viewing Structure of a Table ๐Ÿ—๏ธ

DESCRIBE (or DESC) shows the table's STRUCTURE (column names, data types) โ€” NOT the actual data!

-- describe_demo.sql
DESCRIBE Student;
-- OR:
DESC Student;

Output:

Field    Type           Null   Key   Default   Extra
RollNo   int            YES
Name     varchar(30)    YES
Stream   varchar(20)    YES
Fee      decimal(10,2)  YES
๐Ÿ“‹ Board Exam Tip

"Which command shows a table's structure without displaying its data?" โ€” 1-mark question! Answer: DESCRIBE table_name; or DESC table_name;


5.7.7 Performing Simple Calculations โž•

SQL can do math directly! You can calculate values right inside a SELECT statement.

-- simple_calculations.sql
SELECT 25 + 10;          -- Output: 35
SELECT 100 / 4;          -- Output: 25.0000
SELECT 6 * 7;            -- Output: 42

Calculations using actual table columns:

-- calculation_on_column.sql
SELECT Name, Fee * 12 FROM Student;   -- Annual Fee = Monthly Fee ร— 12

Output:

Name    Fee*12
Rohan   30000.00
Priya   NULL          โ† NULL ร— 12 = NULL (any math with NULL = NULL!)
Karan   26400.00
Sita    33600.00
Meera   22800.00
๐Ÿง  NULL "infects" every calculation! ๐Ÿง 

Any arithmetic done with NULL always gives NULL as the result! NULL + 100 = NULL, NULL * 5 = NULL. This is because NULL means "unknown" โ€” you can't do math with an unknown value!


5.7.8 Using Column Aliases ๐Ÿท๏ธ

An Alias gives a column a temporary, nicer name in the output โ€” using the AS keyword.

-- column_alias_demo.sql
SELECT Name, Fee * 12 AS AnnualFee FROM Student;

Output:

Name    AnnualFee
Rohan   30000.00
Karan   26400.00

The alias AnnualFee is much more readable than the messy default heading "Fee*12"! Remember: the alias is TEMPORARY โ€” it doesn't rename anything in the actual table.

๐Ÿ“‹ Board Exam Tip

"Write a query to display Name and Annual Fee (Fee ร— 12) with a proper column heading." โ€” 2-mark question! Answer: SELECT Name, Fee * 12 AS AnnualFee FROM Student;


5.7.9 Condition Based on a Range โ€” BETWEEN ๐Ÿ“

BETWEEN checks if a value falls within a range โ€” INCLUSIVE of both ends!

-- between_demo.sql
SELECT * FROM Student WHERE Fee BETWEEN 2000 AND 2800;

This is exactly the same as writing: WHERE Fee >= 2000 AND Fee <= 2800

Output:

RollNo  Name    Stream    Fee
1       Rohan   Science   2500.00
3       Karan   Commerce  2200.00
4       Sita    Science   2800.00     โ† 2800 IS included!

5.7.10 Condition Based on a List โ€” IN ๐Ÿ“‹

IN checks if a value matches ANY value from a given list โ€” a shortcut for multiple OR conditions.

-- in_demo.sql
SELECT * FROM Student WHERE Stream IN ('Science', 'Commerce');
-- Same as: WHERE Stream = 'Science' OR Stream = 'Commerce'

Output:

RollNo  Name    Stream     Fee
1       Rohan   Science    2500.00
2       Priya   Science    NULL
3       Karan   Commerce   2200.00
4       Sita    Science    2800.00

Meera (Arts) is excluded โ€” 'Arts' is not in our list!


5.7.11 Condition Based on Pattern Matches โ€” LIKE ๐Ÿ”Ž

LIKE searches for a text PATTERN using wildcard symbols.

Wildcard Meaning Example
% Zero or MORE characters 'R%' โ†’ matches Rohan, Raj, Riya
_ Exactly ONE character '_a%' โ†’ 2nd letter must be 'a'
-- like_demo.sql

-- Names starting with 'R':
SELECT * FROM Student WHERE Name LIKE 'R%';

-- Names where 2nd letter is 'a':
SELECT * FROM Student WHERE Name LIKE '_a%';

-- Names that are EXACTLY 5 characters long:
SELECT * FROM Student WHERE Name LIKE '_____';   -- 5 underscores!

-- Names ending in 'a':
SELECT * FROM Student WHERE Name LIKE '%a';
๐Ÿ”ค

Starts With

'R%'

Name begins with R
๐Ÿ”š

Ends With

'%a'

Name ends with 'a'
๐Ÿ”

Contains

'%an%'

Name has 'an' anywhere
๐Ÿ“‹ Board Exam Tip

"Write a query to display names of students whose name ends with 'a'." โ€” Common 2-mark question! Answer: SELECT * FROM Student WHERE Name LIKE '%a';


5.7.12 Searching for NULL ๐Ÿ•ณ๏ธ

Since NULL means "unknown", you CANNOT use = to find it! You must use special keywords.

-- null_search_demo.sql

-- WRONG way (returns NOTHING, even if NULLs exist!):
SELECT * FROM Student WHERE Fee = NULL;    โŒ

-- CORRECT way:
SELECT * FROM Student WHERE Fee IS NULL;       โœ…
SELECT * FROM Student WHERE Fee IS NOT NULL;   โœ…

Output of WHERE Fee IS NULL:

RollNo  Name    Stream    Fee
2       Priya   Science   NULL
โš  The #1 NULL Mistake in Exams!

WHERE Fee = NULL will ALWAYS return zero rows, even if NULL values exist in the table! Watch for this trick in questions. You MUST use IS NULL and IS NOT NULL โ€” never = or != with NULL!


5.8 ๐Ÿ”’ Creating Tables with SQL Constraints

5.8.1 SQL Constraints โ€” What Are They? ๐Ÿ›ก๏ธ

Constraints are RULES applied to columns that prevent bad or invalid data from being stored. Think of them as "quality control" for your table!

graph TD
    C["๐Ÿ”’ SQL CONSTRAINTS"]
    C1["NOT NULL\nMust have a value"]
    C2["UNIQUE\nNo duplicate values"]
    C3["PRIMARY KEY\nNOT NULL + UNIQUE\n(one per table)"]
    C4["FOREIGN KEY\nMust match another\ntable's Primary Key"]
    C5["CHECK\nMust satisfy a\ncondition"]
    C6["DEFAULT\nAuto-fills a value\nif none given"]

    C --> C1
    C --> C2
    C --> C3
    C --> C4
    C --> C5
    C --> C6

    style C fill:#9C27B0,color:#fff

All Six Constraints Explained Simply:

๐Ÿ”ด

NOT NULL

Column can NEVER be left empty

Name NOT NULL โ€” every student MUST have a name
๐ŸŸก

UNIQUE

No two rows can have the same value

Email UNIQUE โ€” no two students share an email
๐ŸŸข

PRIMARY KEY

Uniquely identifies each row (NOT NULL + UNIQUE combined)

RollNo PRIMARY KEY โ€” the ID of each student
๐Ÿ”ต

FOREIGN KEY

Links to another table's Primary Key

DeptId โ€” must exist in the Department table
๐ŸŸ 

CHECK

Value must pass a test/condition

Fee CHECK (Fee > 0) โ€” no negative fees allowed!
โšช

DEFAULT

Fills a value automatically if none given

Stream DEFAULT 'Science' โ€” assumed if not mentioned

Constraints Quick Reference:

Constraint Allows NULL? Allows Duplicates?
NOT NULL โŒ No โœ… Yes
UNIQUE โœ… Yes โŒ No
PRIMARY KEY โŒ No โŒ No
FOREIGN KEY โœ… Yes โœ… Yes
CHECK โœ… Yes โœ… Yes

5.8.2 Applying Table Constraints ๐Ÿ› ๏ธ

-- table_with_constraints.sql
CREATE TABLE Student (
    RollNo   INT PRIMARY KEY,                       -- Unique ID, can't be NULL
    Name     VARCHAR(30) NOT NULL,                  -- Cannot be blank
    Email    VARCHAR(50) UNIQUE,                     -- No duplicates
    Stream   VARCHAR(20) DEFAULT 'Science',          -- Auto-fills if skipped
    Fee      DECIMAL(10,2) CHECK (Fee > 0),          -- Must be positive!
    DeptId   INT,
    FOREIGN KEY (DeptId) REFERENCES Department(DeptId)   -- Links to another table
);

Testing the constraints:

-- Valid insert (follows all rules):
INSERT INTO Student (RollNo, Name, Fee) VALUES (1, 'Rohan', 2500);
-- Stream automatically becomes 'Science' (DEFAULT)

-- INVALID insert (violates CHECK โ€” negative fee!):
INSERT INTO Student (RollNo, Name, Fee) VALUES (2, 'Priya', -500);
-- โŒ ERROR! CHECK constraint failed.

-- INVALID insert (violates NOT NULL):
INSERT INTO Student (RollNo, Fee) VALUES (3, 2000);
-- โŒ ERROR! Name cannot be NULL.
๐Ÿ“‹ Board Exam Tip

"Write SQL to create a Student table with RollNo as Primary Key, Name that cannot be blank, and Fee that must always be positive." โ€” 4-mark question. Answer:

CREATE TABLE Student (
    RollNo  INT PRIMARY KEY,
    Name    VARCHAR(30) NOT NULL,
    Fee     DECIMAL(10,2) CHECK (Fee > 0)
);

5.9 ๐Ÿ” Viewing a Table Structure

We touched on this in 5.7.6, but let's cement it โ€” DESCRIBE shows structure, NOT data:

-- viewing_structure.sql
DESCRIBE Student;

Output:

Field    Type            Null   Key    Default    Extra
RollNo   int             NO     PRI
Name     varchar(30)     NO
Email    varchar(50)     YES    UNI
Stream   varchar(20)     YES               Science
Fee      decimal(10,2)   YES

What each column of the output means:

Output Column Meaning
Field Column name
Type Data type and size
Null Can this column be NULL? (YES/NO)
Key PRI = Primary Key, UNI = Unique
Default Default value (if any)
๐Ÿง  DESCRIBE vs SELECT * FROM! ๐Ÿง 

DESCRIBE table_name; โ†’ shows the STRUCTURE (blueprint) โ€” column names, types, constraints. SELECT * FROM table_name; โ†’ shows the DATA (actual rows) stored in the table. Don't confuse these two โ€” a typical one-mark question.


5.10 ๐Ÿ”„ Inserting Data into another Table

You can copy matching data from one table into another table, using INSERT INTO ... SELECT:

-- insert_into_another_table.sql

-- First, create a table for toppers:
CREATE TABLE Toppers (
    Name  VARCHAR(30),
    Fee   DECIMAL(10,2)
);

-- Copy data from Student table where Fee > 2500:
INSERT INTO Toppers (Name, Fee)
SELECT Name, Fee FROM Student WHERE Fee > 2500;

What happens:

graph LR
    STU["๐Ÿ“‹ Student Table\n(All Students)"]
    FILTER["WHERE Fee > 2500"]
    TOP["๐Ÿ“‹ Toppers Table\n(Only matching rows\ncopied here!)"]

    STU --> FILTER --> TOP

    style FILTER fill:#FF9800,color:#fff
    style TOP fill:#4CAF50,color:#fff

Very useful for creating backups, archiving old data, or splitting data into categories โ€” like moving "Toppers" into their own table!

๐Ÿ“‹ Board Exam Tip

"Write a query to copy names and marks of students scoring above 90 into a new table Toppers." โ€” 3-mark question! Answer: INSERT INTO Toppers (Name, Marks) SELECT Name, Marks FROM Student WHERE Marks > 90;


5.11 โœ๏ธ Modifying Data in Tables

The UPDATE command changes existing data in a table.

-- update_demo.sql
UPDATE Student
SET Fee = Fee + 500
WHERE Stream = 'Commerce';

More Examples:

-- Update a single row:
UPDATE Student SET Fee = 3000 WHERE RollNo = 1;

-- Update multiple columns at once:
UPDATE Student SET Fee = 2600, Stream = 'Arts' WHERE RollNo = 2;

-- Give everyone in Science a 10% raise:
UPDATE Student SET Fee = Fee * 1.10 WHERE Stream = 'Science';
โš  NEVER forget the WHERE clause!

UPDATE Student SET Fee = 5000; (with NO WHERE!) changes EVERY SINGLE ROW in the entire table to Fee = 5000! Always double-check your WHERE condition before pressing Enter!

๐Ÿ“‹ Board Exam Tip

"Write a query to increase the fee of all Science students by 500." โ€” 2-mark question! Answer: UPDATE Student SET Fee = Fee + 500 WHERE Stream = 'Science';


5.12 ๐Ÿ—‘๏ธ Deleting Data from Tables

The DELETE command removes rows from a table.

-- delete_demo.sql

-- Delete ONE specific row:
DELETE FROM Student WHERE RollNo = 5;

-- Delete MULTIPLE matching rows:
DELETE FROM Student WHERE Stream = 'Arts';

-- Delete ALL rows (table becomes empty, but structure remains!):
DELETE FROM Student;
๐Ÿง  DELETE keeps the table alive! ๐Ÿง 

DELETE FROM Student; (without WHERE) removes ALL rows โ€” but the empty table STILL EXISTS and can accept new data. This is very different from DROP TABLE, which destroys the table entirely (covered in 5.14)!


5.13 ๐Ÿ”ง Altering Tables

ALTER TABLE changes the STRUCTURE of a table (adding/removing/modifying columns) โ€” NOT the data inside it!

graph TD
    ALTER["๐Ÿ”ง ALTER TABLE"]
    ADD["ADD\nAdd a new column"]
    MODIFY["MODIFY\nChange a column's\ndata type/size"]
    DROP["DROP COLUMN\nRemove a column"]

    ALTER --> ADD
    ALTER --> MODIFY
    ALTER --> DROP

    style ALTER fill:#FF9800,color:#fff

Adding a new column:

-- alter_add.sql
ALTER TABLE Student ADD Email VARCHAR(50);

Changing a column's data type/size:

-- alter_modify.sql
ALTER TABLE Student MODIFY Name VARCHAR(50);
-- Expands Name from VARCHAR(30) to VARCHAR(50)

Removing a column:

-- alter_drop_column.sql
ALTER TABLE Student DROP COLUMN Email;
-- Email column AND its data are permanently gone!

Renaming a column (bonus!):

-- alter_change.sql
ALTER TABLE Student CHANGE Fee Tuition DECIMAL(10,2);
-- Renames 'Fee' to 'Tuition'
Clause Purpose
ADD column datatype Add a new column
MODIFY column new_datatype Change type/size (keep same name)
CHANGE old new datatype Rename column (and optionally change type)
DROP COLUMN column Delete a column permanently
๐Ÿ“‹ Board Exam Tip

"Write SQL to add a column Phone (VARCHAR 15) to the Student table." โ€” 2-mark question! Answer: ALTER TABLE Student ADD Phone VARCHAR(15);

โš  ALTER vs UPDATE โ€” Don't Confuse Them!

ALTER TABLE changes the table's STRUCTURE (columns) โ€” it's a DDL command. UPDATE changes the actual DATA (values inside rows) โ€” it's a DML command. Mixing these up is a classic exam mistake!


5.14 ๐Ÿ’ฃ Dropping Tables

DROP TABLE permanently and completely destroys a table โ€” structure AND data โ€” with no way to undo it!

-- drop_table_demo.sql
DROP TABLE Student;
-- Student table is COMPLETELY GONE. Structure gone. Data gone. Forever.
graph LR
    BEFORE["๐Ÿ“‹ Student Table\nStructure + All Data"]
    DROP["๐Ÿ’ฃ DROP TABLE Student;"]
    AFTER["โŒ COMPLETELY GONE\nNo Recycle Bin\nNo Undo!"]

    BEFORE --> DROP --> AFTER

    style DROP fill:#F44336,color:#fff
    style AFTER fill:#9E9E9E,color:#fff

DELETE vs DROP โ€” The Ultimate Comparison Table!

Feature DELETE DROP
What it removes Rows (data) only Entire table (structure + data)
WHERE clause? โœ… Can use it โŒ Cannot use it
Table survives? โœ… Yes, empty table remains โŒ No, table is gone
Command type DML DDL
Can be undone? โœ… Yes (ROLLBACK, if not committed) โŒ No (auto-committed)
โš  DROP is FOREVER!

There is NO undo button for DROP TABLE. Always be 100% sure before running this command, and take a backup if there's ANY chance you'll need the data again!

๐Ÿ“‹ Board Exam Tip

"Differentiate between DELETE and DROP commands." โ€” 2-mark question. Answer: DELETE removes rows (data) from a table but the table itself remains โ€” it's a DML command that can use WHERE. DROP removes the entire table (structure + data) permanently โ€” it's a DDL command that cannot use WHERE and cannot be undone.


โš ๏ธ Common Errors and Misconceptions

Mistake What's Wrong Correct Understanding
โŒ WHERE Fee = NULL Always returns ZERO rows โœ… Use WHERE Fee IS NULL
โŒ Thinking DELETE removes the table Only removes ROWS โœ… Table structure stays; use DROP to remove the table itself
โŒ Using "double quotes" for text Errors in strict SQL mode โœ… Always use 'single quotes' for text/dates
โŒ Confusing ALTER and UPDATE Wrong command used โœ… ALTER = structure (columns); UPDATE = data (values)
โŒ Forgetting WHERE in UPDATE/DELETE Changes/removes ALL rows! โœ… Always double-check WHERE before running
โŒ CHAR and VARCHAR are the same They behave differently โœ… CHAR = fixed length; VARCHAR = variable length
โŒ Thinking Candidate Key = Primary Key They're different concepts โœ… Primary Key IS a Candidate Key, but not vice versa

๐ŸŽฏ Sample Board Exam Questions

Q1: Very Short Answer [1 mark each]

a) What is the full form of DBMS? โ†’ Database Management System

b) Which command is used to view a table's structure? โ†’ DESCRIBE (or DESC)

c) Which keyword removes duplicate values in a SELECT query? โ†’ DISTINCT

d) What is the correct format for a DATE value in MySQL? โ†’ 'YYYY-MM-DD'

e) Which constraint ensures a column can never contain duplicate values? โ†’ UNIQUE


Q2: Short Answer [2 marks]

Q: Differentiate between DELETE and DROP.

DELETE is a DML command that removes rows (data) from a table using a WHERE clause; the table itself continues to exist (empty if all rows deleted). DROP is a DDL command that removes the ENTIRE table โ€” both structure and data โ€” permanently, with no WHERE clause option and no way to undo it.


Q3: Practical SQL [4 marks]

Q: Write SQL statements to: a) Create a table Employee with EmpId (Primary Key), Name (cannot be blank), and Salary (must be positive). b) Insert a record. c) Display employees earning more than 30000, sorted... (display all fields). d) Increase salary of all employees by 10%.

-- a) Create table
CREATE TABLE Employee (
    EmpId   INT PRIMARY KEY,
    Name    VARCHAR(30) NOT NULL,
    Salary  DECIMAL(10,2) CHECK (Salary > 0)
);

-- b) Insert record
INSERT INTO Employee VALUES (1, 'Aman', 35000);

-- c) Display employees earning > 30000
SELECT * FROM Employee WHERE Salary > 30000;

-- d) Increase salary by 10%
UPDATE Employee SET Salary = Salary * 1.10;

Q4: Output Based [2 marks]

Q: What is the output?

SELECT Name FROM Student WHERE Fee IS NULL;

(Given: Priya has NULL fee)

Output:

Name
Priya

โœ๏ธ Practice Problems

  1. Write SQL to create a Book table with BookId (Primary Key), Title (cannot be blank), Price (must be greater than 0), and Genre (default value 'Fiction').

  2. Insert 3 records into the Book table, one with a missing Price (NULL).

  3. Write a query to display all books priced between โ‚น200 and โ‚น500.

  4. Write a query to display book titles that start with the letter 'H'.

  5. Write a query to find books where Genre is either 'Fiction' or 'Mystery' using the IN operator.

  6. Write SQL to add a new column PublishYear to the Book table.

  7. Write a query to display book titles where Price is NULL.

  8. Explain the difference between CHAR(10) and VARCHAR(10) with a real-world example.

  9. Write SQL to copy all books priced above โ‚น500 into a new table called PremiumBooks.

  10. What will happen if you run DELETE FROM Book; followed by SELECT * FROM Book;? What if you instead ran DROP TABLE Book; followed by SELECT * FROM Book;? Explain both outcomes.

Take the Chapter 05 Quiz Custom Quiz
Solved Board Questions

Practice real board exam questions for this chapter.

View Solved Board Questions
Learning Support

Need Help With This Chapter?

Save key topics for exam revision, ask questions to teachers, or submit content corrections.

Verified Doubts & Teacher Answers

Chapter 05 Quiz