Automatically set value on child rows when deleting parent row?
create table tree_data 
(
   id integer primary key, 
   code text, 
   name text, 
   parent_id integer,
   constraint fk_parent 
      foreign key (parent_id) 
      references tree_data(id)
      on delete set null
);


SQLFiddle example: http://sqlfiddle.com/#!15/b9a62/1