1. ๐—ช๐—ต๐—ฎ๐˜โ€™๐˜€ ๐—ฎ ๐—ง๐—ฟ๐—ถ๐—ด๐—ด๐—ฒ๐—ฟ?

    A trigger is an automatic action in the database when a specific event occurs (e.g., after an insert or update).

  2. ๐—ช๐—ต๐˜† ๐—จ๐˜€๐—ฒ ๐—ฎ ๐—ง๐—ฟ๐—ถ๐—ด๐—ด๐—ฒ๐—ฟ ๐—ณ๐—ผ๐—ฟ ๐—•๐˜‚๐—ฑ๐—ด๐—ฒ๐˜ ๐—ง๐—ฟ๐—ฎ๐—ฐ๐—ธ๐—ถ๐—ป๐—ด?

    To ensure the ๐—ฆ๐—ฝ๐—ฒ๐—ป๐˜๐—”๐—บ๐—ผ๐˜‚๐—ป๐˜ in the budget table updates automatically when an expense is added or updated.

  3. ๐—ง๐—ฟ๐—ถ๐—ด๐—ด๐—ฒ๐—ฟ ๐—ผ๐—ป ๐—œ๐—ป๐˜€๐—ฒ๐—ฟ๐˜:

    When an expense is added, a trigger calculates the total spent amount for a user in a specific month and updates the budget.

  4. ๐—ง๐—ฟ๐—ถ๐—ด๐—ด๐—ฒ๐—ฟ ๐—ผ๐—ป ๐—จ๐—ฝ๐—ฑ๐—ฎ๐˜๐—ฒ:

    When an expense is updated, the trigger subtracts the old amount and adds the new amount to the budgetโ€™s ๐—ฆ๐—ฝ๐—ฒ๐—ป๐˜๐—”๐—บ๐—ผ๐˜‚๐—ป๐˜.

  5. ๐—”๐—น๐—ฒ๐—ฟ๐˜ ๐—ผ๐—ป ๐—˜๐˜…๐—ฐ๐—ฒ๐—ฒ๐—ฑ๐—ถ๐—ป๐—ด ๐—•๐˜‚๐—ฑ๐—ด๐—ฒ๐˜:

    If the updated ๐—ฆ๐—ฝ๐—ฒ๐—ป๐˜๐—”๐—บ๐—ผ๐˜‚๐—ป๐˜ exceeds the monthly limit, an alert is raised to warn the user.

  6. ๐—ฆ๐—ค๐—Ÿ ๐—ฆ๐˜†๐—ป๐˜๐—ฎ๐˜… ๐—ณ๐—ผ๐—ฟ ๐—ง๐—ฟ๐—ถ๐—ด๐—ด๐—ฒ๐—ฟ๐˜€:

    Triggers use SQL syntax like AFTER INSERT, AFTER UPDATE, and RAISEERROR to handle automatic updates and alerts.

  7. ๐—ฅ๐—ฒ๐—ฎ๐—น-๐—ช๐—ผ๐—ฟ๐—น๐—ฑ ๐—จ๐˜€๐—ฒ ๐—–๐—ฎ๐˜€๐—ฒ:

    This is especially useful in ๐—ฒ๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ ๐—บ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐˜€๐˜†๐˜€๐˜๐—ฒ๐—บ๐˜€ to track and control monthly spending.

  8. ๐—œ๐—ป๐˜๐—ฒ๐—ด๐—ฟ๐—ฎ๐˜๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐—˜๐—™ ๐—–๐—ผ๐—ฟ๐—ฒ:

    Triggers can be added in the migration files when using ๐—˜๐—™ ๐—–๐—ผ๐—ฟ๐—ฒ in your application.

  9. ๐—ฃ๐—ผ๐˜๐—ฒ๐—ป๐˜๐—ถ๐—ฎ๐—น ๐—œ๐˜€๐˜€๐˜‚๐—ฒ๐˜€:

    Always ensure triggers are properly tested to avoid unexpected updates or errors in your data.

  10. ๐—”๐˜‚๐—ฑ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ค๐˜‚๐—ฒ๐˜€๐˜๐—ถ๐—ผ๐—ป:

    ๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—บ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ ๐—ฎ๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ฐ ๐˜‚๐—ฝ๐—ฑ๐—ฎ๐˜๐—ฒ๐˜€ ๐—ถ๐—ป ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ฑ๐—ฎ๐˜๐—ฎ๐—ฏ๐—ฎ๐˜€๐—ฒ? ๐——๐—ผ ๐˜†๐—ผ๐˜‚ ๐˜‚๐˜€๐—ฒ ๐˜๐—ฟ๐—ถ๐—ด๐—ด๐—ฒ๐—ฟ๐˜€, ๐—ผ๐—ฟ ๐—ถ๐˜€ ๐˜๐—ต๐—ฒ๐—ฟ๐—ฒ ๐—ฎ๐—ป๐—ผ๐˜๐—ต๐—ฒ๐—ฟ ๐˜€๐—ผ๐—น๐˜‚๐˜๐—ถ๐—ผ๐—ป ๐˜†๐—ผ๐˜‚ ๐—ฝ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐—ฟ? ๐—Ÿ๐—ฒ๐˜โ€™๐˜€ ๐—ฑ๐—ถ๐˜€๐—ฐ๐˜‚๐˜€๐˜€!