Kadaj13
Kadaj13

Reputation: 1541

How to change a value of table in triggers

If I want to write a trigger, that if something has changed and does not have the right value, It automatically becomes something else?!

for example I have a table of student numbers and grades. I want to write a trigger that if any number entries any time in the table was less than zero, It becomes automatically zero.

Upvotes: 0

Views: 39

Answers (1)

Ashley Lee
Ashley Lee

Reputation: 3986

I would prevent invalid values from being saved via your application.

One way in your database would be adding a constraint on your values.

ALTER TABLE myTable
ADD CONSTRAINT CK_myTable_grade CHECK (grade BETWEEN 0 AND 100)

This would fail INSERTs or UPDATEs with invalid values, which is better than assuming -80 should = 0

Upvotes: 1

Related Questions