Overview
The Refugio database is designed to manage bed reservations in mountain refuges. It consists of 6 main tables that handle users, rooms, beds, reservations, bed-reservation relationships, and companions.Database Support
The system supports both:- PostgreSQL (Primary, with native ENUM support)
- MySQL (Alternative, using VARCHAR with CHECK constraints)
Tables Structure
usuarios (Users)
Stores information about system users (members and administrators).| Field | Type | Constraints | Description |
|---|---|---|---|
id | SERIAL | PRIMARY KEY | Auto-incrementing user identifier |
num_socio | VARCHAR(50) | UNIQUE, NOT NULL | Member number (e.g., A001, U001) |
dni | VARCHAR(20) | UNIQUE, NOT NULL | National ID document |
telf | VARCHAR(20) | Phone number | |
email | VARCHAR(100) | UNIQUE, NOT NULL | Email address (used for login) |
nombre | VARCHAR(100) | NOT NULL | First name |
apellido1 | VARCHAR(100) | NOT NULL | First surname |
apellido2 | VARCHAR(100) | Second surname | |
password | VARCHAR(255) | NOT NULL | Bcrypt hashed password |
rol | VARCHAR(20) | NOT NULL | User role (ENUM: ‘admin’, ‘user’) |
foto_perfil | VARCHAR(255) | Profile picture path | |
fecha_creacion | TIMESTAMP | DEFAULT NOW | Account creation timestamp |
rol:'admin'(administrator),'user'(regular member)
- PRIMARY KEY on
id - UNIQUE on
num_socio,dni,email
habitaciones (Rooms)
Stores information about available rooms in the refuge.| Field | Type | Constraints | Description |
|---|---|---|---|
id | SERIAL | PRIMARY KEY | Auto-incrementing room identifier |
numero | VARCHAR(10) | UNIQUE, NOT NULL | Room number (e.g., 101, 102, 103, 104) |
capacidad | INTEGER | NOT NULL | Maximum bed capacity |
descripcion | TEXT | Room description |
camas (Beds)
Stores individual bed information within rooms.| Field | Type | Constraints | Description |
|---|---|---|---|
id | SERIAL | PRIMARY KEY | Auto-incrementing bed identifier |
numero | INTEGER | NOT NULL | Bed number within the room |
id_habitacion | INTEGER | NOT NULL, FK | Foreign key to habitaciones table |
estado | VARCHAR(20) | DEFAULT ‘libre’ | Bed status (ENUM: ‘libre’, ‘pendiente’, ‘reservada’) |
estado:'libre'(available)'pendiente'(pending approval)'reservada'(confirmed reservation)
id_habitacion→habitaciones(id)ON DELETE CASCADE
- PRIMARY KEY on
id - UNIQUE on (
id_habitacion,numero)
reservas (Reservations)
Stores reservation information.| Field | Type | Constraints | Description |
|---|---|---|---|
id | SERIAL | PRIMARY KEY | Auto-incrementing reservation identifier |
id_usuario | INTEGER | FK, NULL | Foreign key to usuarios table (NULL for special admin reservations) |
id_habitacion | INTEGER | FK, NULL | Foreign key to habitaciones table (NULL for whole refuge reservations) |
numero_camas | INTEGER | NOT NULL, DEFAULT 1 | Number of beds reserved |
fecha_inicio | DATE | NOT NULL | Reservation start date |
fecha_fin | DATE | NOT NULL | Reservation end date |
estado | VARCHAR(20) | DEFAULT ‘pendiente’ | Reservation status |
observaciones | TEXT | Additional notes or reason for special reservations | |
fecha_creacion | TIMESTAMP | DEFAULT NOW | Reservation creation timestamp |
estado:'pendiente'(pending admin approval)'reservada'(confirmed/approved)'cancelada'(cancelled)
id_usuario→usuarios(id)ON DELETE SET NULLid_habitacion→habitaciones(id)ON DELETE CASCADE
- When
id_usuarioIS NULL: Special admin reservation (events, maintenance) - When
id_habitacionIS NULL: Whole refuge reservation (entire facility booked) - When both are NOT NULL: Regular member reservation
- PRIMARY KEY on
id - Index on
id_usuario - Index on
id_habitacion - Index on
fecha_inicio,fecha_fin(for date range queries)
reservas_camas (Reservation-Bed Relationships)
Junction table linking reservations to specific beds.| Field | Type | Constraints | Description |
|---|---|---|---|
id | SERIAL | PRIMARY KEY | Auto-incrementing identifier |
id_reserva | INTEGER | NOT NULL, FK | Foreign key to reservas table |
id_cama | INTEGER | NOT NULL, FK | Foreign key to camas table |
id_reserva→reservas(id)ON DELETE CASCADEid_cama→camas(id)ON DELETE CASCADE
- PRIMARY KEY on
id - UNIQUE on (
id_reserva,id_cama) - Index on
id_reserva - Index on
id_cama
- One reservation to include multiple beds
- Tracking exactly which beds are assigned to each reservation
- Automatic cleanup when reservations or beds are deleted
acompanantes (Companions)
Stores information about companions accompanying the main reservation holder.| Field | Type | Constraints | Description |
|---|---|---|---|
id | SERIAL | PRIMARY KEY | Auto-incrementing companion identifier |
id_reserva | INTEGER | NOT NULL, FK | Foreign key to reservas table |
num_socio | VARCHAR(50) | Member number (if companion is a member) | |
es_socio | BOOLEAN | DEFAULT false | Whether companion is a club member |
dni | VARCHAR(20) | National ID document | |
nombre | VARCHAR(100) | NOT NULL | First name |
apellido1 | VARCHAR(100) | NOT NULL | First surname |
apellido2 | VARCHAR(100) | Second surname | |
actividad | VARCHAR(255) | Planned activity description | |
fecha_registro | TIMESTAMP | DEFAULT NOW | Registration timestamp |
id_reserva→reservas(id)ON DELETE CASCADE
- PRIMARY KEY on
id - Index on
id_reserva
- If
es_socio= true,num_socioshould be provided - Non-members require
dnifor legal purposes - Multiple companions can be registered per reservation
Entity Relationship Diagram
Relationships Summary
-
usuarios → reservas (1:N)
- One user can have multiple reservations
- Reservations can exist without users (special admin reservations)
-
habitaciones → camas (1:N)
- One room contains multiple beds
- Beds are deleted when room is deleted (CASCADE)
-
habitaciones → reservas (1:N)
- One room can have multiple reservations (different time periods)
- Reservations can exist without rooms (whole refuge reservations)
-
reservas → acompanantes (1:N)
- One reservation can have multiple companions
- Companions are deleted when reservation is deleted (CASCADE)
-
reservas ↔ camas (N:M via reservas_camas)
- One reservation can include multiple beds
- One bed can be in multiple reservations (different time periods)
- Junction table handles the relationship
Data Integrity Rules
Cascading Deletes
- Deleting a room → deletes all its beds and reservations
- Deleting a reservation → deletes all companion records and bed assignments
- Deleting a bed → removes it from all reservation-bed relationships
Set NULL
- Deleting a user → sets
id_usuarioto NULL in their reservations (preserves reservation history)
Uniqueness Constraints
- usuarios:
num_socio,dni,emailmust be unique - habitaciones:
numeromust be unique - camas: combination of (
id_habitacion,numero) must be unique - reservas_camas: combination of (
id_reserva,id_cama) must be unique
Business Logic Constraints
Date Validation
fecha_finmust be greater than or equal tofecha_inicio- No overlapping reservations for the same bed
- Check availability before creating reservations
Bed Assignment
- Number of assigned beds in
reservas_camasmust matchnumero_camasinreservas - Beds must be available (not reserved) for the requested date range
- Beds must belong to the specified room
State Transitions
Reservation States:- New reservation →
'pendiente' - Admin approval →
'reservada' - User/Admin cancellation →
'cancelada'
- When reservation is pending → bed =
'pendiente' - When reservation is approved → bed =
'reservada' - When reservation is cancelled → bed =
'libre'
Special Reservation Types
-
Regular Member Reservation:
id_usuarioIS NOT NULLid_habitacionIS NOT NULL- Initial state:
'pendiente'
-
Admin-Created Reservation (for member):
id_usuarioIS NOT NULLid_habitacionIS NOT NULL- Initial state:
'reservada'(auto-approved)
-
Special Event Reservation:
id_usuarioIS NULLid_habitacionIS NOT NULL- State:
'reservada' - Reason in
observaciones
-
Whole Refuge Reservation:
id_usuarioIS NULLid_habitacionIS NULL- State:
'reservada' - Reserves ALL beds in the refuge
- Requires all beds to be available
Indexes for Performance
Recommended indexes for optimal query performance:Database Statistics
Typical system configuration:- Users: 3+ (1 admin, 2+ regular members)
- Rooms: 4 (101, 102, 103, 104)
- Beds: 26 total
- Room 101: 8 beds
- Room 102: 8 beds
- Room 103: 6 beds
- Room 104: 4 beds
- Reservation States: 3 (pendiente, reservada, cancelada)
- Bed States: 3 (libre, pendiente, reservada)
- User Roles: 2 (admin, user)