MySQL 在mysql中一次性删除多个表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4922189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drop multiple tables in one shot in mysql
提问by Krunal
How to drop multiple tables from one single database at one command. something like,
如何使用一个命令从一个数据库中删除多个表。就像是,
> use test;
> drop table a,b,c;
where a,b,c are the tables from database test.
其中 a,b,c 是数据库 test 中的表。
回答by Leniel Maccaferri
Example:
例子:
Let's say table A has two children B and C. Then we can use the following syntax to drop all tables.
假设表 A 有两个孩子 B 和 C。然后我们可以使用以下语法删除所有表。
DROP TABLE IF EXISTS B,C,A;
This can be placed in the beginning of the script instead of individually dropping each table.
这可以放在脚本的开头,而不是单独删除每个表。
回答by OrangeDog
SET foreign_key_checks = 0;
DROP TABLE IF EXISTS a,b,c;
SET foreign_key_checks = 1;
Then you do not have to worry about dropping them in the correct order, nor whether they actually exist.
这样您就不必担心以正确的顺序放置它们,也不必担心它们是否确实存在。
N.B. this is for MySQL only (as in the question). Other databases likely have different methods for doing this.
注意这仅适用于 MySQL(如问题所示)。其他数据库可能有不同的方法来执行此操作。
回答by Javaughn Hymanson
A lazy way of doing this if there are alot of tables to be deleted.
如果要删除很多表,这是一种懒惰的方法。
Get table using the below
- For sql server - SELECT CONCAT(name,',') Table_Name FROM SYS.tables;
- For oralce - SELECT CONCAT(TABLE_NAME,',') FROM SYS.ALL_TABLES;
Copy and paste the table names from the result set and paste it after the DROP command.
使用下面的获取表格
- 对于 sql server - SELECT CONCAT(name,',') Table_Name FROM SYS.tables;
- 对于oralce - SELECT CONCAT(TABLE_NAME,',') FROM SYS.ALL_TABLES;
从结果集中复制并粘贴表名,然后将其粘贴到 DROP 命令之后。
回答by user4774666
declare @sql1 nvarchar(max)
SELECT @sql1 =
STUFF(
(
select ' drop table dbo.[' + name + ']'
FROM sys.sysobjects AS sobjects
WHERE (xtype = 'U') AND (name LIKE 'GROUP_BASE_NEW_WORK_%')
for xml path('')
),
1, 1, '')
execute sp_executesql @sql1