Introduction to DBMS and MySQL Notes Class 12 β This page covers the complete CBSE Class 12 Computer Science (Code 083) database unit β from basic database concepts and terminology to SQL commands, constraints, aggregate functions, and practical MySQL queries. All topics are explained clearly with definitions, examples, and board-exam-style questions so you can score maximum marks.
π‘
Exam tip: Database questions appear every year in the CBSE Class 12 board paper β usually 8β10 marks worth of SQL queries, definitions, and output-based questions. Focus on: DDL vs DML difference, SQL constraints, SELECT with WHERE/ORDER BY/GROUP BY, and aggregate functions. Practise every query in the
online MySQL terminal.
Quick Overview β What You Will Learn
| # | Topic | Key Concepts | Weight |
|---|
| 1 | What is a Database? | Data, Information, Database, DBMS, Advantages | ββ |
| 2 | Key Database Terminology | Table, Record, Field, Tuple, Attribute, Domain | ββ |
| 3 | Types of Keys | Primary Key, Candidate Key, Alternate Key, Foreign Key | βββ |
| 4 | DBMS vs RDBMS | Differences, Examples, Codd’s Rules | ββ |
| 5 | Introduction to MySQL | Features, Data Types, CHAR vs VARCHAR | ββ |
| 6 | SQL Command Categories | DDL, DML, DCL, TCL β with examples | βββ |
| 7 | SQL Constraints | NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, FOREIGN KEY, CHECK | βββ |
| 8 | CREATE TABLE & INSERT | Table creation, inserting rows | βββ |
| 9 | SELECT Command | *, columns, WHERE, DISTINCT, ORDER BY, BETWEEN, LIKE, IN | βββ |
| 10 | Aggregate Functions | COUNT(), SUM(), AVG(), MAX(), MIN() | βββ |
| 11 | GROUP BY & HAVING | Grouping records, filtering groups | βββ |
| 12 | UPDATE & DELETE | Modifying and removing rows | ββ |
| 13 | ALTER TABLE | ADD, DROP, MODIFY column, constraints | βββ |
Section 1Theory ββ
ποΈ What is a Database?
A database is an organised, structured collection of related data that can be easily stored, accessed, managed, and updated. Unlike a random collection of data, a database organises information in a way that makes retrieval quick and reliable. A Database Management System (DBMS) is the software that manages the database and provides tools to create, read, update, and delete data.
Important Definitions
- Data: Raw, unprocessed facts and figures. Example: 95, “Arjun”, “Delhi”.
- Information: Processed, meaningful data. Example: “Arjun scored 95 marks and lives in Delhi.”
- Database: An organised collection of related data stored and accessed electronically.
- DBMS: Software that creates, manages, and controls access to a database. Example: MySQL, Oracle, MS Access.
- RDBMS: A DBMS that organises data into related tables and supports SQL. Example: MySQL, PostgreSQL, Oracle.
Advantages of a Database over Traditional File System
- Reduced data redundancy β Same data is not stored multiple times unnecessarily.
- Data consistency β Updating data in one place reflects everywhere automatically.
- Data sharing β Multiple users and applications can access the same data simultaneously.
- Data security β Access can be restricted using user roles and passwords.
- Data integrity β Constraints ensure only valid, accurate data is stored.
- Easy backup and recovery β Databases support automated backup and restore features.
- Data independence β Changes to data structure do not affect applications using the data.
DataInformationDatabaseDBMSRDBMSData Redundancy
Section 2Theory ββ
π Key Database Terminology
Before writing SQL queries, you must understand the key terms used to describe the structure of a relational database. These definitions are frequently asked as 1-mark or 2-mark questions in CBSE board exams.
| Term | Definition | Example |
|---|
| Table (Relation) | A collection of related data organised in rows and columns. The basic unit of storage in an RDBMS. | STUDENT table |
| Record (Row / Tuple) | A single, complete entry in a table. One row represents one entity. | Details of one student |
| Field (Column / Attribute) | A single piece of information about an entity. One column in a table. | RollNo, Name, Marks |
| Domain | The set of all valid values that an attribute can hold. | Marks: 0 to 100 |
| Degree | The total number of columns (attributes / fields) in a table. | STUDENT has 5 columns β Degree = 5 |
| Cardinality | The total number of rows (records / tuples) in a table. | STUDENT has 30 rows β Cardinality = 30 |
π‘ Remember for Exams:- Row = Record = Tuple β all three mean the same thing.
- Column = Field = Attribute β all three mean the same thing.
- Degree = number of columns. Cardinality = number of rows.
TableRecord / TupleField / AttributeDomainDegreeCardinality
Section 3Theory βββ
π Types of Keys in RDBMS
A key is a field (or combination of fields) that uniquely identifies a record in a table. Keys are fundamental to the relational model β they establish relationships between tables and ensure data integrity. This is a very commonly asked 2β3 mark definition + example topic in CBSE board exams.
| Key Type | Definition | Example |
|---|
| Primary Key | A field (or combination) that uniquely identifies each record in a table. Cannot be NULL or duplicate. | RollNo in STUDENT table |
| Candidate Key | Any field (or combination) that is capable of being a primary key β unique and not null. A table can have multiple candidate keys. | RollNo, AdmissionNo (both can uniquely identify a student) |
| Alternate Key | All candidate keys that are NOT chosen as the primary key. They are “alternatives” to the primary key. | If RollNo is Primary Key, then AdmissionNo is Alternate Key |
| Foreign Key | A field in one table that references the Primary Key of another table. Used to establish relationships between tables and enforce referential integrity. | RollNo in MARKS table referencing RollNo in STUDENT table |
| Composite Key | A primary key made up of two or more fields together β no single field can uniquely identify a record alone. | (StudentID + SubjectCode) together as primary key in RESULT table |
π‘ Key Relationships to Remember:- Every Primary Key is a Candidate Key, but not every Candidate Key is a Primary Key.
- Alternate Key = Candidate Key β Primary Key.
- A Foreign Key in one table must match an existing Primary Key value in the referenced table.
- A table can have only one Primary Key but multiple Candidate Keys and Alternate Keys.
Primary KeyCandidate KeyAlternate KeyForeign KeyComposite Key
Section 4Theory ββ
βοΈ DBMS vs RDBMS
Both DBMS and RDBMS are database management systems, but they differ in how data is stored, related, and protected. This comparison is a standard 2-mark question in CBSE board exams.
| Feature | DBMS | RDBMS |
|---|
| Data Storage | Stores data as files (hierarchical or network model) | Stores data in related tables (rows and columns) |
| Data Relationships | No formal relationship between data | Tables are related using primary and foreign keys |
| Data Redundancy | High β same data may be repeated across files | Low β normalisation reduces redundancy |
| Security | Limited security features | Strong security with user roles and access control |
| SQL Support | Does not necessarily support SQL | Fully supports SQL (Structured Query Language) |
| Data Integrity | No built-in integrity constraints | Enforces integrity through keys and constraints |
| Examples | MS Access (flat file), XML databases | MySQL, Oracle, PostgreSQL, MS SQL Server |
DBMSRDBMSTablesSQLNormalisationReferential Integrity
Section 5Theory + Practical ββ
ποΈ Introduction to MySQL β Data Types
MySQL is an open-source Relational Database Management System (RDBMS) that uses SQL to manage data. It stores data in tables and is one of the most widely used databases in the world. Every column in a MySQL table must have a data type that defines what kind of data it can store.
| Data Type | Description | Example |
|---|
| INT | Whole numbers. Range: β2,147,483,648 to 2,147,483,647. Occupies 4 bytes. | RollNo INT |
| FLOAT | Decimal numbers with approximate precision. Occupies 4 bytes. | Marks FLOAT |
| DECIMAL(M,D) | Exact decimal numbers. M = total digits, D = digits after decimal point. | Salary DECIMAL(8,2) |
| CHAR(M) | Fixed-length string. Always stores exactly M characters (pads with spaces). Max 255. | Gender CHAR(1) |
| VARCHAR(M) | Variable-length string. Stores only actual characters entered. Max 65,535. M is compulsory. | Name VARCHAR(30) |
| DATE | Date in YYYY-MM-DD format. Range: 1000-01-01 to 9999-12-31. | DOB DATE |
CHAR vs VARCHAR β Most Asked Comparison
| Feature | CHAR(M) | VARCHAR(M) |
|---|
| Type | Fixed-length string | Variable-length string |
| Storage | Always stores exactly M bytes (pads with spaces if shorter) | Stores only actual characters entered β no padding |
| Max Length | 255 characters | 65,535 characters |
| M parameter | Optional (default is 1) | Compulsory β must be specified |
| Best For | Fixed-size data: Gender (M/F), Grade (A/B/C) | Variable-size data: Name, Email, Address |
Memory trick: CHAR(10) storing “Hi” uses 10 bytes (pads with 8 spaces). VARCHAR(10) storing “Hi” uses only 2 bytes (no padding). VARCHAR is more storage-efficient for variable-length data.
INTFLOATDECIMALCHARVARCHARDATE
Section 6Theory βββ
π SQL Command Categories
SQL commands are grouped into categories based on what they do. CBSE Class 12 expects you to know three main categories β DDL, DML, and TCL/DCL β with examples. “Give the full form of DDL and list two DDL commands” is a standard 2-mark board question.
DDL β Data Definition Language
DDL commands work on the structure (schema) of the database β creating, modifying, and deleting tables and databases.
| Command | Purpose | Example |
|---|
| CREATE DATABASE | Creates a new database | CREATE DATABASE School; |
| CREATE TABLE | Creates a new table | CREATE TABLE Student (…); |
| ALTER TABLE | Modifies an existing table structure | ALTER TABLE Student ADD Grade CHAR(1); |
| DROP TABLE | Permanently deletes a table and all its data | DROP TABLE Student; |
DML β Data Manipulation Language
DML commands work on the data (rows) inside tables β inserting, reading, updating, and deleting records.
| Command | Purpose | Example |
|---|
| SELECT | Retrieves data from a table | SELECT * FROM Student; |
| INSERT INTO | Adds new rows into a table | INSERT INTO Student VALUES (…); |
| UPDATE | Modifies existing rows in a table | UPDATE Student SET Marks=90 WHERE RollNo=101; |
| DELETE | Removes rows from a table | DELETE FROM Student WHERE RollNo=101; |
TCL / DCL β Transaction and Data Control Language
| Command | Type | Purpose |
|---|
| COMMIT | TCL | Permanently saves all changes made in the current transaction |
| ROLLBACK | TCL | Undoes all changes made in the current transaction |
| GRANT | DCL | Gives a user permission to perform specific database operations |
| REVOKE | DCL | Takes back permissions previously given to a user |
π‘ Key Distinction β Exam Trick:- DDL works on table structure (schema) β CREATE, ALTER, DROP.
- DML works on table data (rows) β SELECT, INSERT, UPDATE, DELETE.
- SELECT is a DML command, not DDL β very common trick question in exams.
- DROP TABLE β DELETE β DROP removes the table itself; DELETE removes rows from the table.
DDLDMLTCLDCLCREATEALTERSELECTINSERT
Section 7Theory + Practical βββ
π SQL Constraints
Constraints are rules applied to table columns to restrict the type of data that can be stored. They maintain the accuracy, integrity, and reliability of data. If a constraint is violated, the database rejects the operation with an error. Constraints are defined either during CREATE TABLE or added later using ALTER TABLE.
| Constraint | What It Does | Example |
|---|
| NOT NULL | Column cannot store NULL values β a value must always be provided. | Name VARCHAR(30) NOT NULL |
| UNIQUE | All values in the column must be different. No two rows can have the same value. | Email VARCHAR(50) UNIQUE |
| DEFAULT | Assigns a default value if no value is provided during INSERT. | City VARCHAR(20) DEFAULT ‘Delhi’ |
| PRIMARY KEY | Uniquely identifies each row. Combines NOT NULL + UNIQUE. Only one per table. | RollNo INT PRIMARY KEY |
| FOREIGN KEY | Links a column to the PRIMARY KEY of another table. Enforces referential integrity. | FOREIGN KEY(RollNo) REFERENCES Student(RollNo) |
| CHECK | Ensures column values satisfy a specific condition or range. | Marks FLOAT CHECK(Marks >= 0 AND Marks <= 100) |
NOT NULLUNIQUEDEFAULTPRIMARY KEYFOREIGN KEYCHECK
Section 8Practical βββ
π CREATE TABLE and INSERT INTO
CREATE TABLE
SYNTAX
CREATE TABLE <TableName> (
<Column1> <DataType> [Constraint],
<Column2> <DataType> [Constraint],
…
);EXAMPLE
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Class CHAR(3),
Gender VARCHAR(10) DEFAULT ‘Male’,
DOB DATE,
Marks FLOAT CHECK(Marks >= 0 AND Marks <= 100)
);INSERT INTO
SYNTAX
INSERT INTO <TableName> (<Col1>, <Col2>, …) VALUES (<Val1>, <Val2>, …);EXAMPLE
INSERT INTO STUDENT (RollNo, Name, Class, Gender, DOB, Marks)
VALUES (101, ‘Arjun Sharma’, ’12A’, ‘Male’, ‘2006-04-15’, 87.5);INSERT INTO STUDENT (RollNo, Name, Class, DOB, Marks)
VALUES (102, ‘Priya Sharma’, ’12B’, ‘2006-08-22’, 92.0);
— Gender uses DEFAULT value ‘Male’ since not specifiedCREATE TABLEINSERT INTOVALUESDEFAULT
Section 9Practical βββ
π SELECT Command β Retrieving Data
The SELECT command retrieves data from one or more tables. It is the most commonly tested SQL command in CBSE board exams β questions include writing queries, predicting output, and identifying errors. Mastering SELECT is essential for scoring well.
Display All Columns and All Rows
EXAMPLE
SELECT * FROM STUDENT;Display Selected Columns
EXAMPLE
SELECT Name, Marks FROM STUDENT;WHERE β Filter Rows by Condition
EXAMPLE
SELECT * FROM STUDENT WHERE Marks > 80;
SELECT Name, Marks FROM STUDENT WHERE Class = ’12A’;
SELECT * FROM STUDENT WHERE Gender = ‘Female’ AND Marks >= 90;BETWEEN β Range Condition
EXAMPLE
SELECT * FROM STUDENT WHERE Marks BETWEEN 70 AND 90;
— Includes both 70 and 90 (inclusive)LIKE β Pattern Matching
EXAMPLE
SELECT * FROM STUDENT WHERE Name LIKE ‘A%’; — Names starting with A
SELECT * FROM STUDENT WHERE Name LIKE ‘%Kumar’; — Names ending with Kumar
SELECT * FROM STUDENT WHERE Name LIKE ‘_r%’; — Names with ‘r’ as second characterIN β Match from a List
EXAMPLE
SELECT * FROM STUDENT WHERE Class IN (’12A’, ’12B’);
— Same as: WHERE Class=’12A’ OR Class=’12B’DISTINCT β Remove Duplicate Values
EXAMPLE
SELECT DISTINCT Class FROM STUDENT; — Each class name shown only onceORDER BY β Sort Results
EXAMPLE
SELECT Name, Marks FROM STUDENT ORDER BY Marks; — Ascending (default)
SELECT Name, Marks FROM STUDENT ORDER BY Marks DESC; — Descending
SELECT * FROM STUDENT ORDER BY Class ASC, Marks DESC; — Sort by Class ASC, then Marks DESCSELECT *WHEREBETWEENLIKEINDISTINCTORDER BY
Section 10Practical βββ
π Aggregate Functions
Aggregate functions perform calculations on a set of values and return a single result. They are used with SELECT to summarise data β for example, finding the total marks, average score, or number of students. These functions ignore NULL values (except COUNT(*)).
| Function | What it Does | Example |
|---|
| COUNT() | Counts the number of rows. COUNT(*) counts all rows including NULLs. COUNT(column) ignores NULLs. | SELECT COUNT(*) FROM STUDENT; |
| SUM() | Returns the total sum of a numeric column. | SELECT SUM(Marks) FROM STUDENT; |
| AVG() | Returns the average value of a numeric column. | SELECT AVG(Marks) FROM STUDENT; |
| MAX() | Returns the highest value in a column. | SELECT MAX(Marks) FROM STUDENT; |
| MIN() | Returns the lowest value in a column. | SELECT MIN(Marks) FROM STUDENT; |
EXAMPLES
SELECT COUNT(*) FROM STUDENT; — Total number of students
SELECT COUNT(*) FROM STUDENT WHERE Class = ’12A’; — Students in class 12A
SELECT SUM(Marks) FROM STUDENT; — Total of all marks
SELECT AVG(Marks) FROM STUDENT; — Average marks
SELECT MAX(Marks), MIN(Marks) FROM STUDENT; — Highest and lowest marks
SELECT MAX(Marks) FROM STUDENT WHERE Class = ’12B’; — Highest marks in class 12BCOUNT()SUM()AVG()MAX()MIN()
Section 11Practical βββ
π¦ GROUP BY and HAVING
GROUP BY groups rows that have the same value in a specified column and applies aggregate functions to each group separately. HAVING filters groups after grouping β it is the WHERE clause for groups. Remember: WHERE filters individual rows (before grouping), HAVING filters groups (after grouping).
GROUP BY
EXAMPLE
— Count students in each class
SELECT Class, COUNT(*) FROM STUDENT GROUP BY Class;— Average marks per class
SELECT Class, AVG(Marks) FROM STUDENT GROUP BY Class;— Highest marks per class
SELECT Class, MAX(Marks) FROM STUDENT GROUP BY Class;HAVING β Filter Groups
EXAMPLE
— Show only classes with more than 10 students
SELECT Class, COUNT(*) FROM STUDENT
GROUP BY Class HAVING COUNT(*) > 10;— Classes where average marks exceed 75
SELECT Class, AVG(Marks) FROM STUDENT
GROUP BY Class HAVING AVG(Marks) > 75;
π‘ WHERE vs HAVING β Most Asked Difference:- WHERE filters individual rows before grouping. Cannot use aggregate functions in WHERE.
- HAVING filters groups after GROUP BY. Can use aggregate functions in HAVING.
- Example:
WHERE Marks > 60 filters rows. HAVING AVG(Marks) > 60 filters groups.
GROUP BYHAVINGWHERE vs HAVINGAggregate + GROUP BY
Section 12Practical ββ
βοΈ UPDATE and DELETE
UPDATE β Modify Existing Rows
SYNTAX
UPDATE <TableName> SET <Column> = <Value> [WHERE <Condition>];EXAMPLES
UPDATE STUDENT SET Marks = 95 WHERE RollNo = 101;
UPDATE STUDENT SET Class = ’12C’, Marks = 88 WHERE RollNo = 102;
β οΈ Always use WHERE with UPDATE. UPDATE STUDENT SET Marks = 95 without WHERE will update every single row in the table.
DELETE β Remove Rows
SYNTAX
DELETE FROM <TableName> [WHERE <Condition>];EXAMPLES
DELETE FROM STUDENT WHERE RollNo = 101;
DELETE FROM STUDENT WHERE Marks < 33; — Delete all failed students
β οΈ Always use WHERE with DELETE. DELETE FROM STUDENT without WHERE permanently deletes every row from the table.
UPDATESETDELETE FROMWHERE clause
Section 13Practical βββ
π§ ALTER TABLE
The ALTER TABLE command modifies the structure of an existing table β without deleting and recreating it. You can add columns, drop columns, change data types, and add or remove constraints. This is a DDL command and is one of the most frequently asked commands in CBSE board exams β usually as a “write the SQL command to…” question.
Add a New Column
EXAMPLE
ALTER TABLE STUDENT ADD Grade CHAR(1);Remove a Column
EXAMPLE
ALTER TABLE STUDENT DROP DOB;Modify Data Type of a Column
EXAMPLE
ALTER TABLE STUDENT MODIFY Name VARCHAR(50);Add PRIMARY KEY
EXAMPLE
ALTER TABLE STUDENT ADD PRIMARY KEY(RollNo);Drop PRIMARY KEY
EXAMPLE
ALTER TABLE STUDENT DROP PRIMARY KEY;Add FOREIGN KEY
EXAMPLE
ALTER TABLE MARKS ADD FOREIGN KEY(RollNo) REFERENCES STUDENT(RollNo);Add UNIQUE Constraint
EXAMPLE
ALTER TABLE STUDENT ADD UNIQUE(Email);Add DEFAULT Constraint
EXAMPLE
ALTER TABLE STUDENT MODIFY Gender VARCHAR(10) DEFAULT ‘Male’;ADD columnDROP columnMODIFYADD PRIMARY KEYADD FOREIGN KEYADD UNIQUEADD DEFAULT
Frequently Asked Questions
What is the difference between DDL and DML commands?
DDL (Data Definition Language) commands work on the structure (schema) of the database β they create, modify, or delete tables. Examples: CREATE, ALTER, DROP. DML (Data Manipulation Language) commands work on the data (rows) inside tables. Examples: SELECT, INSERT, UPDATE, DELETE. Important trick: SELECT is DML, not DDL.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregate functions. Example: WHERE Marks > 60 filters rows before any grouping; HAVING AVG(Marks) > 60 filters groups after GROUP BY.
What is the difference between PRIMARY KEY and UNIQUE constraint?
Both ensure all values in a column are distinct. However, PRIMARY KEY also enforces NOT NULL β it cannot have NULL values. A table can have only one PRIMARY KEY but multiple UNIQUE constraints. The primary key is the main row identifier; UNIQUE is applied to other columns that must not have duplicates (like email or phone number).
What is the difference between DROP TABLE and DELETE?
DROP TABLE is a DDL command that permanently deletes the entire table β both its structure and all its data. The table no longer exists after DROP. DELETE is a DML command that removes specific rows from a table while keeping the table structure intact. You can filter which rows to delete using WHERE.
What is a Foreign Key and why is it used?
A Foreign Key is a column in one table that references the Primary Key of another table. It enforces referential integrity β you cannot insert a value in the foreign key column that does not exist in the referenced table. This prevents orphan records and keeps data consistent across related tables.
What is the difference between Candidate Key and Alternate Key?
A Candidate Key is any field (or combination) that can uniquely identify each row in a table β it is unique and not null. A table can have multiple candidate keys. The one chosen as the main identifier becomes the Primary Key. All remaining candidate keys that were NOT chosen as the primary key are called Alternate Keys. In short: Alternate Key = Candidate Key β Primary Key.
What is the use of the LIKE operator in SQL?
The LIKE operator is used for pattern matching in string columns with two wildcards β % (matches any sequence of characters, including zero characters) and _ (matches exactly one character). Examples: LIKE 'A%' matches names starting with A; LIKE '%Kumar' matches names ending with Kumar; LIKE '_r%' matches names where the second character is ‘r’.
Quick Practice Links
Explore More Resources for Class 12 Computer Science