Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 4th, 2007, 12:57 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
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
Reply With Quote
  #2 (permalink)  
Old May 4th, 2007, 01:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
Reply With Quote
  #3 (permalink)  
Old May 4th, 2007, 04:11 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
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
Reply With Quote
  #4 (permalink)  
Old May 4th, 2007, 04:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
Reply With Quote
  #5 (permalink)  
Old May 4th, 2007, 05:52 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
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
Reply With Quote
  #6 (permalink)  
Old May 4th, 2007, 07:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
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
Reply With Quote
  #7 (permalink)  
Old May 4th, 2007, 08:46 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx a lot.

Bijgupt
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:39 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.