Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 322
progreccor Avatar asked Jan 29 '16 10:01

progreccor


3 Answers

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.

like image 82
Andrew Avatar answered Nov 16 '22 16:11

Andrew


Try this small SQL script which creates 3 tables. I think you will find this work well.

CREATE TABLE product (
    category INT NOT NULL, id INT NOT NULL,
    price DECIMAL,
    PRIMARY KEY(category, id)
);

CREATE TABLE customer (
    id INT NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE product_order (
    no INT NOT NULL AUTO_INCREMENT,
    product_category INT NOT NULL,
    product_id INT NOT NULL,
    customer_id INT NOT NULL,

    PRIMARY KEY(no),
    INDEX (product_category, product_id),
    INDEX (customer_id),

    FOREIGN KEY (product_category, product_id)
      REFERENCES product(category, id)
      ON UPDATE CASCADE ON DELETE RESTRICT,

    FOREIGN KEY (customer_id)
      REFERENCES customer(id)
)   ;

enter image description here

like image 21
Tim Seed Avatar answered Nov 16 '22 15:11

Tim Seed


Generally: from the context menu or by pressing Ctrl+Alt+U.

If you have found this picture, one more step was to go deeper in the website and you would get to this page: https://www.jetbrains.com/datagrip/features/other.html

And there is an explanation how to do it.

like image 2
moscas Avatar answered Nov 16 '22 17:11

moscas