473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sql getting orphan records


I'm working with mysql without referential itegrity.
Let me make some small example:

tableA
+-----------+---------------+
| id_1 | data_1 |
+-----------+---------------+
| 1 | bla1 |
| 2 | bla2 |
| 3 | bla3 |
+-----------+---------------+

tableB

+-----------+------------+
| id_2 | fk_1 |
+-----------+------------+
| 2_1 | 1 |
| 2_2 | 3 |
| 2_3 | 6 |
+-----------+------------+

Every record on tableA, should have at least 1 record on tableB, linked
trhough fk_1, but as times goes

on, and due users doing wrong things, there could be problems.

For instance, in the above example, there is one record in tableA
(id_1=2) that has no related record on

tableB , and there is one record in tableB (id_2=2_3) whose fk_1 value
doesn't exists on tableA

Now the question:
1) How could I select all records on tableA which has no related
records on tableB?
2) How could I select all records on tableB which has no related
records on tableA?

I've been tring with left and right joins, but I can't figure out how
to do in order to get just "orphan"

records instead full sets

regards - julian

Jun 30 '06 #1
1 3506
julian_m wrote:
I'm working with mysql without referential itegrity.
Let me make some small example:

tableA
+-----------+---------------+
id_1 | data_1 |

+-----------+---------------+
1 | bla1 |
2 | bla2 |
3 | bla3 |

+-----------+---------------+

tableB

+-----------+------------+
id_2 | fk_1 |

+-----------+------------+
2_1 | 1 |
2_2 | 3 |
2_3 | 6 |

+-----------+------------+

Every record on tableA, should have at least 1 record on tableB,
linked trhough fk_1, but as times goes

on, and due users doing wrong things, there could be problems.

For instance, in the above example, there is one record in tableA
(id_1=2) that has no related record on

tableB , and there is one record in tableB (id_2=2_3) whose fk_1 value
doesn't exists on tableA

Now the question:
1) How could I select all records on tableA which has no related
records on tableB?
2) How could I select all records on tableB which has no related
records on tableA?

I've been tring with left and right joins, but I can't figure out how
to do in order to get just "orphan"

records instead full sets

regards - julian


If you've got at least MySQL 4.1 then

SELECT * FROM tableA as a where NOT EXIST SELECT * FROM tableB as b WHERE
a.id_1 = b.fk_1

and vice versa for the other orphans

Alternatively for older versions of MySQL

SELECT * FROM tableA as a LEFT JOIN tableB as b ON a.id_1 = b.fk_1 WHERE
..bfk_1 IS NULL

and vice versa for the other orphans

For future reference, the comp.databases.mysql group is a better bet for
questions on MySQL. The
comp.lang.php group is best for questions on PHP!
Jun 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Thorne | last post by:
Have 2 tables ts_sheets : TimeSheet_ID TimeSheet_Date Emp_ID Orphan LineItem TimeSheet_ID Client
3
by: James | last post by:
I need to define paragraphs on my site that should not break across a page when printed, similar to widow/orphan control in Word. Is this possible to implement? We only use IE6 so no cross-platform...
7
by: NickName | last post by:
Hi, Say, a database has all the FK constraints created properly, would sql server 2000 save this kind of meta data into sysforeignkeys table? Or it depends on ...? I'm trying to find all the...
0
by: prabhupr | last post by:
Hi Folks In CS project, we use "Add Reference" option to make reference to other assemblies. At times, there a is a chance that few of them get added by mistake or must have got added for some...
1
by: Gunnar | last post by:
Are there any way of detecting that a program have crashed and left an orphan file? I have tried with the filesystemwatcher, but that didn't work. Maybe I can use a polling strategy and keep...
2
by: julian_m | last post by:
I'm working with mysql without referential itegrity. Let me make some small example: tableA +-----------+---------------+ | id_1 | data_1 | +-----------+---------------+ | 1 ...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Is there a way to create a registry key, but orphan it from the program that created it? Reason: Create a key, but not associate it with the...
2
by: alikhan707 | last post by:
Hey guys i need help on this problem .. How to solve zombie and orphan process problems ? Please explain with an example...
2
by: smilem | last post by:
Hi, I use Gallery2 v2.3 system on my web server. When I add and delete photos I see orphan records in my db. The project is opensource and developers are focused on new v3 of the product. However...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.