0

This is the sql i'm using based from this answer:

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;

but I get this error Access denied for user 'root'@'%' to database 'information_schema'. What am I doing wrong? It seems to work for other users

Community
  • 1
  • 1
Teodor Vecerdi
  • 178
  • 1
  • 1
  • 11

1 Answers1

2

You trying to execute this statement on "information_schema" database. Read more about this database [https://dev.mysql.com/doc/refman/5.7/en/information-schema.html]

You should not be running statements on the information_schema database (unless you REALLY know what you're doing). The database serves as a "meta" repository that dictates how the server operates. Chances are that you have no need to touch it and you'll likely brick your server if you do.

This is already answered here. [#1044 - Access denied for user 'root'@'localhost' to database 'information_schema'

Restriction to above: This query will work only if the no of table returned by the statement is 1 for more than 1 tables, you will require to use it in iteration.

To make this work for all the table matching the pattern we would require to use stored procedure.

Please change the Procedure name

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
Rohit Kumar
  • 726
  • 3
  • 17
  • Okay, so if I can't run statements on information_schema, then how can I truncate all the tables in my other database which name matches the pattern? – Teodor Vecerdi Jun 01 '17 at 10:49
  • @TeodorVecerdi Use the query in the database that contains the tables which are required to be truncated. – Rohit Kumar Jun 01 '17 at 10:57
  • but the database that contains the tables doesn't have a "TABLES" table like information_schema – Teodor Vecerdi Jun 01 '17 at 11:00
  • @TeodorVecerdi information_schema is not a table, it's a database which serves as a "meta" repository that dictates how the server operates. I have updated my answer. – Rohit Kumar Jun 01 '17 at 11:03
  • Yes, I know, I was reffering to the TABLES table in information_schema. Also, I'm a beginner with SQL so I don't really know how to do advanced things – Teodor Vecerdi Jun 01 '17 at 11:19
  • @TeodorVecerdi No worries, welcome here, we are happy to help, let us know if you are facing issues. – Rohit Kumar Jun 01 '17 at 11:30
  • I still don't know how to truncate all the tables from my database whose name match a pattern. – Teodor Vecerdi Jun 01 '17 at 11:31
  • @TeodorVecerdi No Problem I will have a answer created for you. – Rohit Kumar Jun 01 '17 at 11:37
  • Thank you, the db name is "movielist" and I need the pattern to be "%_movielist" – Teodor Vecerdi Jun 01 '17 at 11:50
  • @TeodorVecerdi I have prepared the answer if this works for you, please feel free to flag and mark the answer as accepted. This will help other looking for similar solution. – Rohit Kumar Jun 01 '17 at 12:16
  • Should I replace the definer with something like 'root'@'%'? – Teodor Vecerdi Jun 01 '17 at 12:33
  • `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4` – Teodor Vecerdi Jun 01 '17 at 12:36
  • @TeodorVecerdi You can ignore the definer and proceed it optional. Will update you on definer in a while. – Rohit Kumar Jun 01 '17 at 12:37
  • [here](https://i.imgur.com/ZnmGN6N.png). The SQL code is the same as you provided. I only changed the procedure name. Line 4 is `SET @pattern = '%_movielist'` – Teodor Vecerdi Jun 01 '17 at 12:45
  • @TeodorVecerdi Which tool are you using explore table and database structure, or doing it through console. – Rohit Kumar Jun 01 '17 at 12:50
  • I'm using phpmyadmin – Teodor Vecerdi Jun 01 '17 at 13:05
  • @TeodorVecerdi this video might be helpful for you [https://www.youtube.com/watch?v=rn-f2I7Sb88 ]creating stored procedure in phpmyadmin . If you still face issues, my skype id is "rimrohit" where we can share the screen solve at earliest. – Rohit Kumar Jun 01 '17 at 13:17
  • 1
    So I just ran the sql code you provided from Jetbrains' DataGrip and it worked xD. Thank you so much man! – Teodor Vecerdi Jun 01 '17 at 13:43
  • @TeodorVecerdi I wished I could have solved it more quickly If you found my answer, would appreciate if you mark it useful and accepted so that it could be helpful for others too. It was nice working with you cheer! Reach us if any help required. – Rohit Kumar Jun 01 '17 at 13:47
  • @TeodorVecerdi Thanks for letting me know another provider Jetbrains' DataGrip ... – Rohit Kumar Jun 01 '17 at 14:24