SELECT *
FROM req, osi
WHERE req.orc_key = osi.orc_key ;
mpaladugu
you will not get any syntax error or runtime error, if you join tables using foreign keys on both sides,
but you may not get the results you intended.
In your scenario ORC_KEY is FK on both tables which means both are referring to some third table and joining these tables REQ and OSI on FK is not going to give you logically correct results(unless you have some ad-hoc requirement)
to be logically as per RDBMS concepts, the query given by leoahmad is the right one, other wise use dportas query.
Since REQ_KEY column of OSI table has no values populated in it, your query may return Zero rows, to avoivd that you can use a outer join, in that case use my query
select r.req_key,r.orc_key,o.sch_key,o.orc_key
from req r, osi o
where r.req_key = o.req_key(+);
Further to mpaladugu's comment. My query is of course a guess based on the little information given here. I don't know the structure of your tables or which column references which so please test it out yourself.
I don't know your requirements either so I can't tell whether or not my query does or doesn't give the result you want. There is nothing in "RDBMS concepts" to say you can't join on ANY attributes(s) you like, whether foreign key or not.
dportas
mpaladugu, Good suggestion on using an outer join. But the legacy (+) syntax has been deprecated for years. I recommend using the standard OUTER JOIN syntax instead. It is more powerful and is understood by more people.
donvike1
ASKER
SELECT *
FROM req, osi
WHERE req.orc_key = osi.orc_key ;
I have the above as a full outer join, see my code at the bottom. All of the joins works (meaning I am able to extract data) prior to adding OSI (f), and the code will add the OSI fields but when I go to extract data from the OSI the rows come back empty. What am I doing wrong in joining the foreign key?
CREATE TABLE OIM_REPORTING.TEST_REQUEST_DETAILS AS
select r.req_key,r.orc_key,o.sch_key,o.orc_key
from req r, osi o
where r.req_key = o.req_key;
My req_key column is empty in osi, that is why I'm trying to use the orc_key to join on in req and osi.
TABLE REQ TABLE OSI
REQ_KEY PK SCH_KEY PK
ORC_KEY FK REQ_KEY FK (but this field is empty in this table)
ORC_KEY FK (this field is populated)
I they join, but I'm not getting data in the fields only nulls. I know the data is there.
dportas
It looks like you have the wrong column in your query. To join in orc_key use:
select r.req_key,r.orc_key,o.sch_key,o.orc_key
from req r, osi o
where r.req_key = o.orc_key;
or maybe:
select r.req_key,r.orc_key,o.sch_key,o.orc_key
from req r, osi o
where r.orc_key = o.orc_key;
Open in new window