Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

DROP all system tables

Questions related to using nuBuilder Forte.
Post Reply
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

DROP all system tables

Unread post by apmuthu »

Created a Wiki Article on the topic.

Multi table DROP based on pattern of table name is not available in MySQL. The workaround is:

Code: Select all

SET SESSION group_concat_max_len = 1000000;
SET @schema:='nubuilder4';
SET @string:='zzzzsys_%';
 
SELECT CONCAT ('DROP TABLE ',GROUP_CONCAT(DISTINCT CONCAT(@schema,'.',TABLE_NAME) SEPARATOR ", "),';')
INTO @droptool
FROM information_schema.TABLES
WHERE @schema = DATABASE() AND TABLE_TYPE="BASE TABLE"
AND TABLE_NAME LIKE @string;
 
SELECT @droptool;
 
PREPARE stmt FROM @droptool;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
On next login, nubuilder4.sql is used to populate it with the System Tables. Useful when you want all developmental work reset to new code from the repo whilst retaining the user tables and their data.
You do not have the required permissions to view the files attached to this post.
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Re: DROP all system tables

Unread post by apmuthu »

As this is a very dangerous operation, a warning is provided for the inexperienced user.
You do not have the required permissions to view the files attached to this post.
apmuthu
Posts: 249
Joined: Sun Dec 06, 2020 6:50 am
Location: Chennai, India, Singapore

Re: DROP all system tables

Unread post by apmuthu »

Updated to include Views as well and purge variable after use.

Code: Select all

SET SESSION group_concat_max_len = 1000000;
SET @schema:='nubuilder4'; -- change as needed
SET @string:='zzzzsys_%';
 
SELECT CONCAT ('DROP TABLE ',GROUP_CONCAT(DISTINCT CONCAT(@schema,'.',table_name) SEPARATOR ", "),';')
INTO @droptool
FROM information_schema.tables
WHERE @schema = DATABASE() AND TABLE_TYPE = "BASE TABLE"
AND table_name LIKE @string;

SELECT @droptool;
 
PREPARE stmt FROM @droptool;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @droptool:=NULL;

SELECT CONCAT ('DROP VIEW ',GROUP_CONCAT(DISTINCT CONCAT(@schema,'.',table_name) SEPARATOR ", "),';')
INTO @droptool
FROM information_schema.tables
WHERE @schema = DATABASE() AND TABLE_TYPE = "VIEW"
AND table_name LIKE @string;

SELECT @droptool;
 
PREPARE stmt FROM @droptool;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @droptool:=NULL;
You do not have the required permissions to view the files attached to this post.
Post Reply