浪子丶不回头 2019-08-18 16:36 采纳率: 100%
浏览 447
已采纳

sql 理解约束 end = 1

今天看到一段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'不能理解为返回为真嘛?

  • 写回答

2条回答 默认 最新

  • xSeeker~ 2019-08-19 10:19
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?