Foreign Key and Primary Key Programatic retrieval

Foreign Key and Primary Key Programatic retrieval

Post by Nick » Fri, 24 May 2002 17:53:41



Hello there,

Does any one know how I can retrieve FK and PK relationships through a
sql statement and not through the a diagram. I have got a sql statment
to retrieve the relationships already but it does not work where the
relationship is made up of multiple columns - it just works where they
are made up of single columns.

Any help would be great.

Thanks
Nicky

 
 
 

Foreign Key and Primary Key Programatic retrieval

Post by Uri Diman » Fri, 24 May 2002 19:43:48


Nicky, you can modify this script for you
SELECT USER_NAME( OBJECTPROPERTY( i.id, 'OwnerID' ) )
                AS OwnerName,
     OBJECT_NAME( i.id ) AS TableName,
  i.name AS IndexName,
      CASE INDEXPROPERTY( i.id , i.name , 'IsClustered')
             WHEN 1 THEN 'YES'
             ELSE 'NO'
      END AS IsClustered,
      CASE INDEXPROPERTY( i.id , i.name , 'IsUnique'    )
         WHEN 1 THEN 'YES'
            ELSE 'NO'
      END AS IsUnique,
      CASE INDEXPROPERTY( i.id , i.name , 'IsForeign'    )
         WHEN 1 THEN 'YES'
            ELSE 'NO'
      END AS IsForeign,
      STATS_DATE( i.id , i.indid ) AS LastUpdatedDate
  FROM sysindexes AS i
WHERE OBJECTPROPERTY( i.id, 'IsMSShipped' ) = 0 And
      1 NOT IN ( INDEXPROPERTY( i.id , i.name , 'IsStatistics'   ) ,
          INDEXPROPERTY( i.id , i.name , 'IsAutoStatistics' ) ,
          INDEXPROPERTY( i.id , i.name , 'IsHypothetical'   ) ) And
      i.indid BETWEEN 1 And 250
ORDER BY OwnerName, TableName, IndexName

Quote:> Hello there,

> Does any one know how I can retrieve FK and PK relationships through a
> sql statement and not through the a diagram. I have got a sql statment
> to retrieve the relationships already but it does not work where the
> relationship is made up of multiple columns - it just works where they
> are made up of single columns.

> Any help would be great.

> Thanks
> Nicky


 
 
 

Foreign Key and Primary Key Programatic retrieval

Post by Nicky Ainsworth-Kell » Fri, 24 May 2002 19:46:46


Thanks for that Uri - but I already have a procedure that does a similar
sort of thing - what I am actualy after is a procedure that will return
all of the columns related to that key, as I stated in my mail I already
have a procedure that does what I have asked, but it only does it for
single column keys - if the key has multiple columns then it will not
work.

Any more ideas ??

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

 
 
 

Foreign Key and Primary Key Programatic retrieval

Post by lindawi » Sat, 25 May 2002 00:45:22


Nicky,

Quote:> Thanks for that Uri - but I already have a procedure that does a
> similar sort of thing - what I am actualy after is a procedure that
> will return all of the columns related to that key, as I stated in my
> mail I already have a procedure that does what I have asked, but it
> only does it for single column keys - if the key has multiple columns
> then it will not work.

Look at sp_fkeys, information_schema.table_constraints,
information_schema.constraint_column_usage.

Linda

 
 
 

Foreign Key and Primary Key Programatic retrieval

Post by Nicky Ainsworth-Kell » Sat, 25 May 2002 01:02:58


Thanks very much Linda

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

 
 
 

1. foreign key w/o foreign/primary key

Does anyone have an opinion on the most efficient way to check
referential integrity on a table where the would-be foreign key is
neither unique or primary?  Would I use a check constraint?  If so, how
without a subquery?

Thanks in advance,

Sent via Deja.com
http://www.deja.com/

2. 16 bits DLL in VFP

3. Foreign Key as subset of Primary Key

4. Report Generation

5. Identification of a primary key/foreign key

6. cant connect to SQL 6.5 using Windows 95 PC

7. One FOREIGN KEY refrences a Table with two Primary Keys

8. Copying Primary Key to Foreign Key

9. Changing Primary Key Values when foreign key constraints exist

10. delete primary key records together with the foreign keys records

11. Replication with Primary Keys & Foreign Keys Schema

12. Update primary key & Foreign key