Subject | Foreign Key with Orphans? |
---|---|
Author | Scott Morgan |
Post date | 2008-12-02T17:06:33Z |
Is it possible to have a FK child table where some of the rows FK values
are not present in the parent table?
For example...
CREATE TABLE PARENT
(
KEY INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(10)
);
CREATE TABLE CHILD
(
FKEY INTEGER NOT NULL,
NAME VARCHAR(10),
CONSTRAINT FK_PARENT
FOREIGN KEY (FKEY)
REFERENCE PARENT(KEY)
ON UPDATE CASCADE
);
INSERT INTO PARENT (KEY, NAME) VALUES(1, 'Test');
INSERT INTO CHILD (FKEY, NAME) VALUES(1, 'Child');
/* okay */
INSERT INTO CHILD (FKEY, NAME) VALUES(123, 'Orphan');
/* fails, but I'd like it to work, the FKEY values would be unique and
not conflict with parent key values */
Scott
are not present in the parent table?
For example...
CREATE TABLE PARENT
(
KEY INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(10)
);
CREATE TABLE CHILD
(
FKEY INTEGER NOT NULL,
NAME VARCHAR(10),
CONSTRAINT FK_PARENT
FOREIGN KEY (FKEY)
REFERENCE PARENT(KEY)
ON UPDATE CASCADE
);
INSERT INTO PARENT (KEY, NAME) VALUES(1, 'Test');
INSERT INTO CHILD (FKEY, NAME) VALUES(1, 'Child');
/* okay */
INSERT INTO CHILD (FKEY, NAME) VALUES(123, 'Orphan');
/* fails, but I'd like it to work, the FKEY values would be unique and
not conflict with parent key values */
Scott