join tables in sql without primary keys or foreign keys common to the 2 tables

-2

I have to do a SQL query on several tables.

I need to relate the two through a common "id" but I do not know how I do not have declared primary keys or foreign keys.

Follow the code snippet:

SELECT a.iban, TO_CHAR(date_, 'month ,yyyy-dd') AS "Opening date", o.client_id AS "Client NR.", op.status
FROM operation o JOIN open_operation op ON o.id_op = op.id
                 JOIN account a ON o.account_id = a.id   
                 JOIN client c ON o.client_id = c.id 

The following information needs to be included in the previous query:

SELECT y.name 
FROM agency y JOIN account a ON y.id = a.agency_id
    
asked by anonymous 28.11.2017 / 18:09

2 answers

0

Even though there are no declared keys, you have described these fields as follows:

agency.id = account.agency_id

So a simple JOIN would solve, depending on the result you want a subselect might be needed.

SELECT a.iban
   , TO_CHAR(date_, 'month ,yyyy-dd') AS "Opening date"
   , o.client_id AS "Client NR.", op.status
   , y.name
FROM operation o 
JOIN open_operation op 
   ON o.id_op = op.id
JOIN account a 
   ON o.account_id = a.id   
JOIN client c 
   ON o.client_id = c.id 
JOIN agency y
   ON y.id = a.agency_id
    
28.11.2017 / 18:25
0

The definition of primary key and foreign key is not necessary. They are a definition that limits certain actions of the system.

Although not advised , you can technically have a database with no key defined.

You can, for example, be defining relationships in queries only. It's a matter of approach.

For example, the attribute X can represent a relation for 2 or more tables, although it is little used, or even for different attributes of the same table in different situations.

It's all a matter of approach.

    
12.12.2017 / 16:51