0

This is my table's records (already populated) table named posts

now i want to make sure that during insertion the user_to and user_from columns are not the same. here in the fourth record you can clearly see user_to=3 and user_from=3

  • 1
    First, I'd handle this in the application layer first. (Actually, I do message myself on Slack when I need to test something, so I'd make sure you really want to do this.) Then I think you could just use a [`CHECK` constraint](https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html) – Chris Haas Jan 20 '22 at 18:36
  • can you give a general code in sql for the trigger i should implement to satisfy the condition – CALDEN MICHAEL Jan 20 '22 at 19:04
  • Trigger **not needed**. Use CHECK. – Akina Jan 20 '22 at 19:16
  • but i need to use trigger its a requirement – CALDEN MICHAEL Jan 20 '22 at 19:26
  • Requirements are often not really requirements. My suggestion is to go back to whomever is requiring this and tell them that there is a better way, and that the MySQL team added a specific succinct syntax that matches your scenario exactly. If you get push back, see this for using a [trigger to raise a signal](https://stackoverflow.com/a/7189396/231316). – Chris Haas Jan 20 '22 at 19:35

0 Answers0