0

I am using postgesql.

I have already created a database and table. However in one of the table I want to add a constraint but it is not working. I wanted to set the value in column is_official to only T or F.

ALTER TABLE countrylanguage
ALTER COLUMN is_official 
ADD CONSTRAINT conchk 
CHECK (is_official IN ('T','F'));

1 Answer 1

3

A check constraint is not defined on a column, but on the table. So the alter column part is invalid when you try to define a check constraint:

ALTER TABLE countrylanguage
  ADD CONSTRAINT conchk CHECK (is_official IN ('T','F'));

However, it would be better to define the column is_official as boolean rather than a text (or varchar) with a check constraint on it to mimic a true boolean column.

Sign up to request clarification or add additional context in comments.

Comments

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.