Reputation: 11
ALTER TABLE apple_member ADD CONSTRAINT uc_banana_member UNIQUE (banana_id, apple_id) WHERE banana_id IS NOT NULL
I want to add unique constraint only for not null values but i get syntax error:
Reason: liquibase.exception.DatabaseException: Incorrect syntax near the keyword 'WHERE'. [Failed SQL: (156) ALTER TABLE apple_member ADD CONSTRAINT uc_banana_member UNIQUE (banana_id, apple_id) WHERE banana_id IS NOT NULL]
I really don't know what else I can do.
Purpose: add the cosntratint for not null values
Upvotes: 0
Views: 101
Reputation: 48875
Unfortunately the SQL Standard does not allow you to add a formal constraint for a subset of the rows. However, you can use an index to impose this restriction.
For example:
create unique index ix1 on apple_member (banana_id, apple_id)
where banana_id is not null
Upvotes: 0