How to consult data from parent tables using foreign keys. oracle

2

I have a Staff table with the following structure:

create table PERSONAL
(
  personal_id NUMBER(8) not null,
  nombre      VARCHAR2(50),
  apellido    VARCHAR2(50),
)
alter table PERSONAL
  add constraint PK_ID primary key (PERSONAL_ID);

And a table of places in the following way:

create table LUGAR
(
  lugar_id    NUMBER(8) not null,
  desc_lugar  VARCHAR2(50),
  latitud     VARCHAR2(50),
  longitud    VARCHAR2(50)
)
alter table LUGAR
  add constraint LU_ID primary key (LUGAR_ID);

I also have a table that links the staff with the places:

create table LUGAR_PERSONA
(
  lugar_id    NUMBER(8) not null,
  personal_id NUMBER(8) not null
);
alter table LUGAR_PERSONA
  add constraint FK_lugar FOREIGN KEY (lugar_id) REFERENCES lugar(lugar_id)
  add constraint FK_personal FOREIGN KEY (personal_id) REFERENCES personal(personal_id);

Well the question is: How do I make a query to the Place table per person to bring me the related data of the personnel and places of the corresponding id?

Greetings

    
asked by Alexis Granja 03.02.2017 в 11:55
source

1 answer

3

You have to make a join by relating the tables by the common fields:

select 
    p.nombre, l.nombre 
from 
    personal p, lugar l, lugar_persona lp
where 
    l.lugar_id=lp.lugar_id
and 
    p.personal_id=lp.personal_id
    
answered by 03.02.2017 / 12:02
source