Can an SQL constraint be used to prevent a particular value being changed when a condition holds?

后端 未结 3 1662
礼貌的吻别
礼貌的吻别 2021-01-05 08:12

I know that SQL constraints can force data to meet validity criteria. However, what about criteria such as \"Student\'s grade can only be updated when the \'finalised\' flag

3条回答
  •  醉话见心
    2021-01-05 08:59

    IMO, I'd say it should be done in either the application or the stored procedure (possibly both), rather than as an actual constraint (among other things, in your specific example, a grade being "finalized" doesn't always mean it's actually final).

    However, if I were implementing this as a constraint, I'd use a CHECK constraint (again, using your example)

    CONSTRAINT chk_grade CHECK(grade between 0 AND 100 and finalized = 0)
    

    Check the specific syntax on that, but it's where I'd start.

提交回复
热议问题