Skip to content
Advertisement

Prohibit users from updating a column if another column is null?

I have mytable which has 3 integer fields: id, status, project_id.

I’ve told people that they should not progress status past 4 before assigning it a project_id value. Naturally people don’t listen and then there are problems down the road.

Is there a way to return an error if someone tries to update from status 4 to 5 while project_id column is null? I still need people to be able to update status from 2 or 3 to status 4 regardless of it having a project_id.

Advertisement

Answer

You can use CHECK constraint as suggested by @stickbit if you need very simple checks.

If you need a more complicated logic, you can use TRIGGER functionality

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement