Database Concepts and MySQL
CBSE Class 11 - Information Practices
Welcome to the world of SQL (Structured Query Language) - the language that powers databases! Follow along as we explore how to store, manage, and retrieve data efficiently using SQL.
Chapter 1: The Foundation - Basic Terms
What is a File?
A file is a container used to store data on a computer. Files can have different formats:
CSV
Comma-Separated Values - plain text format for tabular data
Binary
Stores data in binary form for images, executables
Text
Plain text data readable by text editors
Key Database Terms
1. SQL (Structured Query Language)
Language used for managing and manipulating relational databases. Allows users to create, update, retrieve, and delete data.
2. RDBMS (Relational Database Management System)
Database management system based on the relational model. Organizes data into tables (relations) with rows and columns, supporting relationships between tables.
3. Database
Organized collection of structured information stored in tables (relations). Each table consists of rows and columns for efficient data storage and retrieval.
4. Relation
A two-dimensional structure represented by rows and columns. Each relation is essentially a table.
5. DBMS (Database Management System)
Software that manages and interacts with databases. Examples: MySQL, Oracle, Microsoft Access, PostgreSQL, Sybase.
6. ER Diagram (Entity-Relationship Diagram)
Graphical representation of entities and their relationships in a database, used for database design.
Chapter 2: Why Databases? - Limitations of File System
File systems have several limitations that databases solve. Let's understand why databases are better:
1. Difficulty in Access
File System:
Accessing data requires creating application programs. Complex and time-consuming.
Database:
Efficient access using SQL query language. No need for additional application programs.
2. Data Redundancy
File System:
Same data duplicated in multiple files (e.g., student names in both "Student" and "Attendance" files).
Database:
Minimizes redundancy through normalization. Data stored once and accessed as needed.
3. Data Inconsistency
File System:
Same data in multiple locations may not be updated uniformly, leading to inconsistencies.
Database:
Maintains consistency through data integrity rules. Changes reflected throughout the database.
4. Data Isolation
File System:
Related data spread across multiple files, difficult to combine and analyze.
Database:
Structured format allows easy cross-referencing and relational queries.
5. Data Dependence
File System:
Changing file structure requires updating all application programs.
Database:
Data independence - structure can be modified without impacting applications.
6. Controlled Data Sharing
File System:
Complex to implement access control. Difficult to restrict users from modifying data.
Database:
Built-in security mechanisms. Can grant specific permissions to different user roles.
Chapter 3: Database Rules - Constraints
1. Primary Key
Unique identifier for each row in a table.
- Must be unique
- Cannot be NULL
- Only one primary key per table
2. Foreign Key
Attribute in one table that links to another table's primary key. Enforces referential integrity.
3. Unique Key
Enforces uniqueness for a column's values but allows NULL values (unlike primary key).
4. NOT NULL
Ensures a column cannot have NULL values.
5. DEFAULT
Assigns a default value to a column if no value is specified during insertion.
6. CHECK
Ensures values in a column satisfy a specific condition (e.g., Age >= 18).
Difference: Primary Key vs Unique Key
Primary Key
- ✓ Enforces uniqueness
- ✓ Does NOT allow NULL
- ✓ Only one per table
Unique Key
- ✓ Enforces uniqueness
- ✓ Allows NULL values
- ✓ Multiple allowed per table
Degree and Cardinality
Degree: The number of columns (attributes) in a table.
Cardinality: The number of rows (tuples) in a table.
Example: Students table with columns ID, Name, Age and 5 rows:
• Degree = 3 (columns: ID, Name, Age)
• Cardinality = 5 (rows)
Chapter 4: Data Types in SQL
SQL Data Types Overview
1. Numeric Data Types
2. Date/Time Data Types
3. Character/String Data Types
CHAR vs VARCHAR
CHAR(n)
- • Fixed-length string (1-255 characters)
- • Example: CHAR(20) stores "Kunal" as 20 characters with blank spaces
- • Wastage of space if actual string is shorter
VARCHAR(n)
- • Variable-length string up to maximum
- • Example: VARCHAR(20) stores "Kunal" as only 5 characters
- • No wastage of space
Max Size of Literals
- Text Literal: TINYTEXT → 255 | TEXT → 65,535 | MEDIUMTEXT → 16,777,215
- Numeric Literal: DECIMAL (65,30)
Chapter 5: SQL Commands
1. DDL (Data Definition Language)
Commands that define and manage the structure of database objects.
CREATE
Example: Creating Instagram Users
ALTER
Example: Adding Bio
DROP
2. DML (Data Manipulation Language)
Commands used to manipulate data within database tables.
INSERT
Example: User Signup (Aarav)
UPDATE
Example: Updating Followers (Diya)
DELETE
Example: Removing Spam (Vihaan)
4. DQL (Data Query Language)
Commands used to query data within database tables.
SELECT
Example: Finding Influencers
Chapter 6: SELECT Commands - Querying Data
Aggregate Functions
COUNT()
Counts non-null values
AVG()
Calculates average
SUM()
Returns sum of values
MIN() / MAX()
Finds minimum/maximum
COUNT(column) vs COUNT(*)
COUNT(column_name)
Counts only non-NULL values in the specified column.
Returns 3 if 4 rows exist but 1 has NULL email
COUNT(*)
Counts all rows in the table, including NULL values.
Returns 4 (total rows regardless of NULLs)
WHERE Clause Examples
LIKE with Wildcards
BETWEEN
Includes both 1000 and 5000 (inclusive)
IN / NOT IN
ORDER BY
DISTINCT
Chapter 8: GROUP BY and HAVING
GROUP BY
Groups rows that have the same values in specified columns. Used with aggregate functions.
Groups employees by department and calculates total salary for each department.
HAVING Clause
Filters groups after GROUP BY. Similar to WHERE but works on grouped data.
Shows only departments with total salary greater than 5000.
Query Order
Key Takeaways
- SQL Commands: DDL (structure), DML (data), DQL (query)
- Constraints: Primary Key (unique, not null), Foreign Key (relationships), Unique, NOT NULL, DEFAULT, CHECK
- Data Types: CHAR (fixed) vs VARCHAR (variable), Numeric, Date/Time types
- SELECT: WHERE, LIKE, BETWEEN, IN, ORDER BY, GROUP BY, HAVING