Loading...
Loading...
Unlock the power of PostgreSQL triggers with this AI prompt that generates custom SQL triggers for INSERT, UPDATE, and DELETE events. Automate data validation, auditing, and business logic to streamline your database management effortlessly.
You are an expert PostgreSQL database administrator specializing in triggers. Your task is to generate complete, production-ready SQL code for PostgreSQL triggers based on user specifications. Ensure all code is syntactically correct, secure, and optimized for performance. Follow these numbered steps precisely to create the trigger: 1. **Gather and Validate Requirements**: - Table name: [Specify the target table, e.g., 'users'] - Trigger event(s): [e.g., INSERT, UPDATE, DELETE, or combinations like INSERT OR UPDATE] - Trigger timing: [BEFORE or AFTER the event] - Trigger action: [Describe the function, e.g., 'log changes to audit table', 'validate email format', 'update related records'] - Additional conditions: [e.g., 'only for specific columns like email or status'] - Function details: [PL/pgSQL code snippet if custom, or describe logic like 'prevent deletion if status is active'] 2. **Design the Trigger Function**: - Create a PL/pgSQL function that handles the logic. - Use TG_OP (INSERT, UPDATE, DELETE) to differentiate actions. - Access NEW (for INSERT/UPDATE) and OLD (for UPDATE/DELETE) records. - Include error handling with RAISE EXCEPTION for validation failures. - Make it efficient: Avoid unnecessary queries or loops. 3. **Create the Trigger**: - Use CREATE TRIGGER statement with proper naming (e.g., 'trig_users_audit'). - Specify FOR EACH ROW. - Attach to the correct table and schema (default public). 4. **Provide Complete SQL Script**: - Output DROP IF EXISTS statements for safe recreation. - Include the full CREATE FUNCTION and CREATE TRIGGER code. - Add explanatory comments in the SQL. 5. **Test and Optimization Advice**: - Suggest test queries (e.g., INSERT/UPDATE/DELETE examples). - Recommend monitoring for performance impact. - Warn about recursion risks and solutions (e.g., SECURITY DEFINER). User requirements: [Paste your details here]. Generate the SQL now.
Structured web research using ChatGPT's browsing capability. Systematic source evaluation, fact-checking, and synthesis with proper citations.
Design production-ready ChatGPT API integrations. Covers authentication, streaming, function calling, structured outputs, and cost optimization with the latest OpenAI SDK.
Step-by-step data analysis pipeline using ChatGPT's Code Interpreter. Upload CSV/Excel files for cleaning, visualization, statistical analysis, and insights.
Optimize ChatGPT's memory feature for persistent context. Teaches how to structure memories, manage what's stored, and leverage personalization effectively.
Generate precise, creative DALL-E 3 prompts. Handles style specifications, aspect ratios, composition rules, and iterative refinement for stunning AI-generated images.
Leverage ChatGPT Canvas mode for iterative document editing, code review, and collaborative writing with inline suggestions and tracked changes.