# MariaDB Connection Details

## Docker Container
- **Container Name:** `rosy-mariadb`
- **Image:** `mariadb:11`
- **Version:** 11.8.5-MariaDB

## Credentials
- **User:** `rosy`
- **Password:** `fakan`
- **Database:** `practice`

## Connection Command

A wrapper script is available at `~/bin/mariadb` (added to PATH via `~/.bashrc`):

```bash
mariadb practice -e "SELECT * FROM customers LIMIT 5"
mariadb practice  # interactive mode
```

Or connect directly via Docker:
```bash
docker exec -i rosy-mariadb mariadb -u rosy -pfakan practice
```

## Table Schemas

### customers
```sql
CREATE TABLE `customers` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `region` varchar(50) NOT NULL,
  `segment` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_region` (`region`),
  KEY `idx_segment` (`segment`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### daily_delivery_metrics
```sql
CREATE TABLE `daily_delivery_metrics` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `report_date` date NOT NULL,
  `warehouse_id` int(11) NOT NULL,
  `warehouse_name` varchar(100) NOT NULL,
  `region` varchar(50) NOT NULL,
  `total_shipments` int(11) NOT NULL DEFAULT 0,
  `on_time_count` int(11) NOT NULL DEFAULT 0,
  `late_count` int(11) NOT NULL DEFAULT 0,
  `cancelled_count` int(11) NOT NULL DEFAULT 0,
  `avg_delay_hours` decimal(6,2) DEFAULT NULL,
  `avg_shipping_cost` decimal(10,2) DEFAULT NULL,
  `high_risk_count` int(11) NOT NULL DEFAULT 0,
  `moderate_risk_count` int(11) NOT NULL DEFAULT 0,
  `low_risk_count` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_date_warehouse` (`report_date`,`warehouse_id`),
  KEY `idx_date` (`report_date`),
  KEY `idx_warehouse` (`warehouse_id`),
  KEY `idx_region` (`region`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### drivers
```sql
CREATE TABLE `drivers` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `warehouse_id` int(11) NOT NULL,
  `behavior_score` decimal(3,2) NOT NULL,
  `fatigue_score` decimal(3,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_warehouse` (`warehouse_id`),
  KEY `idx_behavior` (`behavior_score`),
  CONSTRAINT `drivers_ibfk_1` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### inventory_snapshots
```sql
CREATE TABLE `inventory_snapshots` (
  `id` int(11) NOT NULL,
  `warehouse_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `recorded_at` date NOT NULL,
  `quantity` int(11) NOT NULL,
  `reorder_point` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `product_id` (`product_id`),
  KEY `idx_warehouse_product` (`warehouse_id`,`product_id`),
  KEY `idx_recorded` (`recorded_at`),
  CONSTRAINT `inventory_snapshots_ibfk_1` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`),
  CONSTRAINT `inventory_snapshots_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### monthly_inventory_report
```sql
CREATE TABLE `monthly_inventory_report` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `report_month` date NOT NULL,
  `warehouse_id` int(11) NOT NULL,
  `warehouse_name` varchar(100) NOT NULL,
  `product_id` int(11) NOT NULL,
  `sku` varchar(20) NOT NULL,
  `product_name` varchar(100) NOT NULL,
  `category` varchar(50) NOT NULL,
  `start_quantity` int(11) DEFAULT NULL,
  `end_quantity` int(11) DEFAULT NULL,
  `min_quantity` int(11) DEFAULT NULL,
  `max_quantity` int(11) DEFAULT NULL,
  `avg_quantity` decimal(10,2) DEFAULT NULL,
  `below_reorder_days` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_month_warehouse_product` (`report_month`,`warehouse_id`,`product_id`),
  KEY `idx_month` (`report_month`),
  KEY `idx_warehouse` (`warehouse_id`),
  KEY `idx_product` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### order_items
```sql
CREATE TABLE `order_items` (
  `id` int(11) NOT NULL,
  `order_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `unit_price` decimal(10,2) NOT NULL,
  `discount` decimal(3,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`),
  KEY `idx_order` (`order_id`),
  KEY `idx_product` (`product_id`),
  CONSTRAINT `order_items_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`),
  CONSTRAINT `order_items_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### orders
```sql
CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `shipment_id` int(11) NOT NULL,
  `order_date` date NOT NULL,
  `ship_date` date NOT NULL,
  `delivery_date` date DEFAULT NULL,
  `status` varchar(20) NOT NULL,
  `total_amount` decimal(12,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `shipment_id` (`shipment_id`),
  KEY `idx_customer` (`customer_id`),
  KEY `idx_order_date` (`order_date`),
  KEY `idx_status` (`status`),
  CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`),
  CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`shipment_id`) REFERENCES `shipments` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### products
```sql
CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `sku` varchar(20) NOT NULL,
  `name` varchar(100) NOT NULL,
  `category` varchar(50) NOT NULL,
  `unit_cost` decimal(10,2) NOT NULL,
  `supplier_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `sku` (`sku`),
  KEY `supplier_id` (`supplier_id`),
  KEY `idx_category` (`category`),
  KEY `idx_sku` (`sku`),
  CONSTRAINT `products_ibfk_1` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### shipment_conditions
```sql
CREATE TABLE `shipment_conditions` (
  `id` int(11) NOT NULL,
  `shipment_id` int(11) NOT NULL,
  `recorded_at` datetime NOT NULL,
  `gps_latitude` decimal(10,6) NOT NULL,
  `gps_longitude` decimal(10,6) NOT NULL,
  `traffic_congestion_level` decimal(4,2) NOT NULL,
  `weather_severity` decimal(3,2) NOT NULL,
  `iot_temperature` decimal(5,2) NOT NULL,
  `cargo_condition` varchar(20) NOT NULL,
  `route_risk_level` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_shipment` (`shipment_id`),
  KEY `idx_recorded` (`recorded_at`),
  KEY `idx_risk` (`route_risk_level`),
  CONSTRAINT `shipment_conditions_ibfk_1` FOREIGN KEY (`shipment_id`) REFERENCES `shipments` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### shipments
```sql
CREATE TABLE `shipments` (
  `id` int(11) NOT NULL,
  `vehicle_id` int(11) NOT NULL,
  `driver_id` int(11) NOT NULL,
  `warehouse_id` int(11) NOT NULL,
  `scheduled_departure` datetime NOT NULL,
  `actual_departure` datetime NOT NULL,
  `scheduled_arrival` datetime NOT NULL,
  `actual_arrival` datetime DEFAULT NULL,
  `destination_lat` decimal(10,6) NOT NULL,
  `destination_lng` decimal(10,6) NOT NULL,
  `status` varchar(20) NOT NULL,
  `shipping_cost` decimal(10,2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `vehicle_id` (`vehicle_id`),
  KEY `driver_id` (`driver_id`),
  KEY `idx_status` (`status`),
  KEY `idx_scheduled_dep` (`scheduled_departure`),
  KEY `idx_warehouse` (`warehouse_id`),
  CONSTRAINT `shipments_ibfk_1` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles` (`id`),
  CONSTRAINT `shipments_ibfk_2` FOREIGN KEY (`driver_id`) REFERENCES `drivers` (`id`),
  CONSTRAINT `shipments_ibfk_3` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### suppliers
```sql
CREATE TABLE `suppliers` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `reliability_score` decimal(3,2) NOT NULL,
  `lead_time_days` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_reliability` (`reliability_score`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### vehicles
```sql
CREATE TABLE `vehicles` (
  `id` int(11) NOT NULL,
  `vehicle_type` varchar(50) NOT NULL,
  `warehouse_id` int(11) NOT NULL,
  `capacity_kg` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_warehouse` (`warehouse_id`),
  KEY `idx_type` (`vehicle_type`),
  CONSTRAINT `vehicles_ibfk_1` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### warehouses
```sql
CREATE TABLE `warehouses` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `region` varchar(50) NOT NULL,
  `latitude` decimal(10,6) NOT NULL,
  `longitude` decimal(10,6) NOT NULL,
  `capacity` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_region` (`region`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```

### weekly_sales_summary
```sql
CREATE TABLE `weekly_sales_summary` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `week_start` date NOT NULL,
  `week_end` date NOT NULL,
  `product_id` int(11) NOT NULL,
  `sku` varchar(20) NOT NULL,
  `product_name` varchar(100) NOT NULL,
  `category` varchar(50) NOT NULL,
  `total_quantity_sold` int(11) NOT NULL DEFAULT 0,
  `total_revenue` decimal(14,2) NOT NULL DEFAULT 0.00,
  `total_discount_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `avg_unit_price` decimal(10,2) DEFAULT NULL,
  `unique_customers` int(11) NOT NULL DEFAULT 0,
  `order_count` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_week_product` (`week_start`,`product_id`),
  KEY `idx_week` (`week_start`),
  KEY `idx_product` (`product_id`),
  KEY `idx_category` (`category`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
```
