7

i am new to postgres

how to create table with check constraint for column name say polluted which only have to accept 'yes' or 'no' values on insert.

for other values it should promote error message

My table name is vehicles

3
  • my table name is vehicles Commented Jul 12, 2018 at 7:03
  • 1
    Why don't you use a boolean type? That would be a much better choice for a yes/no flag Commented Jul 12, 2018 at 7:04
  • but i want for future purpose for other requirements Commented Jul 12, 2018 at 7:04

1 Answer 1

20

Use an in condition.

create table vehicles
(
  id integer primary key, 
  polluted text not null check (polluted in ('yes', 'no'))
);
Sign up to request clarification or add additional context in comments.

1 Comment

your query working fine and also how to write constraint error message if other value is inserted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.