Community Page
- www.tech-recipes.com/ Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- Go to internet options - general tab. Go to Tabs. Tabs settings. Select "when a new tab is opened, open", and select a blank page. TADA
- Please completely delte my MySpace Profile with all information. I do understand that this will be permanent...
- Thank you! This was very helpful.
- Opps I didn't see lift chair... "The 'gcc compiler' is installed when one installs 'XcodeTools' (via the 'XcodeTools.mpkg' file) from the installation CD (if...
- Sorry if i am repeating anyone but I know if you have mac 10.4.X then on the install disk you can find a copy of xcode...
Tech-Recipes
Cookbook of Tech Tutorials
A foreign key is a column or columns that are used to enforce a link between data in two tables. While SQL Server gives you no quick and easy way to view the foreign keys in a database, this quick query will give you that information. It comes in handy when trying to troubleshoot Foreign Key Cons
... Continue reading »
6 months ago
Your sql seems to have problems with foreign keys consisting of several columns. I get for example:
IE_FK_ADDRESS_USER IE_ADDRESS INST_ID IE_USER HOLDER_ID
IE_FK_ADDRESS_USER IE_ADDRESS INST_ID IE_USER INST_ID
IE_FK_ADDRESS_USER IE_ADDRESS USER_HOLDER_ID IE_USER HOLDER_ID
IE_FK_ADDRESS_USER IE_ADDRESS USER_HOLDER_ID IE_USER INST_ID
but just the second and third row are okay. I am far from being an sql expert, but the following select returns the correct result for my database:
SELECT
ConstraintName = a.CONSTRAINT_NAME,
FromTable = c.TABLE_NAME,
FromColumn = c.COLUMN_NAME,
ToTable = d.TABLE_NAME,
ToColumn = d.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS a,
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS b,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE d
WHERE a.CONSTRAINT_TYPE = 'FOREIGN KEY'
and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
and a.CONSTRAINT_NAME = c.CONSTRAINT_NAME
and b.UNIQUE_CONSTRAINT_NAME = d.CONSTRAINT_NAME
and c.ORDINAL_POSITION = d.ORDINAL_POSITION
ORDER BY a.CONSTRAINT_NAME, c.ORDINAL_POSITION
Best regards,
Heiner