0

Is there an sql constraint that enables you to disallow certain values in a column?

e.g.

ALTER TABLE foo ADD COLUMN code TEXT NOT NULL UNIQUE DISALLOW '<GENERATE>' ; 

s.t. <GENERATE> could be used safely in the application to indicate that the value should be generated before persisting without risking its accidentally being pushed to DB?

2 Answers 2

3

You need a check constraint:

ALTER TABLE foo ADD COLUMN code TEXT NOT NULL UNIQUE; alter table foo add constraint disallow_generate check (code <> '<GENERATE>'); 

If you want to disallowed multiple values, use a NOT IN condition:

alter table foo add constraint disallow_generate check (code not in ('<GENERATE>', '<GENERATED>', 'foo'); 
Sign up to request clarification or add additional context in comments.

Comments

1

You could use Check Constraints. eg:

ALTER TABLE foo ADD COLUMN code TEXT NOT NULL UNIQUE CHECK (code <> '<GENERATE>'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.