|
写个存储过程,drop foreignkey
drop procedure if exists DropForeignKey;
delimiter //
create procedure dropforeignkey(p_tablename varchar(255),p_fieldname varchar(255))
begin
declare v_sql varchar(255) ;
declare v_constraint_name varchar(255) ;
declare done int default 0;
declare c cursor for select constraint_name from t_constraint ;
declare continue handler for sqlstate '02000' set done = 1;
drop temporary table if exists t_constraint ;
create temporary table if not exists t_constraint (constraint_name varchar(255));
insert into t_constraint
select a.constraint_name from information_schema.key_column_usage a
where a.table_name = p_tablename and a.column_name = p_fieldname
and exists (select 1 from information_schema.TABLE_CONSTRAINTS b where a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
and a.TABLE_NAME = b.TABLE_NAME and b.CONSTRAINT_TYPE = 'FOREIGN KEY' ) ;
open c;
repeat
fetch c into v_constraint_name ;
if not done then
set v_sql = concat ('alter table ' ,p_tablename
, ' drop foreign key ',v_constraint_name ) ;
set @sql = v_sql;
prepare sl from @sql;
execute sl;
deallocate prepare sl;
end if ;
until done end repeat;
close c;
end ;
delimiter ;
[ 本帖最后由 gaolu1234 于 2009-3-20 14:22 编辑 ] |
|