Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting all tables with primary but no foreign keys

Status
Not open for further replies.

stiej

Technical User
Jan 20, 2003
142
GB
Hi,

i'm wanting to write a script to output all my user tables that have a primary key field(s), BUT also NO foreign keys in the same table.

Would any one point me in the right direction as to which tables would hold all this data for me, or any SPs that already do this?

Any help would be much appreciated.

Thanks.
 
here we go:

Code:
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' 
AND TABLE_NAME NOT IN
(
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
)
 
FANTASTIC! Thanks ever so much, that's a great help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top