Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 22-Jan-15 16:43pm
v2
Comments
Jörgen Andersson 17-Jan-15 16:41pm    
Is table1 and table2 having the same columns?
Mahesh K.M 22-Jan-15 22:42pm    
yes,table 1 and table2 has the same column with same datatype and length

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

For eg:

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

)


Then dont pass anything in Insert statement for Item_ID... SQL qill processs it automatically...
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900