Using a case statement in a check constraint

血红的双手。 提交于 2019-12-05 18:56:18

I think you can do the following:

CREATE TABLE Grade
(
  salary_grade    char(1) NOT NULL CHECK (REGEXP_LIKE(salary_grade, '[A-G]', 'c')),
  salary_scale    char(2) DEFAULT 'S1' NOT NULL,
  CONSTRAINT pk_grade PRIMARY KEY (salary_grade),
  CONSTRAINT ck_grade_scale CHECK ( REGEXP_LIKE(salary_grade, '[A-D]', 'c') AND salary_scale = 'S1'
                                 OR REGEXP_LIKE(salary_grade, '[E-G]', 'c') AND salary_scale = 'S2' )
);

Please see SQL Fiddle schema here.

You don't need the UPPER() constraint on salary_grade since the regex check will suffice (you're already checking to make sure it's an uppercase letter between A and G). I don't think the constraint on salary_scale alone is necessary either since it would be contained, logically, in the last constraint.

UPDATE

Here is how you might do it with a CASE statement:

CREATE TABLE Grade
(
  salary_grade    char(1) NOT NULL CHECK (REGEXP_LIKE(salary_grade, '[A-G]', 'c')),
  salary_scale    char(2) DEFAULT 'S1' NOT NULL,  
  CONSTRAINT pk_grade PRIMARY KEY (salary_grade),
  CONSTRAINT ck_grade_scale CHECK ( salary_scale = CASE WHEN REGEXP_LIKE(salary_grade, '[A-D]', 'c') THEN 'S1' ELSE 'S2' END )
);

Please see SQL Fiddle schema here.

A case has to be compared to something, which is why you are getting the missing right parenthesis error. Unless you particularly want a case, you can just check the combination with and/or:

CONSTRAINT ck_grade_scale CHECK(
    (salary_grade BETWEEN 'A' AND 'D' AND salary_scale = 'S1')
    OR (salary_grade BETWEEN 'D' AND 'G' AND salary_scale = 'S2')),

SQL Fiddle demo.

As Parado has said, you can't use constraints to set column values conditionally, only to restrict them. You could potentially use a virtual column for the scale, but it would mean putting part of a look-up table into the DDL rather than the data, which seems a bit strange.

Check Constraints is used to test data before insert to protect data structure from fake data. Actually we use case in select statement. You can't use it for conditional insert. If you want to change data for particular column before insert you need to use trigger or you can also use virtual column but which has some restrictions.

More information you can find here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!