gpt4 book ai didi

sql-server - 在表之间设置外键时是否可以忽略 varchar 长度?

转载 作者:行者123 更新时间:2023-12-04 04:55:32 27 4
gpt4 key购买 nike

我的 SQL Server 2008 中有两个表。

CREATE TABLE TABLE1 (
field1 varchar(20) not null,
field2 int,
CONSTRAINT PK_TABLE1 PRIMARY KEY (field1)
)

CREATE TABLE TABLE2 (
Id int,
t1 varchar(10) not null
t2 ntext
CONSTRAINT PK_TABLE2 PRIMARY KEY (Id)
)

现在我想使用以下命令在 table2 上构建一个外键:
ALTER TABLE TABLE2  
ADD CONSTRAINT TABLE2_FK
FOREIGN KEY (t1) REFERENCES TABLE1 (field1)
ON DELETE CASCADE ON UPDATE CASCADE

SQL Server 响应 Table2 中的外键与列 field1不同在 Table1在长度上。

是否可以要求 SQL Server 忽略外键长度的验证?

谢谢你的回答。

最佳答案

不,这是不可能的。来自 MSDN页面上CREATE TABLE ,其中包括有关外键的部分:

The REFERENCES clause of a table-level FOREIGN KEY constraint must have the same number of reference columns as the number of columns in the constraint column list. The data type of each reference column must also be the same as the corresponding column in the column list.



当然,您可以创建一个计算列来强制转换 VARCHAR(10)VARCHAR(20)然后设置外键。

关于sql-server - 在表之间设置外键时是否可以忽略 varchar 长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16804358/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com