Notes on Relational Databases:
Purpose of Relational Databases:
- Used to store and organize information for quick and easy retrieval.
- Example: eCommerce systems store details about customers, products, and orders.
Key Concepts:
- Entities: Represent real-world objects or things (e.g., customers, products, orders).
- Tables: Used to model entities; each table contains rows and columns.
- Rows: Represent individual instances of an entity (e.g., one customer or one order).
- Columns: Define properties of the entity (e.g., customer name, product ID).
Naming Best Practices:
- Use one word for names (avoid spaces).
- Use conventions like camelCase, PascalCase, or underscores for naming entities and fields (e.g.,
customerNameorcustomer_id).
Primary Keys (PK):
- Uniquely identify each row in a table.
- No two rows can have the same primary key.
Foreign Keys (FK):
- Used to link tables and maintain relationships between them.
- Reference the primary key of another table.
- Help prevent anomalies, like invalid orders for non-existent customers.
Relationships Between Tables:
- One-to-Many: One customer can have many orders, but each order belongs to a single customer.
- Many-to-One: Several orders can be for the same product, but each order references a single product.
Characteristics of Relational Databases:
- Data is stored in tables.
- All rows in a table have the same columns.
- Tables can have any number of rows.
- Primary keys ensure rows are unique.
- Foreign keys link related tables.
Normalization:
- The process of splitting entities into multiple tables to avoid duplication and improve efficiency.
Example in eCommerce:
- Customers Table: Contains customer details (e.g., name, address).
- Products Table: Contains product details (e.g., name, price).
- Orders Table: Links customers and products using foreign keys (
customerIDandproductID).
Normalization and proper relationships ensure the database remains organized and scalable.
Comments
Post a Comment