I am working check constraint into one table, I have status and status_sub_type columns, but for example my status 'Closed' only can be status_subtype = 'Closed-typeA' or Closed-typeB', but right now I just put available status_subtypes into my check constraint, How I can put a condition to say that exists 5 subtypes but if it is status Closed, only can insert 2 different subtypes?
if status = 'close'
then status_subtype in ('closed-typeA', 'closed-typeB')
else if status = 'open'
then status_subtype in ('open-typeA', 'open-typeB')
else if status = 'Active'
then status_subtype = ''
| Status | Subtype |
|---------------------- |
| Closed | Closed-TypeA |
| Closed | Closed-TypeB |
| Open | Open-TypeA. |
| Open | Open-TypeB. |
| Active | |
Here is my code
CREATE TABLE IF NOT EXISTS Core.person
(
person_id serial PRIMARY KEY NOT NULL,
person_name varchar(200),
status varchar(25)
CHECK (status IN ('Closed','Open','Active')),
status_subtype varchar(25)
CHECK (status_subtype IN ('Closed-TypeA', 'Closed-TypeB', 'Open-TypeA', 'Open-TypeB'))
)