1

I created a table in PostgreSQL like this:

CREATE TABLE Table1 (
  Id varchar(100) PRIMARY KEY CHECK (Id ~ '^[a-z0-9]{3,15}$'),
  ...
);

This will automatically create a constraint called table1_id_check.

Now I would like to change the check constraint to

(Id ~ '^[a-z0-9]{3,}$')

How can I do this in PostgreSQL as a single statement without dropping the constraint and recreating it again?

1 Answer 1

1

Using multiple statements within a transaction works on all SQL dbms that support using this DDL in a transaction.

begin transaction;
    alter table table1
    drop constraint table1_id_check;

    alter table table1 
    add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
commit;

PostgreSQL lets you use multiple clauses within an ALTER TABLE statement.

alter table table1
drop constraint table1_id_check,
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
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.