Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2000 - Query a Table’s Foreign Key relationships

Nearly identical to Query a Table's Foreign Key relationships, but for SQL Server 2000

For a given table 'foo', I need a query to generate a set of tables that have foreign keys that point to foo.

like image 825
Macho Matt Avatar asked Jan 14 '09 20:01

Macho Matt


People also ask

Can a table's primary key be a foreign key?

Yes, foreign key has to be primary key of parent table. Yes, it may not be unique and may have duplicate entries in child table, but it must be unique and does not have any duplicate entries at the parent table (as it is a primary key).

How do I get a foreign key from another table?

To retrieve data from both table associated with foreign key i.e(common column) you have to join both the tables. if you matching data from both table then use INNER JOIN.


1 Answers

SELECT o2.name
FROM sysobjects o
INNER JOIN sysforeignkeys fk on o.id = fk.rkeyid
INNER JOIN sysobjects o2 on fk.fkeyid = o2.id
WHERE o.name = 'foo'
like image 178
devio Avatar answered Sep 28 '22 16:09

devio