Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to find relationship between tables Expand / Collapse
Author
Message
Posted Wednesday, October 24, 2012 10:51 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:12 PM
Points: 60, Visits: 255
Hi,

we are having more than 3000 tables in our DB, But none of the tables are designed with foreign key relationships.
Is there anyway we can find relationship between tables without having foreign Key, Please share your thoughts..

Regards,
Tony
Post #1376769
Posted Thursday, October 25, 2012 1:28 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
Hi,
I havnt got your question fully but i assume that you want to find tables that "can have relation
base on the column name"

Like Parent table having USERID as primary key and some other table having USERID normal column
than it may have relation between them and you want those tables

if above which i assume is same then
this query might help you

i havent check it for 3000 table but for 10 it's working

;WITH CTE AS
(
select TAB.object_id,TAB.name,COL.name AS COLNAME,COL.column_id,COL.is_identity
from
sys.tables TAB
INNER JOIN sys.columns COL ON TAB.object_id=COL.object_id
)
SELECT
Child.object_id as 'Child Objectid'
,Child.name as 'Child TableName'
,Child.COLNAME as 'Child ColumnName'
,Parent.object_id as 'Parent Objectid'
,Parent.name as 'Parent TableName'
,Parent.COLNAME as 'Parent ColumnName'
FROM
cte Child
INNER JOIN CTE Parent
ON Child.COLNAME=Parent.COLNAME AND Child.name<>Parent.name AND Child.is_identity+1=Parent.is_identity
Post #1376812
Posted Thursday, October 25, 2012 1:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
yeshupandit_2002 (10/25/2012)
Hi,
I havnt got your question fully but i assume that you want to find tables that "can have relation
base on the column name"

Like Parent table having USERID as primary key and some other table having USERID normal column
than it may have relation between them and you want those tables

if above which i assume is same then
this query might help you

i havent check it for 3000 table but for 10 it's working

;WITH CTE AS
(
select TAB.object_id,TAB.name,COL.name AS COLNAME,COL.column_id,COL.is_identity
from
sys.tables TAB
INNER JOIN sys.columns COL ON TAB.object_id=COL.object_id
)
SELECT
Child.object_id as 'Child Objectid'
,Child.name as 'Child TableName'
,Child.COLNAME as 'Child ColumnName'
,Parent.object_id as 'Parent Objectid'
,Parent.name as 'Parent TableName'
,Parent.COLNAME as 'Parent ColumnName'
FROM
cte Child
INNER JOIN CTE Parent
ON Child.COLNAME=Parent.COLNAME AND Child.name<>Parent.name AND Child.is_identity+1=Parent.is_identity
i havent checked your query as i dont have sql environment right now but how would you decide which table will act as parent or child and how the different column's name columns will get foreign key level match here. certainly here manual intervention plus ER logic required


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1376820
Posted Thursday, October 25, 2012 1:47 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
Ok,column which is having identity will be parent table and second one will act as as child
and yes if column name is same both tables then only query will work else need to do some manual work
Post #1376826
Posted Thursday, October 25, 2012 3:19 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:12 PM
Points: 60, Visits: 255
Still this query doesn't work, it is not pulling the common field name from all tables and just pulling few field and tables.. Please need more help on this.
Post #1376858
Posted Thursday, October 25, 2012 3:43 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
can you tell me more in detail whats happening and what is output when you run
few field and few tables means?
if its fine to you can you share some of your table and its column
so i can check
Post #1376871
Posted Thursday, October 25, 2012 4:07 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:12 PM
Points: 60, Visits: 255
Hi,

Please find the following few tables for your reference

--Person
Enterpriseid
Person_Id
LastName
FirstName


--Person_Relationship
Person_Id
Relations_Code
CreatedDate

--Person_Employers
Person_Id
Employee_id
Occuapation

Person,Person_Relationship and Person_Employers are tables.
The common field is Person_Id, But there is no Foreign Key relationship. Like this i have 3000 tables without having Foreign Key relationship

Is there any way can we get all tables relationship through Person_Id Field? like this we have lot of common field names.

Using this i want to find which all tables are linked without foreign Key relationship.

Regards,
tony
Post #1376882
Posted Thursday, October 25, 2012 4:16 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
is your Person_Id column is "identity column" in Primary table
Post #1376887
Posted Thursday, October 25, 2012 4:38 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 10:12 PM
Points: 60, Visits: 255
Person_Id field created with uniqueidentifier
Post #1376900
Posted Thursday, October 25, 2012 4:51 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
try this and let me know
Line "and Parent.user_type_id =36" is for uniqueidentifier
and
Line "AND Child.is_identity+1=Parent.is_identity"(which is comment in this query) is for column which is
identity column in primary table

run this and let me know result


;WITH CTE AS
(
select TAB.object_id,TAB.name,COL.name AS COLNAME,COL.column_id,COL.is_identity ,col.user_type_id
from
sys.tables TAB
INNER JOIN sys.columns COL ON TAB.object_id=COL.object_id
)
SELECT
Child.object_id as 'Child Objectid'
,Child.name as 'Child TableName'
,Child.COLNAME as 'Child ColumnName'
,Parent.object_id as 'Parent Objectid'
,Parent.name as 'Parent TableName'
,Parent.COLNAME as 'Parent ColumnName'
FROM
cte Child
INNER JOIN CTE Parent
ON Child.COLNAME=Parent.COLNAME AND Child.name<>Parent.name
--AND Child.is_identity+1=Parent.is_identity
and Parent.user_type_id =36


Post #1376909
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse