Create a BEFORE Trigger in PostgreSQL
📖 Scenario: You are managing a customer database for a small business. You want to ensure that before any new customer is added, their email address is always stored in lowercase to maintain consistency.
🎯 Goal: Build a PostgreSQL BEFORE INSERT trigger that automatically converts the email field to lowercase before saving a new customer record.
📋 What You'll Learn
Create a table called
customers with columns id (integer primary key), name (text), and email (text).Create a trigger function called
lowercase_email that converts the email field to lowercase.Create a BEFORE INSERT trigger called
before_insert_customers on the customers table that calls the lowercase_email function.Ensure the trigger modifies the
NEW.email value before the row is inserted.💡 Why This Matters
🌍 Real World
Triggers help enforce data consistency automatically, such as formatting emails uniformly before saving.
💼 Career
Database triggers are common in backend development and database administration to maintain data integrity and automate tasks.
Progress0 / 4 steps