If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > how to check for R/I violation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-09, 23:26
rdba rdba is offline
Registered User
 
Join Date: Aug 2009
Posts: 30
how to check for R/I violation

I want to add an R/I to two tables - one table will be parent and one child

parent table - department
child table - employee

How can I check if there are any rows that would violate this R/I? Rows that exist in the child table (employee) with no corresponding row in the parent table (department)
Reply With Quote
  #2 (permalink)  
Old 09-11-09, 01:54
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Code:
SELECT *
  FROM employee e
 WHERE NOT EXIXTS
       (SELECT *
          FROM department d
         WHERE d.deptno = e.workdept);
You can also use NOT IN predicate.
Reply With Quote
  #3 (permalink)  
Old 09-11-09, 09:39
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Almost correct.

Code:
SELECT *
  FROM employee e
 WHERE NOT EXISTS
       (SELECT *
          FROM department d
         WHERE d.deptno = e.workdept)
         and e.workdept is not null;

NULL values do not violate RI.

Andy
Reply With Quote
  #4 (permalink)  
Old 09-11-09, 12:42
rdba rdba is offline
Registered User
 
Join Date: Aug 2009
Posts: 30
Can I use EXCEPT in place of NO EXISTS? Which one performs better?
Reply With Quote
  #5 (permalink)  
Old 09-11-09, 13:13
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
NOT EXISTS will perform better.

Andy
Reply With Quote
  #6 (permalink)  
Old 09-11-09, 14:27
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
You can use also:

Quote:
SELECT *
FROM employee e
Left Join
department d
On d.deptno = e.workdept

Where d.deptno is Null
with the same result.

For better understand how it works you can play with sample:

Quote:
select emp.*, dpt.dept
from
(
select 2 empid, 1 dept from sysibm.sysdummy1
union all
select 3 empid, 5 dept from sysibm.sysdummy1
union all
select 4 empid, 7 dept from sysibm.sysdummy1 ) emp
Left Join
(
select 1 dept from sysibm.sysdummy1
union all
select 7 dept from sysibm.sysdummy1 ) dpt

on emp.dept = dpt.dept

where dpt.dept is null
First time execute without "where dpt.dept is null", then execute whole query.

Lenny
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On