I'm working on an assignment which wants me to write some referential integrity constraints on a table. It asks me to ensure that every battle in Outcomes is also mentioned in Battles and to handle all violations by setting the referencing value to NULL.
I'm working with the following schemas
Battles(
name VARCHAR(255) PRIMARY KEY,
date VARCHAR(255),
)
Outcomes(
ship VARCHAR(255),
battle VARCHAR(255),
RESULT VARCAHR(255),
PRIMARY KEY(ship, battle)
)
I'm trying to add the foregin key like this:
alter table outcomes
add foreign key (battle)
references battles(name)
on delete set null
on update cascade;
I get the following error:
SQL error:
ERROR: insert or update on table "outcomes" violates foreign key constraint "outcomes_battle_fkey"
DETAIL: Key (battle)=(Pearl Harbor
I know this is because "Pearl Harbor" is in OUTCOMES but not in BATTLES, what I want to know is if there is some way to ignore that fact and set the foreign key anyway?