|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 9:39 PM
Points: 54,
Visits: 186
|
|
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
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
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---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
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
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 9:39 PM
Points: 54,
Visits: 186
|
|
| 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.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
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
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 9:39 PM
Points: 54,
Visits: 186
|
|
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
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
| is your Person_Id column is "identity column" in Primary table
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 9:39 PM
Points: 54,
Visits: 186
|
|
| Person_Id field created with uniqueidentifier
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
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
|
|
|
|