我可以看到有一些问题[1,2]已经提出要求,但解决方案并没有包含完整的SQL脚本来执行此任务。 我有一种情况,只使用SQL删除所有外键非常有帮助。 目前我正试图用存储过程和游标解决这个问题,如下所示:
-- No automatic commits:
DROP PROCEDURE IF EXISTS removeConstraints;
-- Magic to happen soon:
DELIMITER |
CREATE PROCEDURE removeConstraints()
BEGIN
SET AUTOCOMMIT=0;
SET FOREIGN_KEY_CHECKS=0;
-- https://dev.mysql.com/doc/refman/5.0/en/cursors.html
-- https://stackoverflow.com/questions/1745165/looping-over-result-sets-in-mysql
-- https://mariadb.com/kb/en/mariadb/cursor-overview/
DECLARE done INT DEFAULT FALSE;
DECLARE s VARCHAR(1024) DEFAULT '';
DECLARE cur CURSOR FOR SELECT CONCAT('ALTER TABLE ',TABLE_NAME, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';')
FROM INFORMATION_SCHEMA.Key_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO s;
IF done THEN
LEAVE read_loop;
END IF;
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
SET FOREIGN_KEY_CHECKS=1;
COMMIT;
SET AUTOCOMMIT=1;
END |
DELIMITER ;
-- Do magic:
CALL removeConstraints();
-- Cleanup:
DROP PROCEDURE removeConstraints;
遗憾的是,这会产生以下错误消息:
ERROR 1064 (42000) at line 5: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE done INT DEFAULT FALSE;
DECLARE s VARCHAR(1024) DEFAULT '';
DECLARE ' at line 8
根据Ravinder Reddy的输入,我现在更新了DECLARE
之后的BEGIN
部分,如下所示:
CREATE PROCEDURE removeConstraints()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE s VARCHAR(1024) DEFAULT '';
DECLARE cur CURSOR FOR
SELECT DISTINCT CONCAT('ALTER TABLE ',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME,';')
FROM INFORMATION_SCHEMA.Key_COLUMN_USAGE
WHERE TABLE_SCHEMA=DATABASE()
AND REFERENCED_TABLE_NAME IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET AUTOCOMMIT=0;
SET FOREIGN_KEY_CHECKS=0;
但是当我尝试执行该过程时,我仍然会收到错误:
MariaDB [v4]> CALL removeConstraints();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NULL' at line 1
我还尝试使用不同的SELECT
语句,如:
SELECT CONCAT('ALTER TABLE ',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME,';')
FROM INFORMATION_SCHEMA.Key_COLUMN_USAGE
WHERE TABLE_SCHEMA=DATABASE()
AND CONSTRAINT_NAME != 'PRIMARY'
AND CONSTRAINT_NAME IS NOT NULL
AND TABLE_NAME IS NOT NULL;
......但它没有帮助。
我现在通过更改代码来使其工作,以便CONCAT
稍后发生。
DROP PROCEDURE IF EXISTS removeConstraints;
-- Magic to happen soon:
DELIMITER |
CREATE PROCEDURE removeConstraints()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tName VARCHAR(64);
DECLARE cName VARCHAR(64);
DECLARE cur CURSOR FOR
SELECT DISTINCT TABLE_NAME, CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.Key_COLUMN_USAGE
WHERE TABLE_SCHEMA=DATABASE()
AND REFERENCED_TABLE_NAME IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET AUTOCOMMIT=0;
SET FOREIGN_KEY_CHECKS=0;
OPEN cur;
read_loop: LOOP
FETCH cur INTO tName, cName;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql = CONCAT('ALTER TABLE ',tName,' DROP FOREIGN KEY ',cName,';');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
SET FOREIGN_KEY_CHECKS=1;
COMMIT;
SET AUTOCOMMIT=1;
END |
DELIMITER ;
-- Do magic:
CALL removeConstraints();
-- Cleanup:
DROP PROCEDURE removeConstraints;
答案 0 :(得分:3)
所有DECLARE
语句必须位于BEGIN
- END
块的顶部。
所有其他陈述都应遵循它们。
在您的代码中,您在SET
语句之前定义了DECLARE
个语句
将这些语句移到DECLARE
语句的下方。
示例:
-- https://dev.mysql.com/doc/refman/5.0/en/cursors.html
-- https://stackoverflow.com/questions/1745165/looping-over-result-sets-in-mysql
-- https://mariadb.com/kb/en/mariadb/cursor-overview/
DECLARE done INT DEFAULT FALSE;
DECLARE s VARCHAR(1024) DEFAULT '';
DECLARE cur CURSOR FOR
SELECT CONCAT('ALTER TABLE ',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME,';')
FROM INFORMATION_SCHEMA.Key_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET AUTOCOMMIT=0;
SET FOREIGN_KEY_CHECKS=0;
文档参考:
仅在DECLARE
复合语句中允许
BEGIN ... END
并且必须在任何其他陈述之前开始。声明必须遵循一定的顺序。游标声明必须 出现在处理程序声明之前。变量和条件 声明必须出现在游标或处理程序声明之前