Skip to main content

Overview

The Categories module enables you to organize products into logical groups for better inventory management, reporting, and navigation. Each product in the system can be assigned to a category for classification.

Category Entity

Categories are stored in the categoria table with the following properties:
CodCategoria
int
required
Unique identifier for the category (auto-incremented primary key)
Codigo
string
required
Category code (max 20 characters). Must be unique across all categories.
Nombre
string
required
Category name (max 150 characters)
Descripcion
string
Detailed description of the category (max 300 characters)
Estado
bool
Indicates whether the category is active (defaults to true)
CreatedDate
DateTime
Timestamp when the category was created (defaults to current date/time)

Database Schema

CREATE TABLE categoria(
    codCategoria INT IDENTITY(1,1) PRIMARY KEY,
    codigo VARCHAR(20) NOT NULL UNIQUE,
    nombre VARCHAR(150) NOT NULL,
    descripcion VARCHAR(300),
    estado BIT DEFAULT 1,
    created_date DATETIME DEFAULT GETDATE()
)

Indexes

The categories table includes optimized indexes for performance:
  • IDX_categoria_codigo: Unique index on codigo field for fast lookups and duplicate prevention
  • IDX_categoria_nombre: Index on nombre for efficient name-based searches
  • IDX_categoria_estado: Index on estado to quickly filter active/inactive categories

Category-Product Relationship

Categories have a one-to-many relationship with products:

One-to-Many

A single category can contain multiple products. Products reference categories via the codCategoria foreign key.

Optional Assignment

Products can exist without a category assignment (codCategoria is nullable in the products table).

Category Operations

Creating Categories

When creating a new category:
1

Assign Unique Code

Provide a unique Codigo (max 20 characters) that identifies the category
2

Define Name

Enter a descriptive Nombre that clearly indicates what products belong in this category
3

Add Description

Optionally provide a Descripcion to give more context about what products fit this category
4

Set Status

Leave Estado as true (default) to make the category active immediately
Use consistent code formats for better organization, such as:
  • CAT-ELEC for Electronics
  • CAT-FOOD for Food Items
  • CAT-FURN for Furniture
  • CAT-CLTH for Clothing

Category Code Examples

{
  "Codigo": "CAT-001",
  "Nombre": "Electronics",
  "Descripcion": "Electronic devices, components, and accessories",
  "Estado": true
}

Enabling/Disabling Categories

Categories can be deactivated without deletion:
  • Active (Estado = 1): Category appears in product selection and reports
  • Inactive (Estado = 0): Category is hidden but existing product associations remain
Disabling a category does not affect existing products assigned to it. Products retain their category assignment even when the category is inactive.

Deleting Categories

Categories that have products assigned to them are protected by the foreign key relationship. Before deleting a category:
  1. Reassign all products to different categories, or
  2. Set product codCategoria to NULL, or
  3. Disable the category instead using Estado = 0

Model Reference

The C# model for Category (TechCore.Models.Categorium) includes:
public partial class Categorium
{
    public int CodCategoria { get; set; }
    public string Codigo { get; set; } = null!;
    public string Nombre { get; set; } = null!;
    public string? Descripcion { get; set; }
    public bool? Estado { get; set; }
    public DateTime? CreatedDate { get; set; }

    // Navigation property
    public virtual ICollection<Producto> Productos { get; set; } = new List<Producto>();
}
The model uses the singular form Categorium following Entity Framework’s naming conventions for the plural table name categoria.

Querying Categories

Get All Active Categories

SELECT * FROM categoria WHERE estado = 1
ORDER BY nombre

Get Categories with Product Count

SELECT 
    c.codCategoria,
    c.codigo,
    c.nombre,
    c.descripcion,
    COUNT(p.codprod) as TotalProducts
FROM categoria c
LEFT JOIN productos p ON c.codCategoria = p.codCategoria
WHERE c.estado = 1
GROUP BY c.codCategoria, c.codigo, c.nombre, c.descripcion
ORDER BY TotalProducts DESC

Find Categories Without Products

SELECT c.*
FROM categoria c
LEFT JOIN productos p ON c.codCategoria = p.codCategoria
WHERE p.codprod IS NULL
AND c.estado = 1

Reporting and Analytics

Generate reports showing stock levels, total value, and product counts grouped by category for inventory analysis.
Track which categories generate the most revenue by joining sales data with product categories.
Calculate profit margins by comparing purchase prices (precioCompra) and sale prices (precioVenta) within each category.
Monitor products that fall below minimum stock levels (stockMinimo) organized by category.

Integration with Products

Products reference categories through the codCategoria foreign key:
CREATE TABLE productos(
    codprod VARCHAR(50) PRIMARY KEY,
    codCategoria INT NULL,
    descripcion VARCHAR(500),
    precioCompra DECIMAL(18,2) NOT NULL,
    precioVenta DECIMAL(18,2) NOT NULL,
    stock INT DEFAULT 0,
    stockMinimo INT DEFAULT 5,
    estado BIT DEFAULT 1,
    created_date DATETIME DEFAULT GETDATE(),
    CONSTRAINT FK_productos_categoria
    FOREIGN KEY (codCategoria) REFERENCES categoria(codCategoria)
)

Best Practices

Logical Grouping

Create categories that reflect how your business naturally organizes products. Avoid creating too many narrow categories.

Code Consistency

Establish and follow a consistent format for category codes (e.g., CAT-XXX or CTG-XXXX).

Clear Descriptions

Write clear descriptions to help users understand what products belong in each category.

Regular Cleanup

Periodically review categories to merge duplicates or remove unused ones.

Category Management Tips

Planning Categories: Before creating categories, analyze your product inventory to identify natural groupings. Common approaches include:
  • By product type (Electronics, Furniture, Clothing)
  • By department (Hardware, Grocery, Pharmacy)
  • By supplier or brand
  • By price range or margin level
Categories help with:
  • Faster product searches and filtering
  • Organized inventory reports
  • Better analytics and sales insights
  • Simplified product management
  • Enhanced user navigation

Build docs developers (and LLMs) love