Fastest way to find-out/understand relationships between tables in MS SQL Database
Hi guys,
For most of the BI developers, getting BI related tasks (SSAS, SSIS, SSRS etc.) from multiple teams in company is very common. So in this situation, developer completes a task(s) for one team and goes to another team for different task(s). Once you go to the team for their requirements … lets say for few SSRS reports development or ELT Jobs, you will realize that, they neither have time to give you the proper knowledge transfer nor they could explain the requirements up to the mark how we wanted know in detail. And you have to give the estimate of your work, plan etc.
Well, so the first thing as for a BI developer to know is the database design, team is using and relationships between tables. Here I want to share my 2 favorite ways to find-out the table relationships in database.
- Database Diagram
- Reverse Engineer Database using Visio
Ways to use these options:
Database Diagram
- Expand your database tree in SQL Server Management Studio (IDE).
- Right click âDatabase Diagramsâ and Select âNew Database Diagramâ
- You will see a dialog-box which lists all the available tables in selected database. Select all/few tables and click Add.
- Thatâs it, it will generate database diagram. You can use the right-bottom button to navigate the diagram if itâs too big to fit in your screen.
Reverse Engineer Database using Visio
- Open Microsoft Office Visio. Select âFileâ menu -> âNewâ -> âSoftware and Databaseâ -> âDatabase Model Diagram (Metric)â.
- Let the empty sheet load. Select âDatabaseâ menu -> âReverse Engineerâ
- This will pop-up the âReverse Engineer Wizardâ. Choose âGeneric OLE DB Providerâ in âInstalled Visio driversâ category. (If you donât see this value then use âSetup..â button)
- Click âNextâ. You will see âData Link Propertiesâ dialog box. Select âMicrosoft OLE DB Provider for SQL Serverâ in Provider. And Click Next.
- In âConnectionâ tab, provide your Server Name, Database Name and required credentials. Test the connection for verification.
Tip: If you have many databases in your server instance, better write the database name in point number 3. Or copy-paste databaseâs name here. Otherwise it will query SMO objects to dynamically find-out how many databases are there in your selected Server instance. And this could take a whileJ. Same thing applies to Point number 1. If you have many database server/instances, then you better copy-paste the name rather than selecting it from the dropdown list. Sometimes it also hangs in cases of network travelling, server not responding etc.
- Then click âFinishâ and you will see âReverse Engineer Wizardâ dialog box again. Here you select everything except Views and Stored Procedures. Because we need only database diagram. Click Next.
- Hit âSelect Allâ button to select all the tables. (If you want to check relationships between only particular tables then select only them.) Click Next.
- Select first radio button which says âYes, add the shapes to the current pageâ. (You can also select the second one and customize the designed diagram your way, but here our motive is to fasten the process to know the table relationships). Click Finish.
- You will see following screen with the database diagram you wanted.
I am pretty sure, there lots of ways, but believe me these are very quick ways. We can also write custom SQL queries based on our personal choice (how we want to know and see the information about database) and re-use that SQL queries whenever required.
You can also check this query for your reference: http://blog.namwarrizvi.com/?p=232
Or My Query đ
Use <Your Database Name>
Go
SELECT
OBJECT_NAME(RKEYID) PARENT_TABLENAME,
SC1.NAME FOREIGNKEY_COLUMNNAME,
OBJECT_NAME(FKEYID) CHILD_TABLENAME,
SC2.NAME REF_KEYCOL,
OBJECT_NAME(CONSTID) FOREIGNKEY_NAME
FROM
SYS.SYSFOREIGNKEYS SFK
LEFT OUTER JOIN
SYS.SYSCOLUMNS SC1
ON ( SFK.FKEYID = SC1.ID AND SFK.FKEY = SC1.COLID )
INNER JOIN
SYS.SYSCOLUMNS SC2
ON ( SFK.RKEYID = SC2.ID AND SFK.RKEY = SC2.COLID )
ORDER BY PARENT_TABLENAME, CHILD_TABLENAME
Go
Hope you will find this useful.
Thanks, Khilit
One of the best tips I know so far on blogs. Thank you very much sir.
Wow, the second one is very nice method. Thank you very much.
Tim
KHILIT…….you are awesome!!!!!!!! I was freaking out at my first assigment at a new job which had a sample report 200 lines of sql code and like you said looks like nobody has time to teach you basic table relationships. THANK YOUUUUUUU so much!!!!!! hope these tips will help me……I think they will…