Skip to content

Instantly share code, notes, and snippets.

@dnthanh98
Created December 4, 2018 02:51
Show Gist options
  • Save dnthanh98/80a2d607db230ee6e017e6310163d964 to your computer and use it in GitHub Desktop.
Save dnthanh98/80a2d607db230ee6e017e6310163d964 to your computer and use it in GitHub Desktop.
sql
//utils.sql
use BookStore;
drop procedure if exists drop_fk;
create procedure drop_fk(
in tb_name varchar(30),
in fk_name varchar(30)
)begin
if exists(select true from information_schema.table_constraints
where table_schema = 'BookStore' and table_name = tb_name
and constraint_type = 'FOREIGN KEY' and constraint_name = fk_name)then
select tb_name, fk_name as '';
alter table tb_name drop foreign key fk_name;
end if;
end;
//main.sql
use BookStore;
call drop_fk('Sach', 'UFK_Sach_TacGia');
call drop_fk('Sach', 'UFK_Sach_NhaSanXuat');
call drop_fk('Sach', 'UFK_Sach_LoaiSach');
call drop_fk('DonHang', 'UFK_DonHang_TaiKhoan');
call drop_fk('ChiTietDonHang', 'UFK_ChiTietDonHang_DonHang');
call drop_fk('ChiTietDonHang', 'UFK_ChiTietDonHang_Sach');
--> Error: ER_NO_SUCH_TABLE: Table 'BookStore.tb_name' doesn't exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment