简体   繁体   English

ERROR列的CHECK约束引用另一列

[英]ERROR Column CHECK constraint for column references another column

I use MS SQL 2008 R2, I need create a Table with a CHECK on a specific column but I receive this error. 我使用MS SQL 2008 R2,我需要在特定列上创建一个带有CHECK的表但是我收到此错误。 Could you please point me out in the right direction? 你能指出我正确的方向吗? Thanks 谢谢

HeatingSystem   tinyint             NOT NULL
    CONSTRAINT  CK_ReProperties_HeatingSystem   CHECK(Size between 0 and 3),

ERROR 错误

Msg 8141, Level 16, State 0, Line 1 Column CHECK constraint for column 'HeatingSystem' references another column, table 'ReProperties'. 消息8141,级别16,状态0,行1列“加热系统”列的CHECK约束引用另一列,表'ReProperties'。 Msg 1750, Level 16, State 0, Line 1 Could not create constraint. 消息1750,级别16,状态0,行1无法创建约束。 See previous errors. 查看以前的错误。

Constraints that are defined inline at column level can only reference the column they are defined next to. 在列级别内联定义的约束只能引用它们旁边定义的列。

Either move the constraint definition next to the correct column or move the constraint definition to the end of the table definition. 将约束定义移动到正确的列旁边,或将约束定义移动到表定义的末尾。

Fails 失败

CREATE TABLE HeatingSystem
(
Size INT,
HeatingSystem TINYINT CHECK(Size between 0 and 3)
)

Succeeds 成功

CREATE TABLE HeatingSystem
(
Size INT CHECK(Size between 0 and 3),
HeatingSystem TINYINT
)   

Also Succeeds 也成功了

CREATE TABLE HeatingSystem
(
Size INT ,
HeatingSystem TINYINT,
CHECK(Size between 0 and 3 AND HeatingSystem BETWEEN 1 AND 10)
)  

The final way also allows you to declare a row level constraint referencing multiple columns. 最后一种方法还允许您声明引用多个列的行级约束。

Here how I have solved. 我在这里如何解决。

HeatingSystem   tinyint             NOT NULL
    CONSTRAINT  CK_ReProperties_HeatingSystem   CHECK(HeatingSystem between 0 and 3),

With your comment, I don't understand where "Size" is coming from... 有了你的评论,我不明白“尺寸”来自哪里......

Can't you just make 你能做到吗?

CONSTRAINT  CK_ReProperties_HeatingSystem   CHECK(HeatingSystem between 0 and 3)

I tried your query and it is giving me error as Invalid column name 'Size'. 我尝试了你的查询,它给我一个错误,因为Invalid column name 'Size'. You should write the columnname - HeatingSystem in place of size . 您应该写下columnname - HeatingSystem来代替size Use the following:- 使用以下内容: -

HeatingSystem   tinyint             NOT NULL
 CONSTRAINT  CK_ReProperties_HeatingSystem   CHECK(HeatingSystem between 0 and 3),

You may be just missing a comma separator before the word CONSTRAINT !! 你可能只是在CONSTRAINT这个词之前错过了一个逗号分隔符!!

eg) if "," after ([WorkLocationId]) in below snippet is missing then, it will throw ERROR Column CHECK constraint for column references another column error. 例如,如果“(,WorkLocationId)”之后的“,”在下面的代码片段中丢失,那么它将为列引用抛出ERROR Column CHECK约束另一个列错误。

This will throw error - 这会抛出错误 -

CREATE TABLE [MYSYSTEM].[User](
[UserId] int Primary key NOT NULL,
[UserName] [nvarchar](50) NULL,
[UserStatus] [nvarchar](1) NULL,
[CreatedDate] [Datetime] NOT NULL,
[WorkLocationId] int NOT NULL Foreign Key References [HRSYSTEM].[WorkLocations]([WorkLocationId])
CONSTRAINT [_UserStatusValues] CHECK ([UserStatus] IN ('A','I') )
)

This will work - 这将有效 -

CREATE TABLE [MYSYSTEM].[User](
[UserId] int Primary key NOT NULL,
[UserName] [nvarchar](50) NULL,
[UserStatus] [nvarchar](1) NULL,
[CreatedDate] [Datetime] NOT NULL,
[WorkLocationId] int NOT NULL Foreign Key References [HRSYSTEM].[WorkLocations]([WorkLocationId]),
CONSTRAINT [_UserStatusValues] CHECK ([UserStatus] IN ('A','I') )
)

只需使用表级约束,即将约束移动到表定义的末尾。

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

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