|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "# Database normalization \n", |
| 7 | + "# Database Normalization \n", |
| 8 | + "\n", |
| 9 | + "# Normalization Principle\n", |
| 10 | + "\n", |
| 11 | + "A fundamental principle in database design is **normalization**—the practice of organizing data to minimize redundancy and dependency. One key aspect of normalization is that **each table should represent one distinct entity class**.\n", |
| 12 | + "\n", |
| 13 | + "## Why Separate Entity Types?\n", |
| 14 | + "\n", |
| 15 | + "Different entity types have different:\n", |
| 16 | + "- **Identification systems**: How they are uniquely identified\n", |
| 17 | + "- **Attributes**: What properties they have\n", |
| 18 | + "- **Relationships**: How they connect to other entities\n", |
| 19 | + "- **Business rules**: What constraints apply to them\n", |
| 20 | + "\n", |
| 21 | + "## Example: Pet Shop Database\n", |
| 22 | + "\n", |
| 23 | + "Consider designing a database for a pet shop. You might be tempted to put everything in one table:\n", |
| 24 | + "\n", |
| 25 | + "```sql\n", |
| 26 | + "-- BAD DESIGN: Mixing different entity types\n", |
| 27 | + "CREATE TABLE pet_shop_data (\n", |
| 28 | + " id INT PRIMARY KEY,\n", |
| 29 | + " name VARCHAR(50),\n", |
| 30 | + " type VARCHAR(20), -- 'cat', 'dog', 'employee', 'customer'\n", |
| 31 | + " breed VARCHAR(30),\n", |
| 32 | + " salary DECIMAL(10,2),\n", |
| 33 | + " phone VARCHAR(20),\n", |
| 34 | + " address TEXT\n", |
| 35 | + ");\n", |
| 36 | + "```\n", |
| 37 | + "\n", |
| 38 | + "This design violates the normalization principle because it mixes:\n", |
| 39 | + "- **Pets** (cats, dogs) with attributes like breed\n", |
| 40 | + "- **Employees** with attributes like salary\n", |
| 41 | + "- **Customers** with attributes like phone and address\n", |
| 42 | + "\n", |
| 43 | + "## Better Design: Separate Tables\n", |
| 44 | + "\n", |
| 45 | + "```sql\n", |
| 46 | + "-- GOOD DESIGN: Separate entity types\n", |
| 47 | + "CREATE TABLE pet (\n", |
| 48 | + " pet_id INT PRIMARY KEY,\n", |
| 49 | + " name VARCHAR(50) NOT NULL,\n", |
| 50 | + " species ENUM('cat', 'dog', 'bird', 'fish') NOT NULL,\n", |
| 51 | + " breed VARCHAR(30),\n", |
| 52 | + " birth_date DATE,\n", |
| 53 | + " owner_id INT\n", |
| 54 | + ");\n", |
| 55 | + "\n", |
| 56 | + "CREATE TABLE employee (\n", |
| 57 | + " employee_id INT PRIMARY KEY,\n", |
| 58 | + " name VARCHAR(50) NOT NULL,\n", |
| 59 | + " position VARCHAR(30) NOT NULL,\n", |
| 60 | + " salary DECIMAL(10,2) NOT NULL,\n", |
| 61 | + " hire_date DATE NOT NULL\n", |
| 62 | + ");\n", |
| 63 | + "\n", |
| 64 | + "CREATE TABLE customer (\n", |
| 65 | + " customer_id INT PRIMARY KEY,\n", |
| 66 | + " name VARCHAR(50) NOT NULL,\n", |
| 67 | + " phone VARCHAR(20),\n", |
| 68 | + " email VARCHAR(100),\n", |
| 69 | + " address TEXT\n", |
| 70 | + ");\n", |
| 71 | + "```\n", |
| 72 | + "\n", |
| 73 | + "Each table now represents a distinct entity class with appropriate attributes and identification systems.\n", |
8 | 74 | "\n", |
9 | 75 | "**Database normalization** is a set of principles for designing databases with clarity and logical rigor. \n", |
10 | 76 | "Normalized designs communicate the mapping between real-world entities and their representations in database design. \n", |
|
0 commit comments