Try OpenEdge Now
skip to main content
SQL Development
OpenEdge SQL Data Definition Language : Maintaining data integrity : Handling cycles in referential integrity : Inserting rows in a cycle
 
Inserting rows in a cycle
insert rows into tables that form a cycle:
1. Insert rows into one of the tables that forms the cycle, with NULL values in the foreign key columns. If the foreign key is NULL, the database does not check for a match between the foreign key and the corresponding primary key. The insert succeeds. This is the referencing table.
2. Update or insert the values in the primary keys of the second, referenced table.
3. Update the foreign key values of the previous table, the referencing table.
The following example shows how to insert or update values into the employee table. This table forms a single-table cycle. First insert NULL into the mgr_code column. After you insert rows, update the values of the mgr_code column.
CREATE TABLE employee (
empno INTEGER NOT NULL PRIMARY KEY,
ename CHAR (30) NOT NULL,
deptno INTEGER NOT NULL,
mgr_code INTEGER REFERENCES employee (empno)
) ;
INSERT INTO employee VALUES (100, 'JOHN', 10, NULL) ;
INSERT INTO employee VALUES (500, 'MARY', 30, NULL) ;
INSERT INTO employee VALUES (101, 'ANITA', 10, NULL) ;
INSERT INTO employee VALUES (501, 'ROBERT', 30, NULL) ;
UPDATE employee set mgr_code = 101 where empno = 100 ;
UPDATE employee set mgr_code = 501 where empno = 500 ;
/*
** Anita is John's manager.
** John's employee row references Anita's employee row.
** Robert is Mary's manager.
** Mary's employee row references Robert's employee row.
** The mgr_code is still NULL in Anita's row and in Robert's row.
** To set the mgr_code in Anita's row and Robert's row:
** 1. Insert rows for Anita's manager and Robert's manager
** 2. Update Anita's row and Robert's row
*/