具有IF条件的外键 [英] Foreign Key with IF condition

查看:69
本文介绍了具有IF条件的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两张桌子

一张桌子是Systemcode(ID Uniqueindetifier,名字是NVARCHAR(50))

第二张桌子是calldesk(类型NVARCHAR(50),systemid Uniqueindetifier)



现在可能有系统级别的呼叫或非系统级别的呼叫。

时有系统级调用我必须在calldesk中插入一个具有适当系统ID的记录(存在于系统代码中)



当有非系统级调用时我必须插入calldesk中有一个空系统ID的记录(0000-0000 ......)



现在验证第一级我需要在systemid表单calldesk之间创建FK来自systemid的id与第二级的检查条件,因为在systemid表中没有这样的记录,空id(0000-000 ....)



我也可以不插入空的id systemid表。



我也无法从SP检查这个。因为已有500 SP将数据从同一级别插入呼叫台。





可以通过检查约束实现此目的。 ?

解决方案

允许外键列为NULL,并为非系统级调用插入NULL而不是有效的Id。


你好,我有类似的问题...我有一个以上的表需要在单个外键中解决,基于偶数赔率...或x mod y类型

例如,如果table_referenced有x然后

如果x mod 3 = 0

然后从table1加载记录

如果x mod 3 = 1

然后从table2加载记录

如果x mod 3 = 2

然后从表3加载记录





但是喜欢这种方式

创建表table_referenced



ref_field int引用table1(t1id), table2(t2id),table3(t3id)





i知道没有这样的命令,但我想做一些同样的事情。 ..


搜索并发现sql server中没有这样的特性

Hi all,
I have two table
One table is Systemcode (ID Uniqueindetifier , name NVARCHAR(50))
Second table is calldesk (type NVARCHAR(50),systemid Uniqueindetifier)

Now there may a system level call or non system level call.
when there is system level call i have to insert one record in calldesk with proper system id (present in systemcode)

and when there is non system level call i have to insert one record in calldesk with empty system id (0000-0000......)

Now to validate the first level i need to create FK between systemid form calldesk to id from systemid with check condition of second level because there is not such record in systemid table with empty id (0000-000....)

Also i can not insert the empty id systemid table.

also i can not check this from SP . because there is already 500 SP which will insert the data into call desk from same level.


can i achieve this with check constraint. ?

解决方案

Allow NULL for the foreign key column and insert NULL instead of a valid Id for the non system level calls.


hello, i have similiar problem... i have a more than one table that need to be addressed in single foreign key, base on even odds... or x mod y type
for example if table_referenced has x then
if x mod 3 = 0
then load records from table1
if x mod 3 = 1
then load records from table2
if x mod 3 = 2
then load records from table3


but like in this way
create table table_referenced
(
ref_field int references table1(t1id),table2(t2id),table3(t3id)
)

i know there is no such command but i want something that do the same thing...


Searched and found that no such feaure available in sql server


这篇关于具有IF条件的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆