hi guys, how can i set null references when deleti...
# sql
c
hi guys, how can i set null references when deleting a foreign key?
o
you can alter the constraint on the column where the foreign key reference is used to cascade the value to null
something like this:
Copy code
sql
ALTER TABLE some_table
  DROP CONSTRAINT some_constraint, 
  ADD  CONSTRAINT some_constraint FOREIGN KEY (the_key) REFERENCES another_table(the_foreign_key) ON DELETE SET NULL;
on delete set null
c
thanks, ill check it
i dont really get the constraint part, is it mandatory?
o
if you want to set to null, yes... the default one is ~~`restrict` ~~
NO ACTION
which will not allow the foreign key to be deleted
you can add multiple behaviors too for instance:
Copy code
sql
ON UPDATE CASCADE ON DELETE SET NULL -- or SET DEFAULT .. etc
c
ok, thank you!