How could I create relationships between tables in JetBrains' DataGrip?

progreccor picture progreccor · Jan 29, 2016 · Viewed 17.9k times · Source

I am using DataGrip by JetBrains in my work. It is ok, but I don't know how to create relationships between tables like in this picture:

enter image description here

Answer

Andrew picture Andrew · May 12, 2017

It's a two step procedure. In the first step, you must modify your table to add foreign key constraint definitions. In the second step, you can display the table diagram.

First, right click on the name of your table in DataGrip, then choose Modify Table. You will see four tabs: Columns, Keys, Indices, and Foreign Keys. Choose the Columns tab. Right click on the column name you want to become a foreign key, and choose New Foreign Key. The window will switch to its Foreign Keys tab with some information filled in. Fill in your "target table". You may also have to write in the target column name in the SQL statement's REFERENCES phrase. Review all the information now in the Modify Table window, and, when satisfied, click "Execute".

Second, right click again on the name of your table in DataGrip, and this time choose Diagrams > Show Visualisation. You should now see a diagram displaying the relations between your original table and the referenced tables.

In DataGrip Help, you can look at the Working with the Database Tool Window page for its Modifying the definition of a table, column, index, or a primary or foreign key section. There is a very short procedure description, there.

Wikipedia has an example in its Defining foreign keys article section that may be useful to you while working in DataGrip's Modify Table window.

I did this procedure in DataGrip 2017.1.3, and I don't know whether other versions vary.