A database is an organized collection of structured information, or data, typically stored electronically in a computer system. Databases are used to store, retrieve, modify, and delete data.
debora-rodrigues / databases
databases
A comprehensive cheat sheet covering database concepts, ER modeling, table creation, data population, and querying using SQL. From understanding databases to crafting advanced queries, this guide provides a quick reference for database design and manipulation.
Database Fundamentals & ER Modeling
What is a Database?
|
Databases enable efficient data management, ensuring data integrity, consistency, and security. They are crucial for applications ranging from simple contact lists to complex inventory management systems. |
ER Model Elements
Entity: A real-world object or concept (e.g., Customer, Product, Order). |
Attribute: A characteristic of an entity (e.g., Customer Name, Product Price, Order Date). |
Relationship: An association between entities (e.g., a Customer places an Order). |
Cardinality: Defines the numerical relationship between entities (e.g., one-to-many, many-to-many). |
Cardinalities
One-to-One (1:1): Each entity in one set is associated with at most one entity in the other set. Example: One person has one passport. |
One-to-Many (1:N): One entity in one set is associated with multiple entities in the other set. Example: One customer can place many orders. |
Many-to-Many (M:N): Multiple entities in one set are associated with multiple entities in the other set. Example: Many students can enroll in many courses. |
ER Diagrams to Tables & Normalization
ER to Table Conversion
Entities become tables. Attributes become columns. |
Primary keys are chosen to uniquely identify each row in a table. Foreign keys are used to represent relationships between tables. |
For 1:1 relationships, the primary key of one table can be added as a foreign key to the other table. |
For 1:N relationships, the primary key of the ‘one’ side is added as a foreign key to the ‘many’ side. |
For M:N relationships, a new table (junction table) is created with foreign keys referencing the primary keys of both tables. |
Normalization
Normalization is the process of organizing data to reduce redundancy and improve data integrity. It involves dividing databases into tables and defining relationships between the tables. |
Common normalization forms include 1NF, 2NF, and 3NF. Each form addresses specific types of redundancy. |
By ensuring each table represents a single entity and minimizing redundant data, normalization helps prevent update anomalies and ensures data consistency. |
Creating Tables in SQL
CREATE TABLE Syntax
|
Common Data Types
INT: Integer numbers. |
VARCHAR(size): Variable-length string (max size). |
DATE: Date values (YYYY-MM-DD). |
BOOLEAN: True/False values. |
DECIMAL(precision, scale): Exact number with precision digits, scale digits after the decimal point. |
TIMESTAMP: Date and time values. |
Constraints
PRIMARY KEY: Unique identifier for the table. |
NOT NULL: Column cannot contain NULL values. |
FOREIGN KEY: Establishes a relationship with another table. |
UNIQUE: Ensures all values in a column are unique. |
CHECK: Specifies a condition that must be true for values in a column. |
DEFAULT: Sets a default value for a column if none is specified. |
Data Manipulation & Querying
INSERT Statements
|
|
SELECT Statements
|
|
WHERE Clause
Comparison Operators: |
Logical Operators: |
BETWEEN: |
LIKE: |
IN: |
IS NULL / IS NOT NULL: Check for NULL values. |
Joins
Cartesian Product: Combines each row of the first table with each row of the second table.
|
NATURAL JOIN: Joins tables based on columns with the same name and data type.
|
INNER JOIN: Returns rows only when there is a match in both tables.
|