Catalog / Database Systems Cheatsheet
Database Systems Cheatsheet
A comprehensive cheat sheet covering essential concepts in database systems, including data modeling, SQL, normalization, transactions, and indexing.
Data Modeling
Entity-Relationship (ER) Model
Entity: A real-world object distinguishable from other objects. Example: Customer, Product, Order |
Attribute: A property describing an entity. Example: Customer ID, Product Name, Order Date |
Relationship: An association among entities. Example: Customer places Order, Product is part of Order |
Cardinality: Specifies the number of instances of one entity that can be related to another entity. Types: One-to-one (1:1), One-to-many (1:N), Many-to-one (N:1), Many-to-many (N:M) |
Primary Key: A unique identifier for an entity. Example: Customer ID in Customer entity |
Foreign Key: An attribute in one entity that refers to the primary key of another entity, establishing a link between them. Example: Customer ID in Order entity referencing Customer entity |
Enhanced Entity-Relationship (EER) Model
Specialization: |
Creating subtypes (child entities) from a supertype (parent entity). |
Generalization: |
Creating a supertype from subtypes. |
Aggregation: |
Treating a relationship as an entity. |
Inheritance: |
Subtypes inherit attributes and relationships from their supertype. |
UML Class Diagrams
Class: Represents a set of objects with common attributes and behavior. Example: |
Association: Represents a relationship between classes. Example: |
Multiplicity: Specifies the cardinality of the association. Example: One |
Aggregation/Composition: Represents a part-whole relationship. Example: |
SQL Fundamentals
Basic Queries
|
Retrieves data from a database.
|
|
Filters the results based on a condition.
|
|
Sorts the results.
|
|
Limits the number of rows returned.
|
|
Retrieves unique values.
|
Joins
|
Returns rows when there is a match in both tables.
|
|
Returns all rows from the left table, and the matched rows from the right table. If there is no match, the result is NULL on the right side.
|
|
Returns all rows from the right table, and the matched rows from the left table. If there is no match, the result is NULL on the left side.
|
|
Returns all rows when there is a match in one of the tables.
|
Aggregate Functions
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Normalization
Normal Forms
1NF (First Normal Form): |
2NF (Second Normal Form): |
3NF (Third Normal Form): |
BCNF (Boyce-Codd Normal Form): |
4NF (Fourth Normal Form): |
5NF (Fifth Normal Form): |
Example of Normalization
Consider a table Unnormalized: |
|
1NF: |
|
2NF: |
Tables: |
Transactions and Indexing
Transaction Properties (ACID)
Atomicity: All operations in a transaction must be treated as a single “unit”. Either all operations succeed, or none do. Example: Transferring money from one account to another involves debiting one account and crediting another. Both must succeed or fail together. |
Consistency: A transaction must maintain the integrity of the database. Moving from one valid state to another. Example: A transaction should not violate any defined constraints (e.g., primary key, foreign key). |
Isolation: Transactions should be isolated from each other. Concurrent execution should have the same result as if transactions were executed serially. Example: Two transactions updating the same data should not interfere with each other. |
Durability: Once a transaction is committed, the changes are permanent and will survive system failures. Example: After a successful money transfer, the changes should not be lost even if the system crashes immediately afterward. |
Transaction Management
|
Begins a new transaction.
|
|
Saves the changes made during the transaction.
|
|
Undoes the changes made during the transaction.
|
|
Creates a point within a transaction to which you can rollback.
|
|
Removes a previously defined savepoint.
|
Indexing
Purpose: |
Types:
|
Creating an Index:
|
Example:
|
Considerations: |