drop trigger if exists `trig_create_user_stat`;
create trigger `trig_create_user_stat` 
    after insert on user
    for each row begin
    insert into user_stat (`user_id`, `user_limit`) values(NEW.user_id, NEW.user_limit);
end;

drop trigger if exists `trig_delete_user_stat`;
create trigger `trig_delete_user_stat` 
    after delete on user
    for each row begin
    delete from user_stat where user_id=OLD.user_id;
end;

drop trigger if exists `trig_update_stat_book_ins`;
create trigger `trig_update_stat_book_ins` 
    after insert on book 
    for each row begin
    update user_stat set user_bookcount=user_bookcount+1 where user_stat.user_id=NEW.user_id;
end;


drop trigger if exists `trig_update_stat_book_del`;
create trigger `trig_update_stat_book_del` 
    after delete on book 
    for each row begin
    update user_stat set user_bookcount=user_bookcount-1 where user_stat.user_id=OLD.user_id;
end;


drop trigger if exists `trig_update_stat_doc_ins`;
create trigger `trig_update_stat_doc_ins` 
    after insert on document 
    for each row begin
    update user_stat set user_doccount=user_doccount+1 where user_stat.user_id=NEW.user_id;
    update user_stat set user_usedspace=user_usedspace+NEW.doc_size where user_stat.user_id=NEW.user_id;
end;


drop trigger if exists `trig_update_stat_doc_del`;
create trigger `trig_update_stat_doc_del` 
    after delete on document 
    for each row begin
    update user_stat set user_doccount=user_doccount-1 where user_stat.user_id=OLD.user_id;
    update user_stat set user_usedspace=user_usedspace-OLD.doc_size where user_stat.user_id=OLD.user_id;
end;


drop trigger if exists `trig_update_stat_note_ins`;
create trigger `trig_update_stat_note_ins` 
    after insert on note
    for each row begin
    update user_stat set user_notecount=user_notecount+1 where user_stat.user_id=NEW.user_id;
end;


drop trigger if exists `trig_update_stat_note_del`;
create trigger `trig_update_stat_note_del` 
    after delete on note
    for each row begin
    update user_stat set user_notecount=user_notecount-1 where user_stat.user_id=OLD.user_id;
end;
