Class 11 Informatics Practices Chapter 09 ยท 19 min read

๐Ÿฌ Introduction to SQL

Unit 3 ยท Ch 9 Sep 30, 2026
Chapter Quiz

In the last chapter, you learnt what a database and a table are. But how do we actually talk to a database โ€” to create a table, add a student, or fix a wrong entry? ๐Ÿ—ฃ๏ธ We use a special language called SQL! This chapter answers the BIG questions: What is SQL and MySQL? What are DDL and DML commands? Which data types can a column have? How do constraints keep wrong data out? And how do we create a database, create a table, change its structure, and insert records? Let's write our first queries! ๐Ÿ’ป

๐Ÿ’ก How to use these notes

The most tested topics are DDL vs DML, CHAR vs VARCHAR, constraints (PRIMARY KEY, NOT NULL, UNIQUE), writing a CREATE TABLE command, ALTER TABLE (ADD, MODIFY, DROP), and INSERT INTO. We'll build ONE table โ€” STUDENT โ€” step by step through the whole chapter. Type every command in MySQL along with the notes!


9.1 ๐Ÿ Introduction โ€” What is SQL?

SQL (Structured Query Language) is the standard language used to create, manage and ask questions (queries) of a relational database. It is pronounced "S-Q-L" or "sequel".

MySQL is a popular, free and open-source RDBMS (Relational DBMS) software that understands SQL.

Analogy: If the database is a library ๐Ÿ“š and MySQL is the librarian, then SQL is the language you speak to the librarian โ€” "Please add this book", "Show me all books by Premchand", "Remove this old book". ๐Ÿ—ฃ๏ธ

๐Ÿ—ฃ๏ธ

English-like

Commands look like simple English sentences

CREATE TABLESELECT
๐Ÿ”ค

Not Case Sensitive

select, SELECT and Select all work the same (data inside quotes may differ)

Any case
๐Ÿ”š

Ends with a Semicolon

Every SQL statement ends with a semicolon ;

Semicolon
๐ŸŒ

Standard Language

The same SQL works in MySQL, Oracle, SQL Server and others

Portable
๐Ÿง  Writing style used in these notes ๐Ÿง 

SQL keywords are written in CAPITALS (CREATE, SELECT) and table and column names in normal case. This isn't compulsory โ€” it just makes commands easy to read!


9.2 ๐Ÿงฉ Types of SQL Commands

graph TD
    SQL["๐Ÿ—ฃ๏ธ SQL COMMANDS"]
    DDL["๐Ÿ—๏ธ DDL\nData Definition Language\n(works on the STRUCTURE)"]
    DML["๐Ÿ“ DML\nData Manipulation Language\n(works on the DATA)"]
    D1["CREATE\nALTER\nDROP"]
    D2["INSERT\nUPDATE\nDELETE\nSELECT"]

    SQL --> DDL --> D1
    SQL --> DML --> D2

    style DDL fill:#2196F3,color:#fff
    style DML fill:#4CAF50,color:#fff
DDL (Data Definition Language) DML (Data Manipulation Language)
Defines or changes the structure (design) of tables and databases Works on the data (records) inside tables
CREATE, ALTER, DROP INSERT, UPDATE, DELETE, SELECT
e.g. create a table, add a new column e.g. add a student, change marks, show records

Analogy: Building a cupboard ๐Ÿ—„๏ธ vs using it:

  • DDL = the carpenter โ€” makes the cupboard, adds a shelf, or breaks it down (structure).
  • DML = you โ€” put clothes in, take them out, rearrange them (data).
๐Ÿง  Memory Trick ๐Ÿง 

DDL = "Design" โ†’ Create, Alter, Drop โ†’ "CAD" (like a design software!). DML = "Manage the data" โ†’ Insert, Update, Delete, Select โ†’ "IUDS" โ€” "I Use Data Sometimes".

๐Ÿ“‹ Typical question

"Differentiate between DDL and DML commands. Give two examples of each." โ€” 2 marks Answer: DDL (Data Definition Language) commands define or change the structure of database objects such as tables โ€” e.g. CREATE TABLE, ALTER TABLE. DML (Data Manipulation Language) commands work on the data stored in tables โ€” e.g. INSERT, UPDATE.


9.3 ๐Ÿ”ข MySQL Data Types

Every column must have a data type that tells MySQL what kind of values it can hold.

Data Type Stores Example Values
CHAR(n) Fixed-length text of exactly n characters 'M', 'A1'
VARCHAR(n) Variable-length text of up to n characters 'Riya', 'Kolkata'
INT Whole numbers 101, -5, 2026
FLOAT Numbers with a decimal point 92.5, 3.14
DECIMAL(p, s) Exact decimals โ€” p digits in all, s after the point DECIMAL(8, 2) โ†’ 12345.50
DATE A date in 'YYYY-MM-DD' format '2009-05-21'
๐Ÿ”‘ Values in quotes

Text and dates are written inside single quotes ('Riya', '2009-05-21'). Numbers are written without quotes (92, 85.5).

9.3.1 CHAR vs VARCHAR โš–๏ธ

Suppose a column is CHAR(10) and another is VARCHAR(10), and we store 'Riya' (4 characters) in both:

CHAR(10)     โ”‚ R โ”‚ i โ”‚ y โ”‚ a โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ† always uses 10 (padded with spaces)
VARCHAR(10)  โ”‚ R โ”‚ i โ”‚ y โ”‚ a โ”‚                           โ† uses only 4 (+ a tiny length marker)
CHAR(n) VARCHAR(n)
Fixed length โ€” always uses n characters of space Variable length โ€” uses only as much as needed
Extra space is filled with blanks No padding
Faster for values that are always the same size Saves space when values vary in size
Best for: gender 'M'/'F', PIN code, grade Best for: names, addresses, emails

Analogy: CHAR is like a fixed train seat ๐Ÿ’บ โ€” you get the whole seat even if you're small. VARCHAR is like a bench ๐Ÿช‘ โ€” each person takes only as much space as they need!

๐Ÿ“‹ Typical question

"Differentiate between CHAR and VARCHAR data types." โ€” 2 marks Answer: CHAR(n) stores fixed-length strings; it always occupies n characters, padding shorter values with spaces. VARCHAR(n) stores variable-length strings of up to n characters and occupies only as much space as the actual value needs. CHAR suits values of fixed size (like gender), VARCHAR suits values of varying size (like names).


9.4 ๐Ÿ›ก๏ธ Constraints

A constraint is a rule applied to a column so that only valid data can be stored in it. If someone tries to enter data that breaks the rule, MySQL rejects it with an error.

๐Ÿšซ

NOT NULL

The column can never be left empty

Must have a value
1๏ธโƒฃ

UNIQUE

No two rows can have the same value in this column (NULL is allowed)

No duplicates
๐Ÿ”‘

PRIMARY KEY

Identifies each row โ€” it is UNIQUE and NOT NULL together. Only one per table

Unique + Not Null
๐Ÿ“Œ

DEFAULT

Gives a value automatically when none is entered

Default value
Constraint Duplicates allowed? NULL allowed? How many per table?
NOT NULL Yes No Any number
UNIQUE No Yes Any number
PRIMARY KEY No No Only one

Analogy: Constraints are like the security guard at your school gate ๐Ÿ’‚ โ€” "No ID card? You can't come in!" (NOT NULL). "Two students with the same roll number? Not allowed!" (UNIQUE / PRIMARY KEY).

๐Ÿ“‹ Typical question

"What is the difference between a PRIMARY KEY and a UNIQUE constraint?" โ€” 2 marks Answer: Both prevent duplicate values. A PRIMARY KEY also does not allow NULL, and a table can have only one primary key. A UNIQUE column allows NULL values, and a table can have many UNIQUE columns.


9.5 ๐Ÿ—‚๏ธ Working with Databases

A MySQL server can hold many databases. First, we create one and select it.

CREATE DATABASE School;     -- make a new database
SHOW DATABASES;             -- list all databases
USE School;                 -- select it for work
Command Purpose
CREATE DATABASE name; Creates a new, empty database
SHOW DATABASES; Lists all databases on the server
USE name; Opens (selects) the database for use
DROP DATABASE name; Deletes the database and all its tables permanently
โš  Forgot USE? โ†’ "No database selected"

If you try to create a table without first running USE School;, MySQL gives ERROR 1046: No database selected.

๐Ÿง  Comments in SQL ๐Ÿง 

Anything after -- (two hyphens and a space) or # on a line is a comment โ€” MySQL ignores it, just like # in Python.


9.6 ๐Ÿ—๏ธ Creating a Table โ€” CREATE TABLE

CREATE TABLE table_name (
    column1  datatype  [constraint],
    column2  datatype  [constraint],
    ...
);

Let's create our STUDENT table:

CREATE TABLE Student (
    RollNo   INT PRIMARY KEY,
    Name     VARCHAR(30) NOT NULL,
    Gender   CHAR(1),
    Class    INT,
    Marks    FLOAT,
    DOB      DATE
);
   CREATE TABLE Student (
       RollNo   INT          PRIMARY KEY,
       โ”€โ”€โ”€โ”€โ”€โ”€   โ”€โ”€โ”€          โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
         โ”‚       โ”‚               โ””โ”€ constraint (unique + not null)
         โ”‚       โ””โ”€ data type (whole number)
         โ””โ”€ column name
       Name     VARCHAR(30)  NOT NULL,   โ† text up to 30 characters, must be filled
       ...
   );                                    โ† closing bracket + semicolon
๐Ÿ”‘ Rules for CREATE TABLE

  1. Columns are separated by commas โ€” but no comma after the last column.
  2. The whole column list is inside round brackets ( ).
  3. The command ends with a semicolon ;
  4. Table and column names can't have spaces (use Roll_No, not Roll No).

9.6.1 Viewing Tables and Their Structure ๐Ÿ”

SHOW TABLES;
+------------------+
| Tables_in_school |
+------------------+
| Student          |
+------------------+

DESCRIBE (or DESC) shows the structure of a table โ€” its columns, types and constraints:

DESCRIBE Student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| RollNo | int         | NO   | PRI | NULL    |       |
| Name   | varchar(30) | NO   |     | NULL    |       |
| Gender | char(1)     | YES  |     | NULL    |       |
| Class  | int         | YES  |     | NULL    |       |
| Marks  | float       | YES  |     | NULL    |       |
| DOB    | date        | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
๐Ÿง  Reading DESCRIBE ๐Ÿง 

Null = NO means the column can't be empty. Key = PRI marks the primary key. (Some MySQL versions show int(11) instead of int โ€” both mean the same.)

๐Ÿ“‹ Typical question

"Write a command to create a table ITEM with columns ItemCode (primary key, 4 characters), ItemName (up to 25 characters, must not be empty), Price (decimal) and Qty (integer)." โ€” 3 marks Answer:

CREATE TABLE Item (
    ItemCode  CHAR(4) PRIMARY KEY,
    ItemName  VARCHAR(25) NOT NULL,
    Price     FLOAT,
    Qty       INT
);

9.7 ๐Ÿ”ง Changing a Table โ€” ALTER TABLE

ALTER TABLE changes the structure of an existing table โ€” without losing its data.

graph TD
    AT["๐Ÿ”ง ALTER TABLE"]
    A1["โž• ADD\na new column"]
    A2["โœ๏ธ MODIFY\na column's type or size"]
    A3["โž– DROP\na column"]
    A4["๐Ÿ”‘ ADD / DROP\nPRIMARY KEY"]

    AT --> A1
    AT --> A2
    AT --> A3
    AT --> A4

    style A1 fill:#4CAF50,color:#fff
    style A2 fill:#FF9800,color:#fff
    style A3 fill:#F44336,color:#fff
    style A4 fill:#9C27B0,color:#fff

1. Add a new column:

ALTER TABLE Student ADD City VARCHAR(20);

2. Change the data type or size of a column:

ALTER TABLE Student MODIFY Name VARCHAR(40) NOT NULL;

3. Delete a column:

ALTER TABLE Student DROP City;

4. Remove and add a primary key:

ALTER TABLE Student DROP PRIMARY KEY;
ALTER TABLE Student ADD PRIMARY KEY (RollNo);
โš  MODIFY replaces the WHOLE column definition!

If Name was VARCHAR(30) NOT NULL and you write MODIFY Name VARCHAR(40); (without NOT NULL), the column loses its NOT NULL rule. Always repeat the constraints you want to keep.

โš  Can't add a primary key to a column with duplicates or NULLs

If two rows already have the same RollNo, ADD PRIMARY KEY (RollNo) fails with a duplicate entry error. Clean the data first!


9.8 ๐Ÿ—‘๏ธ Deleting a Table โ€” DROP TABLE

DROP TABLE Student;

This deletes the table completely โ€” its structure AND all its data. It cannot be undone! โš ๏ธ

โš  DROP is a DDL command โ€” no "Undo" button!

DROP TABLE removes the table itself. Later, you'll meet DELETE, a DML command that removes only rows and keeps the table. Don't mix them up!


9.9 โž• Adding Records โ€” INSERT INTO

INSERT INTO adds new rows (records) to a table. (Let's assume our Student table from 9.6 exists again.)

9.9.1 Inserting Values for All Columns

Values must be given in the same order as the columns in the table:

INSERT INTO Student VALUES (101, 'Riya', 'F', 11, 92.5, '2009-05-21');
INSERT INTO Student VALUES (102, 'Aman', 'M', 11, 85, '2009-08-14');

9.9.2 Inserting Values for Selected Columns

Name the columns you want to fill. Columns you leave out get NULL (or their DEFAULT value):

INSERT INTO Student (RollNo, Name, Class) VALUES (103, 'Zoya', 12);

9.9.3 Inserting Many Rows at Once

INSERT INTO Student VALUES
    (104, 'Kabir', 'M', 12, 78, '2008-11-02'),
    (105, 'Meera', 'F', 11, NULL, '2009-01-30');

Let's see all the records with SELECT * FROM Student; (you'll learn SELECT fully in the next chapter):

SELECT * FROM Student;
+--------+-------+--------+-------+-------+------------+
| RollNo | Name  | Gender | Class | Marks | DOB        |
+--------+-------+--------+-------+-------+------------+
|    101 | Riya  | F      |    11 |  92.5 | 2009-05-21 |
|    102 | Aman  | M      |    11 |    85 | 2009-08-14 |
|    103 | Zoya  | NULL   |    12 |  NULL | NULL       |
|    104 | Kabir | M      |    12 |    78 | 2008-11-02 |
|    105 | Meera | F      |    11 |  NULL | 2009-01-30 |
+--------+-------+--------+-------+-------+------------+
โš  Constraints in action โ€” these INSERTs FAIL!

INSERT INTO Student VALUES (101, 'Tina', 'F', 11, 70, '2009-02-02');
-- ERROR 1062: Duplicate entry '101' โ€” RollNo is the PRIMARY KEY

INSERT INTO Student (RollNo, Class) VALUES (106, 11);
-- ERROR 1364: Field 'Name' doesn't have a default value โ€” Name is NOT NULL
๐Ÿ”‘ Golden rules for INSERT

  1. Text and dates go in single quotes; numbers don't.
  2. Dates are written as 'YYYY-MM-DD'.
  3. Without a column list, give a value for every column, in order.
  4. Write NULL (no quotes!) for an unknown value. 'NULL' in quotes is just the text "NULL".
๐Ÿ“‹ Typical question

"Write a command to insert the following record into the table ITEM(ItemCode, ItemName, Price, Qty): I005, Pencil Box, 120, 50" โ€” 1 mark Answer:

INSERT INTO Item VALUES ('I005', 'Pencil Box', 120, 50);

๐Ÿงพ Commands at a Glance

Command Type Purpose
CREATE DATABASE db; DDL Make a database
SHOW DATABASES; โ€” List databases
USE db; โ€” Select a database
DROP DATABASE db; DDL Delete a database
CREATE TABLE t (...); DDL Make a table
SHOW TABLES; โ€” List tables in the current database
DESCRIBE t; / DESC t; โ€” Show the structure of a table
ALTER TABLE t ADD col type; DDL Add a column
ALTER TABLE t MODIFY col type; DDL Change a column's type or size
ALTER TABLE t DROP col; DDL Remove a column
ALTER TABLE t ADD PRIMARY KEY (col); DDL Set a primary key
DROP TABLE t; DDL Delete the table and all its data
INSERT INTO t VALUES (...); DML Add a row

โš ๏ธ Common Errors and Misconceptions

Mistake What's Wrong Correct Understanding
Forgetting the semicolon MySQL waits for more input End every command with ;
Comma after the last column in CREATE TABLE Syntax error No comma before the closing )
INSERT INTO Student VALUES (106, Tina, โ€ฆ) Text without quotes Write 'Tina'
Date written as '21-05-2009' Wrong format Use '2009-05-21' (YYYY-MM-DD)
'NULL' for an unknown value That's the text "NULL" Write NULL without quotes
"SELECT is a DDL command" It works on data SELECT is DML
"DROP TABLE deletes only the rows" It deletes everything DROP removes structure and data
MODIFY without repeating NOT NULL The rule is lost Repeat all constraints in MODIFY
"UNIQUE and PRIMARY KEY are the same" UNIQUE allows NULL and many per table PRIMARY KEY = unique + not null, only one
Creating a table before USE No database selected Run USE dbname; first

๐Ÿ”‘ Quick Revision โ€” Exam Ready!

  • SQL โ†’ Structured Query Language, used to work with relational databases; MySQL โ†’ free, open-source RDBMS
  • SQL is not case sensitive (for keywords); statements end with ;
  • DDL โ†’ structure: CREATE, ALTER, DROP
  • DML โ†’ data: INSERT, UPDATE, DELETE, SELECT
  • Data types โ†’ CHAR(n) fixed, VARCHAR(n) variable, INT, FLOAT, DECIMAL(p, s), DATE ('YYYY-MM-DD')
  • Constraints โ†’ NOT NULL, UNIQUE, PRIMARY KEY (unique + not null, one per table), DEFAULT
  • Database โ†’ CREATE DATABASE, SHOW DATABASES, USE, DROP DATABASE
  • Table โ†’ CREATE TABLE, SHOW TABLES, DESCRIBE, DROP TABLE
  • ALTER TABLE โ†’ ADD col, MODIFY col, DROP col, ADD PRIMARY KEY (col), DROP PRIMARY KEY
  • INSERT INTO โ†’ all columns in order, or chosen columns (rest become NULL); many rows with commas
  • Text and dates in single quotes; numbers and NULL without quotes

๐ŸŽฏ Sample Exam Questions

Q1: Very Short Answer [1 mark each]

a) What is the full form of SQL? Answer: Structured Query Language

b) Which command shows the structure of a table? Answer: DESCRIBE (or DESC)

c) Which constraint allows NULL values but no duplicates? Answer: UNIQUE

d) Is ALTER a DDL or a DML command? Answer: DDL

e) In which format is a date written in MySQL? Answer: 'YYYY-MM-DD'

f) Which command selects a database for use? Answer: USE

Q2: Short Answer [2 marks]

Write SQL commands to (a) create a database named Library and (b) open it for use.

Answer:

CREATE DATABASE Library;
USE Library;

Q3: Program [3 marks]

Write a command to create the table BOOK with the following structure:

Column Type Constraint
BookID CHAR(5) Primary key
Title VARCHAR(40) Must not be empty
Author VARCHAR(30)
Price FLOAT
PubDate DATE

Answer:

CREATE TABLE Book (
    BookID   CHAR(5) PRIMARY KEY,
    Title    VARCHAR(40) NOT NULL,
    Author   VARCHAR(30),
    Price    FLOAT,
    PubDate  DATE
);

Q4: Short Answer [3 marks]

For the table BOOK above, write commands to: (a) add a column Qty of integer type (b) increase the size of Author to 50 characters (c) insert the record: B0001, Wings of Fire, A P J Abdul Kalam, 350, 1999-01-01, 10

Answer:

ALTER TABLE Book ADD Qty INT;
ALTER TABLE Book MODIFY Author VARCHAR(50);
INSERT INTO Book VALUES ('B0001', 'Wings of Fire', 'A P J Abdul Kalam', 350, '1999-01-01', 10);

Q5: Error Finding [2 marks]

Find the errors in the following commands:

CREATE TABLE Emp (
    EmpID INT PRIMARY KEY,
    Name VARCHAR(20),
);
INSERT INTO Emp VALUES (1, Ravi);

Answer: (1) There must be no comma after the last column Name VARCHAR(20). (2) The text value must be in quotes: 'Ravi'.

CREATE TABLE Emp (
    EmpID INT PRIMARY KEY,
    Name VARCHAR(20)
);
INSERT INTO Emp VALUES (1, 'Ravi');

โœ๏ธ Practice Problems

  1. What is SQL? Why is MySQL called an RDBMS?
  2. Classify as DDL or DML: INSERT, DROP, SELECT, ALTER, UPDATE, CREATE, DELETE.
  3. Which data type would you use for: (a) PIN code (b) name of a city (c) date of admission (d) percentage (e) number of siblings?
  4. A column Grade is CHAR(2) and stores 'A'. How many characters of space does it use? What if it were VARCHAR(2)?
  5. Write a command to create a table TEACHER(TID, TName, Subject, Salary, DOJ) with TID as the primary key and TName not null.
  6. Write commands to add a column Phone VARCHAR(15) to TEACHER, and then remove it.
  7. Write commands to insert any three records into TEACHER.
  8. Write an INSERT command that fills only TID and TName of TEACHER. What will the other columns contain?
  9. What happens if you insert two rows with the same TID? Why?
  10. What is the difference between DROP TABLE Teacher; and DROP DATABASE School;?
  11. Write the command to see the structure of TEACHER. What does "PRI" in the Key column mean?
  12. Can a table have two UNIQUE columns? Two PRIMARY KEYs? Explain.
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

Take the Chapter 09 Quiz