# Entity Relationship Diagram (ERD) — StockHub Ltd SMS

## Entities, Attributes, Keys

### 1. Product
| Attribute       | Type        | Key        |
|-----------------|-------------|------------|
| productCode     | VARCHAR(20) | **PK**     |
| productName     | VARCHAR(100)|            |
| category        | VARCHAR(50) |            |
| quantityInStock | INT         |            |
| unitPrice       | DECIMAL     |            |
| supplierName    | VARCHAR(100)|            |
| dateReceived    | DATE        |            |

### 2. Warehouse
| Attribute         | Type        | Key    |
|-------------------|-------------|--------|
| warehouseCode     | VARCHAR(20) | **PK** |
| warehouseName     | VARCHAR(100)|        |
| warehouseLocation   | VARCHAR(150)|        |

### 3. StockTransaction
| Attribute       | Type        | Key    |
|-----------------|-------------|--------|
| transactionId   | INT         | **PK** (AUTO_INCREMENT) |
| productCode     | VARCHAR(20) | **FK** → Product(productCode) |
| warehouseCode   | VARCHAR(20) | **FK** → Warehouse(warehouseCode) |
| transactionDate | DATE        |        |
| quantityMoved   | INT         |        |
| transactionType | ENUM('IN','OUT') |   |

### 4. User (Authentication)
| Attribute | Type        | Key    |
|-----------|-------------|--------|
| userId    | INT         | **PK** |
| username  | VARCHAR(50) | UNIQUE |
| password  | VARCHAR(255)|        |

## Relationships & Cardinalities

```
Product (1) ────────< (M) StockTransaction
   One product can appear in many stock transactions.

Warehouse (1) ──────< (M) StockTransaction
   One warehouse can have many stock transactions.
```

| Relationship                    | Cardinality | Description                                      |
|---------------------------------|-------------|--------------------------------------------------|
| Product — StockTransaction      | **1 : M**   | Each transaction references one product          |
| Warehouse — StockTransaction    | **1 : M**   | Each transaction occurs at one warehouse         |

## ERD Diagram (draw.io / Lucidchart reference)

```
┌─────────────────────┐         ┌──────────────────────────┐         ┌─────────────────────┐
│      PRODUCT        │         │    STOCK_TRANSACTION     │         │     WAREHOUSE       │
├─────────────────────┤         ├──────────────────────────┤         ├─────────────────────┤
│ PK productCode      │◄────────│ FK productCode           │         │ PK warehouseCode    │
│    productName      │   1   M │ PK transactionId         │ M   1 ──►│    warehouseName    │
│    category         │         │ FK warehouseCode         │────────►│    warehouseLocation│
│    quantityInStock  │         │    transactionDate       │         └─────────────────────┘
│    unitPrice        │         │    quantityMoved         │
│    supplierName     │         │    transactionType       │
│    dateReceived     │         └──────────────────────────┘
└─────────────────────┘
```

**Symbols used:**
- **PK** = Primary Key (underlined in hand-drawn ERD)
- **FK** = Foreign Key
- **1** = One (crow's foot on the "many" side)
- **M** = Many

## Business Rules
- Stock **IN** transactions increase `Product.quantityInStock`.
- Stock **OUT** transactions decrease `Product.quantityInStock`.
- Product and Warehouse forms support **INSERT** only (per exam requirements).
- StockTransaction supports full **CRUD** (Create, Read, Update, Delete).
