# 05. Entity Relationship Diagram

## Entity List

1. User
2. Supplier
3. Shipment
4. Delivery

## Primary Keys

- User: `_id`
- Supplier: `_id`
- Shipment: `_id`
- Delivery: `_id`

MongoDB automatically creates `_id` as the primary key for every document.

## Important Unique Fields

- User: `username`
- Supplier: `supplierCode`, `email`
- Shipment: `shipmentNumber`
- Delivery: `deliveryCode`

## Foreign Key References

- Shipment contains `supplier`, which references Supplier `_id`.
- Delivery contains `shipment`, which references Shipment `_id`.

## Relationships and Cardinalities

### Supplier to Shipment

One Supplier can have many Shipments.

Manual ERD drawing:

```text
Supplier (1) -------------------- (Many) Shipment
```

The `supplier` field in the Shipment collection stores the referenced Supplier `_id`.

### Shipment to Delivery

One Shipment can have many Deliveries.

Manual ERD drawing:

```text
Shipment (1) -------------------- (Many) Delivery
```

The `shipment` field in the Delivery collection stores the referenced Shipment `_id`.

## ERD Drawing Explanation

Draw four rectangles named User, Supplier, Shipment, and Delivery. Under each rectangle, list its fields. Mark `_id` as the primary key. In Shipment, mark `supplier` as a foreign key to Supplier. In Delivery, mark `shipment` as a foreign key to Shipment. Connect Supplier to Shipment using a one-to-many line. Connect Shipment to Delivery using a one-to-many line.

