V
V
Vladimir Galynsky2016-02-22 13:27:03
PostgreSQL
Vladimir Galynsky, 2016-02-22 13:27:03

How to organize relationships between tables in PostgreSQL?

Hello. There are two tables Log and Stations. As you understand, I write logs of the machine in the Log table, the machines and their parameters are located in Stations. How to do it better. The task is to display the logs of the machine and plus the name, that is, description, which is located in the Stations table. I think this option can be compiled using JOIN probably. The second option is to create a Description field in the Log table that will be tied to Stations.description. But here's the problem. With this approach, the Description field should be filled in automatically from Stations depending on the Station ID. Actually, I've probably confused you. But what do you suggest to do it right? I think this is a standard situation, just that I have never encountered.
tablelog :

{
"LOG": {
    "id": "INTEGER AUTO_INCREMENT",
    "id_station": "INTEGER",
    "message": "VARCHAR(255)",
    "type": "VARCHAR(12)",
    "date": "TIMESTAMP"
  }
}

Table Stations:
{
"STATIONS": {
    "id": "INTEGER AUTO_INCREMENT",
    "id_station": "INTEGER",
    "description": "VARCHAR(50)",
    "latitude": "FLOAT",
    "longitude": "FLOAT"
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2016-02-22
@SaNNy32

Link the Log and Stations tables by the id_station field. The Description field is a description of the station, stored in the Stations table. When requesting data from the log, attach the Stations table and take the description from there.

SELECT L.*, S.Description FROM Log as L LEFT JOIN Stations as S ON L.id_station = S.id_station WHERE S.id_station = 5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question