Create a PostgreSQL Table with GENERATED Columns (Stored)
📖 Scenario: You are managing a small online store database. You want to keep track of product prices and calculate the total price including tax automatically.
🎯 Goal: Create a PostgreSQL table called products with columns for product_name, price, and two stored generated columns: price_with_tax that adds 10% tax to the price, and price_with_discount that calculates a 5% discount on the price.
📋 What You'll Learn
Create a table named
productsAdd a
product_name column of type TEXTAdd a
price column of type NUMERIC(10,2)Add a stored generated column
price_with_tax that is price * 1.10Add a stored generated column
price_with_discount that is price * 0.95💡 Why This Matters
🌍 Real World
Generated columns help automate calculations in databases, such as computing taxes, discounts, or derived values without manual updates.
💼 Career
Understanding generated columns is useful for database developers and administrators to optimize data integrity and reduce application logic.
Progress0 / 4 steps