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 > inserting with foreign keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-30-11, 05:39
super_mpk super_mpk is offline
Registered User
 
Join Date: May 2011
Posts: 15
inserting with foreign keys

hi all,

I want to insert data into the child table before inserting into parent table.
but I want to do this into the insert statement.
Is there any option available with the insert statement.

please guide me.......
Reply With Quote
  #2 (permalink)  
Old 05-30-11, 06:12
dr_te_z dr_te_z is offline
Registered User
 
Join Date: Jan 2009
Location: Zoetermeer, Holland
Posts: 555
You want an inconsistent database? Drop the FK and re-create when the parent rows are in-place.
Reply With Quote
  #3 (permalink)  
Old 05-30-11, 06:34
super_mpk super_mpk is offline
Registered User
 
Join Date: May 2011
Posts: 15
thanks for your reply..

this solution i tried but i want to know if any option available with insert statement so that the constraints will be unchecked.
Reply With Quote
  #4 (permalink)  
Old 05-30-11, 06:45
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
You are looking for deferrable constraints, but that is not supported by DB2
Reply With Quote
  #5 (permalink)  
Old 05-30-11, 07:51
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
You could define NOT ENFORCED foreign keys - they will be used by the optimizer but you will have to ensure integrity of data yourself, and there is a chance that your query results will not look as you would expect if you screw up.

Check informational constraints in the manual.
Reply With Quote
  #6 (permalink)  
Old 05-30-11, 11:13
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Although, this example might not satisfy the OP's requirements,
here is an example inserting child and inserting parent(if it was necessary) in a statement.
(Insert into parent table was specified explicitly.)

CREATE tables for test:
Code:
------------------------------ Commands Entered ------------------------------
CREATE TABLE test_fk_parent
( pk   INTEGER     NOT NULL PRIMARY KEY
, desc VARCHAR(20)
);
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE TABLE test_fk_child
( pk1  INTEGER     NOT NULL
, pk2  INTEGER     NOT NULL
, desc VARCHAR(30)
, PRIMARY KEY(pk1 , pk2)
, FOREIGN KEY(pk1)
    REFERENCES test_fk_parent(pk)
);
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.
INSERT first child row:
Code:
------------------------------ Commands Entered ------------------------------
WITH
 insert_data(pk1 , pk2 , desc) AS (
VALUES (100 , 1 , 'First child of a parent.')
)
, inter_result1 AS (
SELECT *
 FROM  FINAL TABLE
       (INSERT INTO test_fk_parent
             ( pk )
        SELECT DISTINCT
               pk1
         FROM  insert_data n
         WHERE NOT EXISTS
               (SELECT 0
                 FROM  test_fk_parent e
                 WHERE e.pk = n.pk1
               )
       ) p
)
, inter_result2 AS (
SELECT *
 FROM  FINAL TABLE
       (INSERT INTO test_fk_child
        SELECT *
         FROM  insert_data
       ) c
)
SELECT *
 FROM  inter_result1 r1
 RIGHT OUTER JOIN
       inter_result2 r2
   ON  r2.pk1 = r1.pk
;
------------------------------------------------------------------------------

PK          DESC                 PK1         PK2         DESC                          
----------- -------------------- ----------- ----------- ------------------------------
        100 -                            100           1 First child of a parent.      

  1 record(s) selected.
INSERT second child row:
Code:
------------------------------ Commands Entered ------------------------------
WITH
 insert_data(pk1 , pk2 , desc) AS (
VALUES (100 , 2 , 'Second child of a parent')
)
, inter_result1 AS (
SELECT *
 FROM  FINAL TABLE
       (INSERT INTO test_fk_parent
             ( pk )
        SELECT DISTINCT
               pk1
         FROM  insert_data n
         WHERE NOT EXISTS
               (SELECT 0
                 FROM  test_fk_parent e
                 WHERE e.pk = n.pk1
               )
       ) p
)
, inter_result2 AS (
SELECT *
 FROM  FINAL TABLE
       (INSERT INTO test_fk_child
        SELECT *
         FROM  insert_data
       ) c
)
SELECT *
 FROM  inter_result1 r1
 RIGHT OUTER JOIN
       inter_result2 r2
   ON  r2.pk1 = r1.pk
;
------------------------------------------------------------------------------

PK          DESC                 PK1         PK2         DESC                          
----------- -------------------- ----------- ----------- ------------------------------
          - -                            100           2 Second child of a parent      

  1 record(s) selected.

Result(rows in tables):
Code:
------------------------------ Commands Entered ------------------------------
SELECT * FROM test_fk_parent;
------------------------------------------------------------------------------

PK          DESC                
----------- --------------------
        100 -                   

  1 record(s) selected.


------------------------------ Commands Entered ------------------------------
SELECT * FROM test_fk_child;
------------------------------------------------------------------------------

PK1         PK2         DESC                          
----------- ----------- ------------------------------
        100           1 First child of a parent.      
        100           2 Second child of a parent      

  2 record(s) selected.

Last edited by tonkuma; 05-30-11 at 20:16. Reason: Minor edit of code. Add DISTINCT
Reply With Quote
  #7 (permalink)  
Old 05-30-11, 17:11
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
It's also possible to switch an existing constraint to be NOT ENFORCED and back.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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