今天看到一段sql的约束
create table emploee (
id bigserial NOT NULL PRIMARY KEY,
name varchar(32) NOT NULL,
age int NOT NULL,
sex int NOT NULL,
salary int NOT NULL
);
alter table emploee add constraint check_salary check
(
case when sex = 2 then
case when salary <= 200000 then 1 else 0 end
else 1 end = 1
);
倒数第二行 else 1 end = 1 没有理解为什么要写上 'end=1'
查询语句没有看到end=1这种操作,'else 1 end'不能理解为返回为真嘛?
case when sex = 2 then
case when salary <= 200000 then 1 else 0 end
else 1 end = 1
翻译之后:
int a;
if(sex==2){ // case when sex = 2 then
if(salary < 200000){ // case when salary <= 200000 then
a=1; // 1
}else{ // else
a= 0; // 0
} // end
}else{ // else
a = 1; // 1
} // end
if (a == 1){ // = 1
return true;
}else{
return false;
}
create table t1(id int identity primary key,A int,B int,
constraint Chk_B Check(case when A<>1 then 1 else
case when B>0 then 1 else 0 end end=1));