问题描述
假设我有一个名为 ProjectTimeSpan 的表(我没有,只是作为示例!)包含 StartDate 和 EndDate 列>.
Let's say I have one table called ProjectTimeSpan (which I haven't, just as an example!) containing the columns StartDate and EndDate.
而且我有另一个名为 SubProjectTimeSpan 的表,其中还包含名为 StartDate 和 EndDate 的列,我想在其中设置一个 检查约束,这使得无法将 StartDate 和 EndDate 设置为 ProjectTimeSpan.StartDate 到 ProjectTimeSpan.EndDate
And that I have another table called SubProjectTimeSpan, also containing columns called StartDate and EndDate, where I would like to set a Check constraint that makes it impossible to set StartDate and EndDate to values "outside" the ProjectTimeSpan.StartDate to ProjectTimeSpan.EndDate
一种知道另一个表值的检查约束......
Kind of a Check constraint that knows about another tables values...
这可能吗?
推荐答案
为了回应您对 GSerg 的回答的评论,这里有一个使用函数的检查约束示例:
In response to your comment on GSerg's answer, here's an example check constraint using a function:
alter table YourTable
add constraint chk_CheckFunction
check (dbo.CheckFunction() = 1)
您可以在哪里定义函数,例如:
Where you can define the function like:
create function dbo.CheckFunction()
returns int
as begin
return (select 1)
end
该函数允许引用其他表.
The function is allowed to reference other tables.
这篇关于Check 约束可以与另一个表相关吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!