Overview
The Veterinaria ALFA Inventory System uses SQLite as its embedded database. The schema consists of two main tables that manage product inventory and sales history.The database is automatically created at
./db/baseDeDatosInventario.db on first application launch via the crearBaseDeDatos() method in InventarioDAO.java.Tables
productos
Stores information about all pharmaceutical products in the veterinary inventory.Primary key, auto-incremented unique identifier for each product.
Name of the pharmaceutical product or medication.
Current stock quantity available in inventory. Must be non-negative.
Batch or lot number for product traceability.
Expiration date in
yyyy-MM format (e.g., “2025-07”). Used for expiration alerts and visual warnings.Date the product was added to inventory in ISO format
yyyy-MM-dd (e.g., “2024-03-15”).Date the product was reserved (apartado) in ISO format
yyyy-MM-dd. NULL if product is not reserved.SQL Definition
Table Indexes
- Primary Key: Automatically indexed on
idcolumn - No additional indexes defined
Constraints
id: Auto-incrementing primary keyexistencias: Application-level validation ensures non-negative valuescaducidad: Application-level validation ensuresyyyy-MMformatfecha_separado: Can be NULL for non-reserved products
historial_ventas
Records all sales transactions, tracking which products were sold, quantities, and dates.Primary key, auto-incremented unique identifier for each sale record.
Foreign key referencing
productos.id. Links the sale to the specific product.Product name at time of sale (denormalized for historical record keeping).
Quantity of product sold in this transaction. Must be positive.
Date of the sale in ISO format
yyyy-MM-dd (e.g., “2024-03-15”).SQL Definition
Foreign Keys
idProducto → productos(id)
idProducto → productos(id)
Relationship: Many-to-OneOn Delete: CASCADE - When a product is deleted from
productos, all associated sales records are automatically deletedNote: This maintains referential integrity but means historical sales data is lost if a product is deleted. Consider soft deletes for production use.Table Indexes
- Primary Key: Automatically indexed on
idcolumn - Foreign Key: Automatically indexed on
idProductocolumn (SQLite behavior)
Data Types
Date Storage
All dates are stored as TEXT in ISO-8601 format:| Field | Format | Example | Usage |
|---|---|---|---|
fechaEntrada | yyyy-MM-dd | 2024-03-15 | Product entry date |
fechaVenta | yyyy-MM-dd | 2024-03-15 | Sale transaction date |
fecha_separado | yyyy-MM-dd | 2024-03-15 | Reservation date |
caducidad | yyyy-MM | 2025-07 | Expiration year-month |
SQLite does not have a native date/time type. Dates are stored as TEXT and parsed using Java’s
LocalDate and YearMonth classes.Numeric Types
- INTEGER: Used for IDs, quantities, and stock levels
- TEXT: Used for all string data and dates
Relationships
Common Queries
Get All Products
InventarioDAO.obtenerProductos() (line 94)
Get Products Near Expiration
InventarioDAO.obtenerMedicamentosProximosACaducar(int diasUmbral) (line 486)
Get Expired Products
InventarioDAO.obtenerMedicamentosCaducados() (line 521)
Get Reserved Products
InventarioDAO.obtenerProductosApartados() (line 571)
Get Sales History
InventarioDAO.obtenerHistorialVentas() (line 426)
Add Product
InventarioDAO.agregarProducto() (line 119)
Register Sale
InventarioDAO.registrarVenta() (line 247)
Sales are processed within a transaction to ensure atomicity. If any step fails, all changes are rolled back.
Database Initialization
The database and tables are created automatically on first run:InventarioDAO.crearBaseDeDatos() (line 69)
Data Validation
Application-Level Validation
The application enforces these business rules before database operations:Must be non-negative (≥ 0). Validated before insert/update operations.
Must match format
yyyy-MM and be a valid year-month. Validated using regex and YearMonth.parse().Cannot be empty or whitespace-only. Trimmed before validation.
Must be positive (> 0) for sales. Must not exceed available stock.
Stock Validation
Before registering a sale, the system verifies:- Product exists by ID
- Product name matches (for additional verification)
- Sufficient stock is available (
cantidad <= existencias)
Database Location
Default Location
Default Location
The database is created in a If running from JAR:
db/ subdirectory relative to the application:./db/ is relative to JAR locationIf running from IDE: ./db/ is relative to project rootCustom Location
Custom Location
To use a custom database location, provide a JDBC URL to the
InventarioDAO constructor:Backup and Export
CSV Export
The application provides CSV export functionality for:- Complete Inventory (
exportarInventarioCSV) - Sales History (
exportarCSV) - Reserved Products (
exportarApartadosCSV)
Database Backup
To backup the database, simply copy the
./db/baseDeDatosInventario.db file while the application is closed.