Solved

Sql to get orphans

Posted on 2016-08-31
7
54 Views
Last Modified: 2016-09-01
Hi Experts,

The database is Oracle 11g
I have table Parent  with column  ( ID ,name)
 The table Child has column ChildID , ParentID  ,Name
 
sample data
Parent
    P1   Parent1
     P2  Parent2

Child
   C1 P1  Child1
   C2  P1  Child2
   C3  P3  Child3

it is expected for each ChildID there has to be a corresponding
There are some  orphaned rows in the table Child  . In the sample data Child3 is an orphan because there is no P3 in Parent table

I need help in getting the Sql that can give the orphan children
0
Comment
Question by:Sam OZ
7 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 41777661
select
       c.*
from child c
left join parent p on c.ParentID = p.id
where p.id IS NULL

Open in new window

0
 

Author Comment

by:Sam OZ
ID: 41777690
Thanks .  Infact in my table it is p.UID instead of p.ID    For some reason , p.UID throws error ( Because UID is a reserved word it looks ( Even select p.UID from parent p throws error)
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41777772
What is the exact error message?
Is p.uid an integer?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Expert Comment

by:johnsone
ID: 41778108
You would need to put it in double quotes:

select p."UID" from parent p

The double quotes makes is case sensitive, so make sure it is upper case.

This is a total design flaw, you should never have column names with reserved words.

An alternate way to find duplicates:

Create an exception table by running this in SQL*Plus:
@?/rdbms/admin/utlexcpt

Then create a referential integrity constraint (something you should have to prevent this in the first place):
alter table child add foreign key (id) references parent("UID") exceptions into exceptions;

Then you can get all the invalid rows with this:
select * from child where rowid in (select row_id from exceptions);

Let Oracle do the work for you.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41778136
Do you have foreign key constraints on the child tables?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41778861
UID returns an integer that uniquely identifies the session user (the user who logged on).
https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions211.htm

certainly not a good idea to use that as a column name
0
 
LVL 8

Assisted Solution

by:Ghunaima
Ghunaima earned 250 total points
ID: 41780292
select * from child where parentid not in (select p."UID" from Parent p)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
levels for reporting 5 65
PL/SQL LOOP CURSOR 3 58
File generation using utl_file 4 46
query in Oracle forms Builder 2 42
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now