From the course: MySQL Advanced Topics

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Preventing updates

Preventing updates - MySQL Tutorial

From the course: MySQL Advanced Topics

Preventing updates

- [Bill] Hi, I am Bill Weinman. Triggers may be used to prevent changes to rows that have already been reconciled or should not be changed for other reasons. I've copied this block of SQL from the chapter five exercise file. We'll start by using the Scratch database and then we're going to create a simple sale table and insert some sales into it and select from it. And that all looks like this, we have a sale table with sale id, item id, customer id, quantity, price, and reconciled. And you notice that our quantities are five, three, and one for the IDs one, two, and three. And that relates to what we're about to do here. We're going to create a trigger that prevents an update to the widgetSale table if the reconciled flag is already set. So here's the trigger, it says CREATE TRIGGER updateWidgetSale BEFORE UPDATE ON widgetSale. So if we try to update the widgetSale table, it'll trigger this trigger before the update, so we can see whether or not we actually want to do the update. And…

Contents