If you want to require that col1 be null if col2 not equal to 'y', you can write your CHECK constraint as:
col2 = 'y' OR col1 IS NULL
If you also want to require col1 to have a value when col2 is 'y', you can write a constraint like:
(col2 = 'y' AND col1 IS NOT NULL) OR (col2 != 'y' AND col1 IS NULL)
You should write this as a table constraint, by the way. I do not think the column restriction is allowed to refer to other columns. (But a column constraint is another way to write a table constraint, so you don't lose anything by creating table constraints instead.)
source share