Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2007, 12:57 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default Composite Foreign Key

Table ABC
{
  id,
.....,
  AssetId
}

Table Asset
{
   AssetID,
   AssetTypeID,
   ......,
   ......,
}

I need to use foreign key constraint on AssetId field of ABC based on
AssetId field of Asset table where AssetType is equal to certain Value of AssetTypeId

How this can be done????

Seek u'r help.



Bijgupt
__________________
Bijgupt
 
Old May 4th, 2007, 01:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Don't you have a table for AssetTypes? If you have one, you should be setting up a Foreign Key contraint on Asset.AssetTypeID to reference AssetTypes.AssetTypeID and a Foreign Key contraint on ABC.AssetID to reference Asset.AssetID

Hope that helps.

_________________________
- Vijay G
Strive for Perfection
 
Old May 4th, 2007, 04:11 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Exactly i've Assettype table and same foreign key constraint. But Asset table contains asset with different assetType. Now the table ABC is for specific details of particular assetType say books. So AssetID of Table ABC should only allow those assetId from Asset table where AssetTypeID is a particular value.


Bijgupt
 
Old May 4th, 2007, 04:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:But Asset table contains asset with different assetType.
Does it mean that Asset Table contains AssetTypes that are not available in AssetTypes table? I wonder how the data is maintained that way. When you set Foreign key constraint set for Asset Table to refer AssetTypeID from AssetTypes, I dont think you should have such a set up that violate foreign key constraint.

I would say you should be adding that to AssetTypes table that are missing in Asset table.

_________________________
- Vijay G
Strive for Perfection
 
Old May 4th, 2007, 05:52 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No the case is like this...
  ABC.AssetID references Asset.AssetID.
  Asset.AssetTypeID references AssetType.AssetTypeId

Table ABC's AssetId can accept any AssetId which is in Asset Table irrespective of its AssetTypeId field.
For each time when something is to be inserted in Table ABc i need to check by query in every procedure whether its assetTypeId is a particular value..
 Select * From Asset
    Where AssetTypeId = <some value>

Asset Table contains comman fields of all asset or say resources and those field which are specific to particular asset is stored in its corresponding table. it is all working very fine but i need above composite fk to reduce some lines and better constraint.
Table Abc should accept only those value out of all assetId value from asset table where assetTypeid is a particular value


Bijgupt
 
Old May 4th, 2007, 07:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

I dont think you can set foreign key constraint for specific values. It is defined at the table and column level, not at the data level.

cheers.

_________________________
- Vijay G
Strive for Perfection
 
Old May 4th, 2007, 08:46 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx a lot.

Bijgupt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Composite primary key evandromoreno Dreamweaver (all versions) 0 July 16th, 2004 02:12 PM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM
Foreign Key Problem re_anu SQL Server 2000 1 September 26th, 2003 10:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.