I have a country and let’s say user tables, and in case a submitted country does not match a constraint I want a null go there instead.
create table countries(
ccode char(2), primary key (ccode)
);
create table users(
ccode char(2), foreign key (ccode) references countries(ccode)
);
insert into countries values('ru'),('us'),('hk');
Now if the user submitted an nonexistent code, eg:
insert into users values('xx');
- The query would actually fail due to the fk constaint. But I want it to write a null instead. How can we do this?
Answering my own question it looks like this is the way to do it:
(But if you suggest a better answer I will accept it of course)
^ This will return a code, and a null if it didn’t match – which is what I want.