Postgresql alter table column type to unique not null

Viewed 1387
ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT NULL;
ERROR:  syntax error at or near "VARCHAR"
LINE 1: ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT ...

I want to alter column email to add its type as UNIQUE NOT NULL in Postgresql and get this error. Can you explain to me what's wrong?

1 Answers

You cannot create 2 constraints with one single statement. And you have to use PostgreSQL syntax.

alter table users alter column email set not null;
alter table users add constraint email_unique unique (email);
Related