【发布时间】:2018-09-19 10:29:31
【问题描述】:
我是编写程序的新手,我检查了一些stackoverflow帖子,但没有找到删除特定表的所有外键的帖子。
我有一个写在下面的程序。
drop procedure if exists remove_foreign_key;
delimiter ;;
create procedure remove_foreign_key(vTableName varchar(128)) begin
if exists (select table_name from information_schema.columns where table_schema=database() and table_name = vTableName) then
(SELECT concat('ALTER TABLE ', TABLE_NAME, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';')
FROM information_schema.key_column_usage
WHERE table_schema=database() and TABLE_NAME = vTableName
AND referenced_table_name IS NOT NULL);
end if;
end if;
end;;
delimiter ;
call remove_foreign_key('my_table_name');
上面的查询返回我所有需要执行的查询(表包含的外键数),但我想一个一个地执行它们。我已经阅读了游标但不确定它的实现。有人可以帮忙吗?
【问题讨论】:
-
那么对于每一个外键,你要一个一个地启动程序吗?
-
不,每张桌子
-
好的,所以在表上使用游标,它将获取表然后启动过程。这是每个表的循环过程吗?
-
请查看链接,希望对解决您的问题有所帮助。 stackoverflow.com/questions/43044683/…
标签: mysql sql cursor procedure