A database is an organised collection of data stored and accessed electronically. While files store raw data with no structure, a database management system (DBMS) adds facilities to store, retrieve, update, and protect data in a systematic way. MySQL is one of the most popular open-source relational database management systems (RDBMS) in the world, and it forms the SQL half of the Class 12 Informatic Practices syllabus. This chapter is a revision tour that consolidates everything learnt in Class 11 about MySQL, serving as the base for the querying and function chapters that follow.
An RDBMS stores data in tables, where a table is a collection of rows and columns. Each row, called a tuple or record, describes one entity, and each column, called an attribute or field, holds one property of that entity. Tables can be related to each other through shared columns, which is the "relational" part of the name. MySQL communicates with the user through SQL (Structured Query Language), a standard language whose statements are divided into categories such as DDL, DML, and DCL.
This revision chapter covers the complete command set needed to build and manage tables. We will start with the distinction between DBMS and RDBMS, then look at the MySQL data types, and finally work through the full set of SQL commands: CREATE, INSERT, SELECT, UPDATE, DELETE, ALTER, and DROP, together with the constraints that enforce data integrity.
A DBMS stores data as files and has no relationship between the files. An RDBMS stores data in tables and allows the tables to be related through common fields. Because relationships reduce redundancy and enforce consistency, RDBMS systems such as MySQL, Oracle, and PostgreSQL dominate industry use.
Key characteristics of an RDBMS:
Every column of a table must have a declared data type. The most commonly used types in the syllabus are:
CHAR(10).VARCHAR(30).DECIMAL(6, 2).YYYY-MM-DD.CREATE TABLE student (
roll INT,
name VARCHAR(30),
class CHAR(3),
fees DECIMAL(6, 2),
dob DATE
);
Commands that define or modify the structure of database objects. Examples: CREATE, ALTER, DROP.
Commands that work with the data inside tables. Examples: INSERT, UPDATE, DELETE, SELECT.
Commands that control access rights, such as GRANT and REVOKE.
CREATE DATABASE school;
USE school;
SHOW DATABASES;
DROP DATABASE school;
CREATE DATABASE creates a new database.USE selects the database for subsequent commands.SHOW DATABASES lists all databases on the server.DROP DATABASE removes a database completely.CREATE TABLE student (
roll INT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
marks DECIMAL(5, 2),
city VARCHAR(20) DEFAULT 'Delhi'
);
After creation, the structure can be inspected:
DESCRIBE student;
DESC student;
SHOW TABLES;
DESCRIBE (or its short form DESC) lists each column, its data type, nullability, key status, and default value. SHOW TABLES lists all tables in the current database.
Constraints enforce rules on the data and protect its integrity.
CREATE TABLE fee (
roll INT,
amount DECIMAL(6, 2),
FOREIGN KEY (roll) REFERENCES student(roll)
);
INSERT INTO student (roll, name, marks) VALUES (101, 'Ravi', 88.5);
INSERT INTO student VALUES (102, 'Simran', 92.0, 'Mumbai');
INSERT INTO student (roll, name, marks, city) VALUES (103, 'Amit', 78.0, 'Kolkata');
UPDATE student SET marks = 95 WHERE roll = 101;
UPDATE student SET marks = marks + 5 WHERE city = 'Mumbai';
UPDATE changes existing rows. The SET clause assigns new values and the WHERE clause decides which rows are affected. If WHERE is omitted, all rows are updated.
DELETE FROM student WHERE roll = 103;
DELETE FROM student;
DELETE removes rows matching the WHERE condition. Without a WHERE clause it removes all rows but keeps the table structure.
ALTER TABLE modifies an existing table:
ALTER TABLE student ADD COLUMN phone VARCHAR(10);
ALTER TABLE student DROP COLUMN phone;
ALTER TABLE student MODIFY name VARCHAR(40);
ALTER TABLE student RENAME COLUMN marks TO score;
ALTER TABLE student RENAME TO student_new;
ADD COLUMN inserts a new column.DROP COLUMN removes a column.MODIFY changes the data type or constraints of a column.RENAME COLUMN changes the name of a column.RENAME TO renames the whole table.DROP TABLE student;
DROP TABLE deletes the table together with all of its data and structure. Unlike DELETE, it cannot be undone and the table must be recreated.
The SELECT command retrieves data from one or more tables:
SELECT * FROM student;
SELECT roll, name FROM student;
SELECT DISTINCT city FROM student;
* selects all columns.DISTINCT removes duplicate values from the result.WHERE filters rows by a condition.ORDER BY sorts the result.LIMIT restricts the number of rows shown.SELECT name, marks FROM student WHERE marks > 80 ORDER BY marks DESC;
| Command | Category | Purpose |
|---|---|---|
| CREATE | DDL | Create database or table |
| ALTER | DDL | Change table structure |
| DROP | DDL | Remove table or database |
| INSERT | DML | Add rows to a table |
| UPDATE | DML | Modify existing rows |
| DELETE | DML | Remove rows |
| SELECT | DML | Retrieve data |
| GRANT | DCL | Give access rights |
| Data Type | Use |
|---|---|
| INT | Whole numbers |
| CHAR(n) | Fixed-length string |
| VARCHAR(n) | Variable-length string |
| DECIMAL(p, s) | Exact decimal numbers |
| DATE | Date in YYYY-MM-DD format |
| FLOAT | Approximate decimal numbers |
USE database; after creating the database, so commands give an error because no database is selected.VARCHAR without a length like VARCHAR(name), which is invalid syntax; it must be VARCHAR(30).WHERE clause in an UPDATE or DELETE, which unintentionally affects all rows of the table.INSERT with a column list that does not match the number of values, producing a column count mismatch error.DELETE FROM with DROP TABLE; DELETE keeps the structure while DROP removes everything.ALTER TABLE ADD COLUMN without the word COLUMN in older MySQL versions, giving a syntax error.date or order as a column name without backticks.DESC table and DESCRIBE table are identical commands.DELETE (removes rows) and DROP (removes the table structure as well).MySQL Revision Tour consolidates the complete command vocabulary of the relational database system. Data is stored in related tables whose columns carry declared data types and constraints that enforce integrity. DDL commands like CREATE, ALTER, and DROP manage the structure of databases and tables, while DML commands such as INSERT, UPDATE, DELETE, and SELECT manage the data inside them. The primary key identifies rows uniquely and the foreign key builds relationships between tables. A clear grasp of this command set is the necessary foundation for the two following chapters, which add advanced querying techniques such as filtering, grouping, ordering, joins, and the rich library of SQL functions.