Skip to content

Instantly share code, notes, and snippets.

@victorcrbt
Last active May 1, 2020 17:18
Show Gist options
  • Save victorcrbt/e3576bf240e79c24566fc55627941743 to your computer and use it in GitHub Desktop.
Save victorcrbt/e3576bf240e79c24566fc55627941743 to your computer and use it in GitHub Desktop.
Conditional PostgreSQL not null column.
CREATE TABLE clients (
id SERIAL UNIQUE,
name varchar(255) NOT NULL,
client_type client_type NOT NULL,
cpf varchar(11),
cnpj varchar(14),
CHECK(CASE WHEN client_type = 'F' THEN cpf IS NOT NULL END),
CHECK(CASE WHEN client_type = 'J' THEN cnpj IS NOT NULL END)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment