Databases · Senior · Question 35 of 75

How do you add a foreign key or NOT NULL check to a big table without blocking writes for a long time?

Explanation

For NOT NULL, add a CHECK (col IS NOT NULL) NOT VALID, validate it, then SET NOT NULL, which can use the validated check and skip a full scan.

Further reading

Problem with a question

Databases · Senior · question 35

Test yourself

Take the Databases · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz