如何根据表中的列值设置约束 [英] How Can I Set Constraints According To A Colum Value In Table

查看:154
本文介绍了如何根据表中的列值设置约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Item_ID分支Item_type

1 20 A

1 20 B



if item_type = A then data来自table1的ITEM_ID

如果item_type = B则来自table2的ITEM_ID数据



我如何在item_id中设置参考约束acc_ item_type value

Item_ID Branch Item_type
1 20 A
1 20 B

if item_type =A then data of ITEM_ID from table1
if item_type =B then data of ITEM_ID from table2

How can i set reference constraints in item_id accoding item_type value

推荐答案

创建一个标量函数,它接受一个参数并返回单个值。

现在定义表模式时使用计算列。



例如:



Create a scalar function which takes an argument and return single value.
Now while defining table schema use computed column.

For eg:

Create Table TableName
(
Branch yourdataType,
ItemType yourdataType
Item_ID as (dbo.YourfunctionName(ItemType ))

)





然后不要在Item语句的Insert语句中传递任何内容... SQL qill自动处理...



Then dont pass anything in Insert statement for Item_ID... SQL qill processs it automatically...


您的数据模型存在缺陷。

将所有内容放在一个表中,然后为 Item_type 添加一列。

现在你可以简单地加入 Item_type 上的表格。



关于规范化的wiki文章是在其中 [ ^ ]。



但是关于原因的更好的文章是 here [ ^ ],Celko是该主题的适当权威,他一直在委员会编写SQL标准。



然后我还建议这个 [ ^ ]文章。它有利于解释普通英语如何没有流行语。
Your data model is flawed.
Put it all in one table instead and add a column for the Item_type.
Now you can simply join the tables on Item_type.

The wiki article on normalization is here[^].

But a better article on the why is here[^], and Celko is a proper authority on the subject, he's been on the committee writing the SQL standard.

And then I'd also recommend this[^] article. It's having the benefit of explaining how in plain English being void of buzzwords.


这篇关于如何根据表中的列值设置约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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