从bit到int的外键

Ego*_*4eg 2 sql-server-2008

是否可以创建一个外键,其中第一列的类型是位,但引用列的类型是int?

Mar*_*ith 5

没有.

create table X
(
  c int primary key
)

create table Y
(
  c bit references X
)
Run Code Online (Sandbox Code Playgroud)

返回:

Msg 1778, Level 16, State 0, Line 1
Column 'X.c' is not the same data type as referencing column 'Y.c' in foreign key 'FK__Y__c__34C8D9D1'. Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

另请参阅BOL中的相关部分:

  • REFERENCES列级FOREIGN KEY约束的子句只能列出一个引用列.此列必须与定义约束的列具有相同的数据类型.

  • REFERENCES表级FOREIGN KEY约束的子句必须具有与约束列列表中的列数相同的引用列数.每个引用列的数据类型也必须与列列表中的相应列相同.