Link to home
Start Free TrialLog in
Avatar of Pau Lo
Pau Lo

asked on

SQL Sever table relationships query

We have a complex DB with a large number of tables. I am trying to write a query to pull data from numerous tables, but as there is no real documentation for the DB design its tricky... is it possible to query a specific table to determine any relationships/foreign keys estbalished betwee the table and other tables in that DB? if so can you provide any syntax? I only have db_datareader permissions if that is an issue
ASKER CERTIFIED SOLUTION
Avatar of plusone3055
plusone3055
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pau Lo
Pau Lo

ASKER

thanks

I presume '.' is where I specify our DB schema?
Hae?

Just run the query.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And these
SELECT
DISTINCT
Constraint_Name AS [Constraint],
Table_Schema AS [Schema],
Table_Name AS [TableName]
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
GO

Method 2: sys.objects

SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects 
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT','PRIMARY_KEY_CONSTRAINT')
GO

Open in new window

pma111, do you still need help with this question?