简体   繁体   English

仅在满足条件时才使用 CHECK 语句

[英]CHECK statement only IF the condition is met

I have to create a table where I need to check a condition only if another condition is met.我必须创建一个表,只有在满足另一个条件时才需要检查一个条件。

To precise I have a table "repair" with columns "solved" and "price", if the column "solved" has value 0 i need to check that the column "price" is equal to 0 too.准确地说,我有一个包含“已解决”和“价格”列的“修复”表,如果“已解决”列的值为 0,我需要检查“价格”列是否也等于 0。

How can I do this in the create table statement?如何在 create table 语句中执行此操作?

I think you're looking for我想你正在寻找

ALTER TABLE Repair
ADD CONSTRAINT MyConstraint CHECK((Solved = 0 AND Price = 0) OR (Solved <> 0 AND Price <> 0));

How can I do this in the create table statement?如何在 create table 语句中执行此操作?

CREATE TABLE Repair(
  Solved BIT NOT NULL,
  Price DECIMAL(19,4) NOT NULL,
  CONSTRAINT MyConstraint CHECK((Solved = 0 AND Price = 0) OR (Solved <> 0 AND Price <> 0))
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM