SQL & Database Design
Every application stores data. SQL is the universal language for talking to relational databases. Understanding SQL deeply is what separates junior developers from strong hires.
| SQL Category | Commands | Purpose |
|---|---|---|
| DDL โ Data Definition | CREATE, ALTER, DROP | Define and modify table structure |
| DML โ Data Manipulation | INSERT, UPDATE, DELETE | Add and modify data |
| DQL โ Data Query | SELECT | Read and retrieve data |
| DCL โ Data Control | GRANT, REVOKE | Manage permissions |
| TCL โ Transaction Control | COMMIT, ROLLBACK | Manage transactions |
| Constraint | Purpose |
|---|---|
PRIMARY KEY | Unique + NOT NULL โ main row identifier |
FOREIGN KEY / REFERENCES | Link to another table's primary key |
NOT NULL | Value is required |
UNIQUE | No duplicate values allowed |
CHECK | Value must satisfy a condition |
DEFAULT | Use this value if none provided |
LEFT JOIN = all rows from the left table, plus matching rows from the right. Like listing all students and their course (if enrolled).
| JOIN Type | Returns |
|---|---|
| INNER JOIN | Rows matching in BOTH tables |
| LEFT JOIN | All left rows + matching right rows (NULL if no match) |
| RIGHT JOIN | All right rows + matching left rows |
| FULL OUTER JOIN | All rows from both tables |
ACID Properties
| Property | Meaning |
|---|---|
| Atomicity | All operations succeed or all fail โ no partial state |
| Consistency | DB is in a valid state before and after the transaction |
| Isolation | Concurrent transactions don't interfere with each other |
| Durability | Committed transactions survive system failures |
@Transactional โ Spring automatically wraps the method in a database transaction and commits or rolls back based on success/exception.Normalization is the process of organizing database tables to reduce data redundancy and improve data integrity.
| Normal Form | Rule | Problem it solves |
|---|---|---|
| 1NF | Each column has atomic values, no repeating groups | Storing multiple phone numbers in one cell |
| 2NF | 1NF + no partial dependency on composite key | Redundant data depending on only part of key |
| 3NF | 2NF + no transitive dependencies | Column depends on another non-key column |
INNER JOIN returns only rows where there is a match in both tables. If a student has no enrollment, they won't appear in the result.
LEFT JOIN returns all rows from the left table, plus matching rows from the right. If a student has no enrollment, they still appear with NULL for the course columns.
Use INNER JOIN when you only want records that have related data. Use LEFT JOIN when you want all records from the primary table regardless of related data.
WHERE filters individual rows before any grouping occurs. You cannot use aggregate functions (COUNT, SUM) in WHERE.
HAVING filters groups after GROUP BY is applied. You can use aggregate functions here.
Example: WHERE age > 18 filters students before grouping. HAVING COUNT(*) > 5 filters city groups that have more than 5 students.
Primary Key โ A column (or set of columns) that uniquely identifies each row in a table. Must be unique and NOT NULL. A table can have only one primary key.
Foreign Key โ A column that references the primary key of another table. It enforces referential integrity โ you can't insert an enrollment with a student_id that doesn't exist in the students table. ON DELETE CASCADE means child rows are deleted when the parent is deleted.
An index is a data structure that improves the speed of data retrieval at the cost of additional storage and slower writes.
Use indexes on columns that are: frequently in WHERE clauses, used in JOIN conditions, used in ORDER BY/GROUP BY, foreign key columns.
Don't index columns with low cardinality (e.g., a boolean column), small tables, or columns that are rarely queried.
Atomicity โ Transaction is all-or-nothing. Either all statements succeed, or none take effect. A failure in the middle rolls back all changes.
Consistency โ A transaction brings the database from one valid state to another. All constraints, rules, and cascades are respected.
Isolation โ Concurrent transactions are isolated from each other. The intermediate state of a transaction is not visible to other transactions.
Durability โ Once committed, a transaction's changes persist even if the system crashes (written to disk/WAL logs).
Normalization is the process of organizing a database to reduce data redundancy (storing the same data in multiple places) and ensure data integrity.
Without normalization, updating a student's city in one place but not another causes inconsistency. Normalized design stores each piece of data in exactly one place, referenced everywhere it's needed.
3NF is the standard target for most applications.