Bogdan Lashkov
Bogdan Lashkov

Reputation: 359

How automatically create SQL Server relationship diagram?

I am new to SQL Server. On the previous job I used working with Postgres and MySQL. But now I was faced with the task connected with SQL Server. And I discovered very strange thing in the DB with that I should work. There don't exist any relations!

Is it normal to SQL Server? How can I automatically connect tables according to their primary keys? Any other ideas?

An screenshot of the ER diagram:

enter image description here

Upvotes: 2

Views: 2331

Answers (1)

Carlos Rodriguez
Carlos Rodriguez

Reputation: 61

Unless you are talking about creating hundreds of FKs you may be better off by just adding those relationships manually either through SSMS database diagram or through a sql script. If the number of possible relations is really large or you expect to have to do this again in the future, you may want to look to SMO (SQL Management Objects) and either use Powershell or a small C# program to script out the tsql that would join those tables. But you would need to make sure that there is a repeatable pattern / naming convention between the columns and tables that you can leverage. For Example:

Table1
ID
Name
Table2
ID
Table1ID
Name

Here you could consider Table1ID as a FK referencing Table1.ID

Upvotes: 1

Related Questions