### Understanding Databases Cheat Sheet
**Simplified**
---
**1. What is a Database?**
- Definition: A digital storage system that organizes and manages data efficiently.
- Example: A library catalog that stores book information.
**2. Types of Databases:**
- **Relational Databases** (SQL):
- Structure: Data organized into tables with rows and columns.
- Example: MySQL Table - `employees`
| EmployeeID | FirstName | LastName | Salary |
|------------|-----------|----------|--------|
| 101 | John | Smith | 50000 |
| 102 | Jane | Doe | 60000 |
- **NoSQL Databases**:
- Structure: Flexible, non-tabular data storage.
- Example: MongoDB Document - `customer`
```json
{
"_id": 1,
"name": "Alice",
"email": "alice@email.com"
}
```
**3. Schema:**
- Definition: Defines the structure of a database, including tables, fields, and relationships.
- Example: An e-commerce database schema with tables for `products`, `orders`, and `customers`.
**4. Tables:**
- Definition: Basic containers for data, composed of rows and columns.
- Example: `products` table
| ProductID | ProductName | Price |
|-----------|--------------|-------|
| 1 | Laptop | 800 |
| 2 | Smartphone | 500 |
**5. Primary Key:**
- Definition: A unique identifier for each record in a table.
- Example: `ProductID` in the `products` table.
**6. Foreign Key:**
- Definition: Establishes relationships between tables.
- Example: `CustomerID` in the `orders` table linking to `CustomerID` in the `customers` table.
**7. SQL (Structured Query Language):**
- Usage: Querying, updating, and managing relational databases.
- Example: SQL query to select all products with a price less than 600.
```sql
SELECT * FROM products WHERE Price < 600;
```
**8. Data Modeling:**
- Definition: The process of designing a database schema.
- Example: Designing a schema for a blog website with tables for `posts`, `users`, and `comments`.
**9. Data Cleaning:**
- Definition: Identifying and correcting errors or inconsistencies in data.
- Example: Removing duplicate records from a customer database.
**10. Querying Databases:**
- Usage: Retrieving and manipulating data using SQL queries.
- Example: SQL query to find the total sales for each product.
```sql
SELECT ProductName, SUM(Quantity) AS TotalSales
FROM products
JOIN order_details ON products.ProductID = order_details.ProductID
GROUP BY ProductName;
```
**11. Scaling Databases:**
- Methods: Horizontal Scaling (adding more servers), Vertical Scaling (upgrading server resources).
- Example: Scaling a web application by adding more web servers.
**12. Data Validation:**
- Purpose: Ensures data accuracy by validating and sanitizing inputs.
- Example: Validating user input in a registration form to prevent SQL injection.
**13. Backup and Recovery:**
- Importance: Regular backups protect against data loss.
- Example: Scheduled database backups to a remote server.
**14. Indexes:**
- Purpose: Enhance query performance by enabling fast data retrieval.
- Example: Creating an index on the `ProductID` column of the `products` table.
**15. Transactions:**
- Definition: Groups database operations into atomic, consistent, isolated, and durable units.
- Example: A banking application ensuring a transfer of funds is completed or rolled back entirely.
**16. Data Security:**
- Measures: Encryption, authentication, access controls.
- Example: Implementing user roles and permissions to restrict access to sensitive data.
**17. Data Warehouse:**
- Usage: Stores historical data for business intelligence and reporting.
- Example: A data warehouse used for analyzing sales trends over the past five years.
**18. Distributed Databases:**
- Purpose: Distributes data across multiple locations or servers.
- Example: Multi-datacenter architecture for a global e-commerce platform.
**19. Database Management Systems (DBMS):**
- Definition: Software systems for database creation, management, and interaction.
- Example: Using MySQL as a DBMS to manage an e-commerce database.
**20. Continuous Learning:**
- Importance: The field of databases is continually evolving.
- Example: Staying updated with new database technologies and best practices.
<hr>
<center>
Follow me on X: <a href="https://twitter.com/DataEmperor" target="_blank">
@DataEmperor
</a>
|| Made with ❣️ by
<a href="https://angelsantana.io" target="_blank">
Angel Santana
</a>
</center>