截断所有匹配名称模式的表
TRUNCATE all tables matching name pattern
这是我正在使用的 sql 基于 this 答案:
SET @pattern = '%_movielist';
SELECT concat('TRUNCATE TABLE ', GROUP_CONCAT(concat(TABLE_NAME)), ';')
INTO @truncatelike FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE @pattern;
SELECT @truncatelike;
PREPARE stmt FROM @truncatelike;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
但我收到此错误 Access denied for user 'root'@'%' to database 'information_schema'
。
我究竟做错了什么?它似乎适用于其他用户
您试图在 "information_schema" 数据库上执行此语句。阅读有关此数据库的更多信息 [https://dev.mysql.com/doc/refman/5.7/en/information-schema.html]
您不应该在 information_schema 数据库上 运行 语句(除非您真的知道自己在做什么)。该数据库用作指示服务器如何运行的 "meta" 存储库。很可能你不需要触摸它,如果你这样做,你可能会破坏你的服务器。
这里已经回答了。 [
以上限制:只有当语句返回的table的no为1且超过1tables时,该查询才有效,您将需要在迭代中使用它。
为了让所有 table 匹配我们需要使用存储过程的模式。
请更改程序名称
CREATE PROCEDURE `new_procedure`()
BEGIN
-- Pattern to Match
SET @pattern = '%_movielist';
-- Temporary Table to Store the Result of The Select Statement
CREATE TEMPORARY TABLE IF NOT EXISTS Table_ToBeTruncated
(
Id int NOT NULL AUTO_INCREMENT,TableName varchar(100),
PRIMARY KEY (id)
);
-- Insert all the TableName to be Truncated
insert Table_ToBeTruncated(TableName)
SELECT distinct concat('TRUNCATE TABLE `', TABLE_NAME, '`;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE @pattern and TABLE_SCHEMA = 'movielist';
-- Declare a variable to count the no of records to be truncated.
SET @count=(Select count(*)from Table_ToBeTruncated);
-- Iterate the list
WHILE @count> 0 DO
-- Pick One table from the Temporary Table List;
SELECT TableName into @truncatelike from Table_ToBeTruncated where ID= @count;
-- Prepare the statement
PREPARE stmt FROM @truncatelike;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Decrease the counter.
set @count = @count- 1;
END WHILE;
drop TEMPORARY TABLE IF EXISTS Table_ToBeTruncated ;
END
这是我正在使用的 sql 基于 this 答案:
SET @pattern = '%_movielist';
SELECT concat('TRUNCATE TABLE ', GROUP_CONCAT(concat(TABLE_NAME)), ';')
INTO @truncatelike FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE @pattern;
SELECT @truncatelike;
PREPARE stmt FROM @truncatelike;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
但我收到此错误 Access denied for user 'root'@'%' to database 'information_schema'
。
我究竟做错了什么?它似乎适用于其他用户
您试图在 "information_schema" 数据库上执行此语句。阅读有关此数据库的更多信息 [https://dev.mysql.com/doc/refman/5.7/en/information-schema.html]
您不应该在 information_schema 数据库上 运行 语句(除非您真的知道自己在做什么)。该数据库用作指示服务器如何运行的 "meta" 存储库。很可能你不需要触摸它,如果你这样做,你可能会破坏你的服务器。
这里已经回答了。 [
以上限制:只有当语句返回的table的no为1且超过1tables时,该查询才有效,您将需要在迭代中使用它。
为了让所有 table 匹配我们需要使用存储过程的模式。
请更改程序名称
CREATE PROCEDURE `new_procedure`()
BEGIN
-- Pattern to Match
SET @pattern = '%_movielist';
-- Temporary Table to Store the Result of The Select Statement
CREATE TEMPORARY TABLE IF NOT EXISTS Table_ToBeTruncated
(
Id int NOT NULL AUTO_INCREMENT,TableName varchar(100),
PRIMARY KEY (id)
);
-- Insert all the TableName to be Truncated
insert Table_ToBeTruncated(TableName)
SELECT distinct concat('TRUNCATE TABLE `', TABLE_NAME, '`;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE @pattern and TABLE_SCHEMA = 'movielist';
-- Declare a variable to count the no of records to be truncated.
SET @count=(Select count(*)from Table_ToBeTruncated);
-- Iterate the list
WHILE @count> 0 DO
-- Pick One table from the Temporary Table List;
SELECT TableName into @truncatelike from Table_ToBeTruncated where ID= @count;
-- Prepare the statement
PREPARE stmt FROM @truncatelike;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Decrease the counter.
set @count = @count- 1;
END WHILE;
drop TEMPORARY TABLE IF EXISTS Table_ToBeTruncated ;
END