ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

A database is an organised collection of related data that is stored and managed electronically so that it can be easily accessed, updated, and retrieved. In the modern world, every website, bank, school, and hospital uses a database to store its records. MySQL is one of the most popular open-source relational database management systems (RDBMS) in the world. It uses Structured Query Language (SQL) to perform operations on the data, and it is widely used with web technologies such as PHP, Python, and Java to build dynamic websites.

A Relational Database Management System stores data in the form of tables. A table is a collection of rows and columns, where each column represents a field or attribute and each row represents a record. For example, a school database may have a STUDENT table with columns such as ROLL_NO, NAME, CLASS, and MARKS. Relationships can be established between tables using keys, which is what makes the system relational. MySQL supports these relational features along with data integrity and security.

In this chapter we will learn about database concepts such as tables, rows, columns, and keys, and then study the practical SQL commands used to create, manage, and query data. We will cover DDL commands such as CREATE and ALTER, DML commands such as INSERT, UPDATE, and DELETE, and the most important query command SELECT. Students will also learn constraints like PRIMARY KEY and FOREIGN KEY, and how to use operators and clauses in queries.

2. Database Terminology

Understanding the basic terminology is essential before learning SQL commands.

For example, in a STUDENT table, ROLL_NO can serve as the primary key because no two students have the same roll number. This terminology is frequently tested in examinations.

3. Introduction to SQL and Data Types

SQL is the standard language for defining and manipulating data in a relational database. SQL commands are divided into categories based on their function.

MySQL supports many data types for table columns, including:

4. Creating and Modifying Tables (DDL)

The CREATE TABLE command is used to create a new table. The syntax is:

CREATE TABLE student (
    roll_no INT PRIMARY KEY,
    name VARCHAR(30),
    class VARCHAR(10),
    marks INT
);

The PRIMARY KEY constraint ensures that the roll_no column uniquely identifies each row and cannot contain NULL. Once a table is created, its structure can be changed with ALTER TABLE. The ALTER TABLE command can add a new column, modify an existing column, or drop a column.

ALTER TABLE student ADD phone VARCHAR(12);
ALTER TABLE student MODIFY name VARCHAR(40);

The DROP TABLE command removes a table completely along with its data. These commands define the structure, so great care must be taken before using DROP.

5. Inserting and Updating Data (DML)

The INSERT INTO command adds new records to a table. Values must be written in the same order as the columns.

INSERT INTO student VALUES (1, 'Riya', 'X', 92);
INSERT INTO student (roll_no, name) VALUES (2, 'Arjun');

The UPDATE command modifies the data of existing records. The SET clause specifies which columns to change, and the WHERE clause specifies which records to update. Without a WHERE clause, all records would be updated, which is dangerous.

UPDATE student SET marks = 95 WHERE roll_no = 2;

The DELETE command removes records from the table. Like UPDATE, it should be used with a WHERE clause to target specific records.

DELETE FROM student WHERE roll_no = 3;

6. Retrieving Data with SELECT

The SELECT command is the most important and most frequently used SQL command. It retrieves data from the database. The basic syntax is:

SELECT column1, column2 FROM table_name;

The WHERE clause can use comparison operators (=, >, <, >=, <=, !=) and logical operators (AND, OR, NOT). The LIKE operator is used for pattern matching, for example WHERE name LIKE 'A%' finds names beginning with the letter A.

7. SQL Constraints

Constraints are rules applied to table columns to ensure the accuracy and integrity of the data.

These constraints protect the database from invalid data and are an important topic in both theory and practical exams.

8. Advantages of MySQL and Databases

MySQL in particular is free, open-source, fast, reliable, and compatible with all major operating systems, which explains its enormous popularity.

Quick Revision Tables

Command Category Purpose
CREATE TABLE DDL Creates a new table
ALTER TABLE DDL Changes the structure of a table
DROP TABLE DDL Deletes a table completely
INSERT INTO DML Adds new records
UPDATE DML Modifies existing records
DELETE DML Removes records
SELECT DQL Retrieves data
Constraint Purpose
NOT NULL Column cannot be empty
UNIQUE No duplicate values allowed
PRIMARY KEY Uniquely identifies each record
FOREIGN KEY Links to primary key of another table
CHECK Enforces a condition on values
DEFAULT Provides a default value

Mind Map

graph TD A["MySQL Database"] --> B["Terminology"] A --> C["SQL Categories"] A --> D["DDL Commands"] A --> E["DML Commands"] A --> F["SELECT Query"] A --> G["Constraints"] B --> B1["Table, Record, Field"] B --> B2["Primary Key, Foreign Key"] C --> C1["DDL, DML, DQL, DCL"] D --> D1["CREATE, ALTER, DROP"] E --> E1["INSERT, UPDATE, DELETE"] F --> F1["WHERE, ORDER BY, LIKE"] F --> F2["COUNT, MAX, MIN, AVG"]

Important Diagrams (SVG)

Diagram 1: Relational Database Structure

STUDENT Table (Parent) Columns: ROLL_NO (PK), NAME, CLASS ROLL_NO | NAME | CLASS 1 | Riya | X 2 | Arjun | X 3 | Sita | IX MARKS Table (Child) Columns: ROLL_NO (FK), SUBJECT, MARKS ROLL_NO | SUBJECT | MARKS 1 | Maths | 92 2 | Maths | 88 3 | Science | 78 1 : M One student many records Golden Rule: A foreign key in one table refers to the primary key of another table.

Diagram 2: SQL Command Categories

SQL Commands DDL CREATE, ALTER, DROP DML INSERT, UPDATE, DELETE DQL SELECT DCL GRANT, REVOKE DDL defines structure, DML manipulates data, DQL retrieves data, DCL controls access rights. Golden Rule: DDL defines the structure; DML manipulates the data; DQL retrieves it.

Common Mistakes

  1. Running UPDATE or DELETE without a WHERE clause, which modifies or removes all records in the table.
  2. Forgetting to enclose text values in single quotes in INSERT and WHERE clauses, such as writing WHERE name = Riya instead of WHERE name = 'Riya'.
  3. Confusing DROP with DELETE. DROP removes the entire table, while DELETE removes records but keeps the table.
  4. Giving duplicate or NULL values to a primary key column, which is not allowed.
  5. Confusing the order of WHERE and ORDER BY clauses. ORDER BY must come after WHERE.
  6. Using VARCHAR without a size, or using the wrong data type for a column.
  7. Writing UPDATE without the SET clause, which makes the command invalid.

Exam Tips

  1. Learn the full forms: SQL (Structured Query Language), DDL, DML, DQL, and DCL.
  2. Practise writing a CREATE TABLE statement with a primary key and at least two data types.
  3. Memorise that INSERT adds, UPDATE modifies, and DELETE removes data, with WHERE used for targeting.
  4. Be able to write SELECT queries with WHERE, ORDER BY, and aggregate functions like COUNT and MAX.
  5. Remember that a primary key is NOT NULL and UNIQUE, and a foreign key links two tables.
  6. Practise practical commands on a MySQL console or online playground before the exam.

Conclusion

Databases are the storage engines behind all modern applications, and MySQL is one of the most widely used database systems in the world. In this chapter we learned the fundamental terminology of relational databases, including tables, records, fields, and keys. We studied the SQL command categories and practised the DDL commands CREATE, ALTER, and DROP, the DML commands INSERT, UPDATE, and DELETE, and the DQL command SELECT with its powerful clauses and aggregate functions. Constraints such as PRIMARY KEY, FOREIGN KEY, NOT NULL, and UNIQUE protect the integrity of the data. With these skills, students can connect a database to a web application and build real, dynamic systems.