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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hae?
Just run the query.
Just run the query.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
pma111, do you still need help with this question?
ASKER
I presume '.' is where I specify our DB schema?