Introduction
When designing a database, we often start with a picture showing how things relate to each other. But computers need a clear table format to store data. Converting these pictures into tables is the key step to make the design usable.
Imagine organizing a library. Books are entities, each with details like title and author. Relationships show which books belong to which categories. To keep track, you create shelves (tables) for books and categories, and labels (foreign keys) to link them.
┌─────────────┐ ┌─────────────┐ ┌───────────────┐
│ Entity A │ │ Entity B │ │ Associative │
│ (Table A) │ │ (Table B) │ │ Table (for │
│ PK: A_id │ │ PK: B_id │ │ many-to-many) │
└─────┬───────┘ └─────┬───────┘ │ FK: A_id │
│ │ │ FK: B_id │
│ One-to-many │ └───────────────┘
│ relationship │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Table B │ │ Table A │
│ FK: A_id │◄─────┤ PK: A_id │
└─────────────┘ └─────────────┘CREATE TABLE Department ( DeptID INT PRIMARY KEY, DeptName VARCHAR(100) ); CREATE TABLE Employee ( EmpID INT PRIMARY KEY, EmpName VARCHAR(100), DeptID INT, FOREIGN KEY (DeptID) REFERENCES Department(DeptID) ); CREATE TABLE Project ( ProjectID INT PRIMARY KEY, ProjectName VARCHAR(100) ); CREATE TABLE EmployeeProject ( EmpID INT, ProjectID INT, PRIMARY KEY (EmpID, ProjectID), FOREIGN KEY (EmpID) REFERENCES Employee(EmpID), FOREIGN KEY (ProjectID) REFERENCES Project(ProjectID) );