add schema to sysforeignkeys info

To the below code, how would you add the schema (owner) info also, in column 2 and 3?

thanks
SELECT 
OBJECT_NAME(f.constid) AS 'ForeignKey',
OBJECT_NAME(f.rkeyid) AS 'PKTable', 
--c2.[name] AS 'PKColumnName',
OBJECT_NAME(f.fkeyid) AS 'FKTable'
--c1.[name] AS 'FKColumnName'
FROM sysforeignkeys f
INNER JOIN syscolumns c1
ON f.fkeyid = c1.[id]
AND f.fkey = c1.colid
INNER JOIN syscolumns c2
ON f.rkeyid = c2.[id]
AND f.rkey = c2.colid

Open in new window

LVL 6
anushahannaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Hope this provides you with the required info:

select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k_murli_krishnaCommented:
rrejan17 is correct. You can try a more generic one:

select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;
0
anushahannaAuthor Commented:
k_murli_krishna, do you see benefit adding INFORMATION_SCHEMA.TABLE_CONSTRAINTS instead of INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS in the code below?
SELECT 
OBJECT_NAME(f.constid) AS 'ForeignKey',
UNIQUE_CONSTRAINT_SCHEMA+'.'+OBJECT_NAME(f.rkeyid) AS 'PKTable', 
CONSTRAINT_SCHEMA+'.'+OBJECT_NAME(f.fkeyid) AS 'FKTable',
UNIQUE_CONSTRAINT_NAME as PrimaryKey
FROM sysforeignkeys f
INNER JOIN syscolumns c1
ON f.fkeyid = c1.[id]
AND f.fkey = c1.colid
INNER JOIN syscolumns c2
ON f.rkeyid = c2.[id]
AND f.rkey = c2.colid
LEFT OUTER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
ON OBJECT_NAME(f.constid)=CONSTRAINT_NAME

Open in new window

0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
>> do you see benefit adding INFORMATION_SCHEMA.TABLE_CONSTRAINTS  instead of INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS in the code below?

TABLE_CONSTRAINTS will contain information about the Constraint Name along with Table name whereas REFERENTIAL_CONSTRAINTS view would provide you information about the referenced table name as you are finding it..
0
anushahannaAuthor Commented:
Good point, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.