Populating Master-Detail Foreign Key Values Using Sequences
This article presents some safe methods for populating master-detail foreign key columns when using sequences.
oracle miscconfigurationintermediate
by OracleDba
13 views
This article presents some safe methods for populating master-detail foreign key columns when using sequences.
12345678910111213141516171819
CREATE TABLE orders (
id NUMBER(10),
description VARCHAR2(50),
CONSTRAINT orders_pk PRIMARY KEY (id)
);
CREATE SEQUENCE orders_seq;
CREATE TABLE order_lines (
id NUMBER(10),
order_id NUMBER(10),
description VARCHAR2(50),
CONSTRAINT order_lines_pk PRIMARY KEY (id),
CONSTRAINT orli_orde_fk FOREIGN KEY (order_id) REFERENCES orders(id)
);
CREATE INDEX orli_orde_fk_i ON order_lines(order_id);
CREATE SEQUENCE order_lines_seq START WITH 100;1234567891011121314151617181920212223242526272829303132
BEGIN
-- Populate the master table.
INSERT INTO orders (id, description)
VALUES (orders_seq.NEXTVAL, 'Dummy order description.');
-- Requery the master table to populate the FK link in the detail table.
INSERT INTO order_lines (id, order_id, description)
VALUES (order_lines_seq.NEXTVAL, (SELECT MAX(id) FROM orders), 'Dummy order line description');
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SQL> SELECT * FROM orders ORDER BY id;
ID DESCRIPTION
---------- --------------------------------------------------
1 Dummy order description.
1 row selected.
SQL> SELECT * FROM order_lines ORDER BY id;
ID ORDER_ID DESCRIPTION
---------- ---------- --------------------------------------------------
100 1 Dummy order line description
1 row selected.
SQL>12345678910111213141516171819202122232425262728293031323334
BEGIN
-- Use NEXTVAL to populate the master table.
INSERT INTO orders (id, description)
VALUES (orders_seq.NEXTVAL, 'Dummy order description.');
-- Use CURRVAL to populate the FK link in the detail table.
INSERT INTO order_lines (id, order_id, description)
VALUES (order_lines_seq.NEXTVAL, orders_seq.CURRVAL, 'Dummy order line description');
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SQL> SELECT * FROM orders ORDER BY id;
ID DESCRIPTION
---------- --------------------------------------------------
1 Dummy order description.
2 Dummy order description.
2 rows selected.
SQL> SELECT * FROM order_lines ORDER BY id;
ID ORDER_ID DESCRIPTION
---------- ---------- --------------------------------------------------
100 1 Dummy order line description
101 2 Dummy order line description
2 rows selected.
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243
DECLARE
l_order_id orders.id%TYPE;
BEGIN
-- Select the next sequence value.
SELECT orders_seq.NEXTVAL
INTO l_order_id
FROM dual;
-- Use the value to populate the master table.
INSERT INTO orders (id, description)
VALUES (l_order_id, 'Dummy order description.');
-- Reuse the value to populate the FK link in the detail table.
INSERT INTO order_lines (id, order_id, description)
VALUES (order_lines_seq.NEXTVAL, l_order_id, 'Dummy order line description');
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SQL> SELECT * FROM orders ORDER BY id;
ID DESCRIPTION
---------- --------------------------------------------------
1 Dummy order description.
2 Dummy order description.
3 Dummy order description.
3 rows selected.
SQL> SELECT * FROM order_lines ORDER BY id;
ID ORDER_ID DESCRIPTION
---------- ---------- --------------------------------------------------
100 1 Dummy order line description
101 2 Dummy order line description
102 3 Dummy order line description
3 rows selected.
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
DECLARE
l_order_id orders.id%TYPE;
BEGIN
-- Populate the master table, returning the sequence value.
INSERT INTO orders (id, description)
VALUES (orders_seq.NEXTVAL, 'Dummy order description.')
RETURNING id INTO l_order_id;
-- Use the returned value to populate the FK link in the detail table.
INSERT INTO order_lines (id, order_id, description)
VALUES (order_lines_seq.NEXTVAL, l_order_id, 'Dummy order line description');
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SQL> SELECT * FROM orders ORDER BY id;
ID DESCRIPTION
---------- --------------------------------------------------
1 Dummy order description.
2 Dummy order description.
3 Dummy order description.
4 Dummy order description.
4 rows selected.
SQL> SELECT * FROM order_lines ORDER BY id;
ID ORDER_ID DESCRIPTION
---------- ---------- --------------------------------------------------
100 1 Dummy order line description
101 2 Dummy order line description
102 3 Dummy order line description
103 4 Dummy order line description
4 rows selected.
SQL>
-- Create triggers to support autonumber functionality.
CREATE OR REPLACE TRIGGER orders_bir
BEFORE INSERT ON orders
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
SELECT orders_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/
CREATE OR REPLACE TRIGGER order_lines_bir
BEFORE INSERT ON order_lines
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
SELECT order_lines_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/
DECLARE
l_order_id orders.id%TYPE;
BEGIN
-- Populate the master table, returning the sequence value.
INSERT INTO orders (description)
VALUES ('Dummy order description.')
RETURNING id INTO l_order_id;
-- Use the returned value to populate the FK link in the detail table.
INSERT INTO order_lines (order_id, description)
VALUES (l_order_id, 'Dummy order line description');
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SQL> SELECT * FROM orders ORDER BY id;
ID DESCRIPTION
---------- --------------------------------------------------
1 Dummy order description.
2 Dummy order description.
3 Dummy order description.
4 Dummy order description.
5 Dummy order description.
5 rows selected.
SQL> SELECT * FROM order_lines ORDER BY id;
ID ORDER_ID DESCRIPTION
---------- ---------- --------------------------------------------------
100 1 Dummy order line description
101 2 Dummy order line description
102 3 Dummy order line description
103 4 Dummy order line description
104 5 Dummy order line description
5 rows selected.
SQL>Please to add comments
No comments yet. Be the first to comment!