1. Basic DBMS Concepts
What is a DBMS?
A Database Management System (DBMS) is software that enables users to create, read, update, and delete data in a database efficiently. It acts as an interface between the database and end-users or application programs.
Key Functions of DBMS:
- Data Definition: Define database structure (CREATE, ALTER, DROP)
- Data Manipulation: Insert, update, delete, retrieve data (CRUD operations)
- Data Security: User authentication, authorization, access control
- Data Integrity: Maintain accuracy and consistency through constraints
- Concurrency Control: Handle simultaneous data access by multiple users
- Backup & Recovery: Protect against data loss and system failures
- Data Independence: Separate physical storage from logical view
Popular DBMS: MySQL, PostgreSQL, Oracle, MongoDB, SQL Server, SQLite, Cassandra, Redis
SQL vs NoSQL: When to use each?
📊 SQL (Relational)
- Structure: Tables with fixed schema (rows & columns)
- Query Language: Structured Query Language (SQL)
- Scalability: Vertical (scale up - add more CPU/RAM)
- ACID: Strong consistency, transactions
- Relationships: Foreign keys, JOINs
- Best for: Complex queries, financial data, e-commerce
- Examples: MySQL, PostgreSQL, Oracle, MS SQL
🚀 NoSQL (Non-Relational)
- Structure: Flexible schema (JSON, key-value, graph)
- Query Language: Database-specific APIs
- Scalability: Horizontal (scale out - add more servers)
- BASE: Eventual consistency, high availability
- Relationships: Embedded documents, no JOINs
- Best for: Big data, real-time apps, IoT, social media
- Examples: MongoDB, Cassandra, Redis, DynamoDB
Decision Guide:
Choose SQL when:
- You need complex joins and relationships between tables
- Data integrity and ACID transactions are critical (banking, payments)
- Your schema is well-defined and stable
- You need strong consistency
Choose NoSQL when:
- You need to scale horizontally across multiple servers
- Your schema is flexible or evolves frequently
- You prioritize speed and availability over strict consistency
- You're handling huge volumes of unstructured data
Primary Key, Foreign Key, Candidate Key, Super Key
1. Primary Key 🔑
Uniquely identifies each record in a table. Cannot be NULL. Only ONE per table.
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
2. Foreign Key 🔗
Links two tables together. References the PRIMARY KEY of another table. Ensures referential integrity.
CREATE TABLE enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id)
);
Benefit: If you try to delete a student who has enrollments, the database will prevent it (maintaining data integrity).
3. Candidate Key 🎯
Any column (or combination) that can uniquely identify a record. There can be multiple candidate keys. The Primary Key is chosen from candidate keys.
Example: In a Students table:
student_id→ Candidate Key (unique)email→ Candidate Key (unique)phone→ Candidate Key (if unique)
We choose student_id as PRIMARY KEY from these candidates.
4. Super Key 🔐
Any combination of columns that uniquely identifies a record. A Candidate Key is a minimal Super Key (no unnecessary columns).
Example Super Keys:
- {student_id} → Minimal ✅
- {email} → Minimal ✅
- {student_id, name} → Has redundancy ❌
- {student_id, name, email} → Has redundancy ❌
Schema vs Instance in a Database
Schema (Blueprint) 📐
The logical design/structure of the database. Defines table names, column names, data types, constraints. Rarely changes.
CREATE TABLE users (
id INT,
name VARCHAR(50),
age INT
);
Instance (Data) 📊
The actual data stored at a particular moment. Changes frequently as records are inserted/updated/deleted.
(1, 'Alice', 25)
(2, 'Bob', 30)
(3, 'Charlie', 28)
Perfect Analogy:
Schema = House Blueprint (structure, layout)
Instance = Furniture Inside (actual content, changes often)
What are Constraints?
Constraints are rules applied to table columns to ensure data accuracy, consistency, and integrity. They prevent invalid data from being entered.
CREATE TABLE employees (
id INT PRIMARY KEY, -- NOT NULL + UNIQUE
name VARCHAR(100) NOT NULL, -- Must have value
email VARCHAR(100) UNIQUE, -- No duplicates
age INT CHECK (age >= 18), -- Must be 18+
salary DECIMAL DEFAULT 50000, -- Default if not provided
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(id)
);
Types of Constraints:
- NOT NULL: Column cannot have NULL value
- UNIQUE: All values in column must be different
- PRIMARY KEY: NOT NULL + UNIQUE combined (one per table)
- FOREIGN KEY: Links tables, ensures referential integrity
- CHECK: Ensures values meet a specific condition
- DEFAULT: Sets default value if none is provided