多个列的SQL检查约束

时间:2022-07-22 04:23:37

I am new to the SQL CHECK CONSTRAINT and need something to verify that a combination of three columns in my table does not match those on another row.

我是SQL检查约束的新手,需要一些东西来验证表中三个列的组合是否与另一行中的列相匹配。

I have a Report table including three columns that I need to check against: NAME, CREATEDBY, and TYPE. No multiples of a row with those three values being identical may be created.

我有一个包含三列的报表,我需要检查它们:NAME、CREATEDBY和TYPE。不能创建具有这三个值相同的行的倍数。

Please help!

请帮助!

CREATE TABLE Report(
    ReportID    INT             IDENTITY(1,1) NOT NULL,
    [Name]      VARCHAR(255)    NOT NULL,
    CreatedBy   VARCHAR(50)     NOT NULL,
    [Type]      VARCHAR(50)     NOT NULL,
    PageSize    INT             NOT NULL DEFAULT 25,
    Criteria    XML             NOT NULL
    CONSTRAINT CHK_Name_CreatedBy_Type CHECK ([Name], CreatedBy, [Type])
)

ALTER TABLE Report
    ADD CONSTRAINT PK_Report PRIMARY KEY (ReportID)

Obviously, the constraint currently makes no sense as it does not provide a boolean...

显然,约束当前没有意义,因为它不提供布尔值……

CONSTRAINT CHK_Name_CreatedBy_Type CHECK ([Name], CreatedBy, [Type])

Thanks in advance!!

提前谢谢! !

1 个解决方案

#1


7  

You need a UNIQUE constraint:

您需要一个唯一的约束:

CONSTRAINT UNQ_Name_CreatedBy_Type UNIQUE ([Name], CreatedBy, [Type])

#1


7  

You need a UNIQUE constraint:

您需要一个唯一的约束:

CONSTRAINT UNQ_Name_CreatedBy_Type UNIQUE ([Name], CreatedBy, [Type])