{"id":1167,"date":"2010-01-20T17:47:36","date_gmt":"2010-01-21T01:47:36","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=1167"},"modified":"2023-12-29T08:18:59","modified_gmt":"2023-12-29T15:18:59","slug":"mysql-drop-table","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/","title":{"rendered":"MySQL DROP TABLE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>DROP TABLE<\/code> statement to drop a table from the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL DROP TABLE statement syntax<\/h2>\n\n\n\n<p>To&nbsp;remove existing tables, you use the MySQL <code>DROP TABLE<\/code> statement.<\/p>\n\n\n\n<p>Here is the basic syntax of the <code>DROP TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> &#91;<span class=\"hljs-keyword\">TEMPORARY<\/span>] <span class=\"hljs-keyword\">TABLE<\/span> &#91;<span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span>] table_name &#91;, table_name] ...\n&#91;RESTRICT | <span class=\"hljs-keyword\">CASCADE<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>DROP TABLE<\/code> statement removes a table and its data permanently from the database. In MySQL, you can also remove multiple tables using a single <code>DROP TABLE<\/code> statement, each table is separated by a comma (,).<\/p>\n\n\n\n<p>The <code>TEMPORARY<\/code> option allows you to remove <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-temporary-table\/\">temporary tables<\/a> only. It ensures that you do not accidentally remove non-temporary tables.<\/p>\n\n\n\n<p>The <code>IF EXISTS<\/code> option conditionally drop a table only if it exists. If you drop a non-existing table with the <code>IF EXISTS<\/code> option, MySQL generates a NOTE, which can be retrieved using the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-show-warnings\/\">SHOW WARNINGS<\/a><\/code> statement.<\/p>\n\n\n\n<p>Note that the <code>DROP TABLE<\/code> statement only drops tables. It doesn&#8217;t remove specific user privileges associated with the tables. Therefore, if you create a table with the same name as the dropped one, MySQL will apply the existing privileges to the new table, which may pose a security risk.<\/p>\n\n\n\n<p>The <code>RESTRICT<\/code> and <code>CASCADE<\/code> &nbsp;options are reserved for the future versions of MySQL.<\/p>\n\n\n\n<p>To execute the <code>DROP TABLE<\/code> statement, you must have <code>DROP<\/code> privileges for the table that you want to remove.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL DROP TABLE examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>DROP TABLE<\/code> statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using MySQL DROP TABLE to drop a single table example<\/h3>\n\n\n\n<p>First, create a table named <code>insurances<\/code> for testing purposes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> insurances (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n    title <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    effectiveDate <span class=\"hljs-built_in\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-keyword\">duration<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    amount <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span> , <span class=\"hljs-number\">2<\/span> ) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(<span class=\"hljs-keyword\">id<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the <code>DROP TABLE<\/code> to delete the <code>insurances<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> insurances;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2) Using MySQL DROP TABLE to drop multiple tables<\/h3>\n\n\n\n<p>First, create two tables named <code>CarAccessories<\/code> and <code>CarGadgets<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> CarAccessories (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    price <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span> , <span class=\"hljs-number\">2<\/span> ) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(<span class=\"hljs-keyword\">id<\/span>)\n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> CarGadgets (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    price <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">10<\/span> , <span class=\"hljs-number\">2<\/span> ) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(<span class=\"hljs-keyword\">id<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the <code>DROP TABLE<\/code> statement to drop the two tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> CarAccessories, CarGadgets;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3) Using MySQL DROP TABLE to drop a non-existing table<\/h3>\n\n\n\n<p>This statement attempts to drop a non-existing table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> aliens;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Error Code: 1051. Unknown table 'classicmodels.aliens'\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, if you use the <code>IF EXISTS<\/code> option in the <code>DROP TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> aliens;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued a warning instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">0 row(s) affected, 1 warning(s): 1051 Unknown table 'classicmodels.aliens'\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To show the warning, you can use the <code>SHOW WARNINGS<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">WARNINGS<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"307\" height=\"36\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png\" alt=\"MySQL DROP TABLE removes non existing table\" class=\"wp-image-8765\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png 307w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table-200x23.png 200w\" sizes=\"auto, (max-width: 307px) 100vw, 307px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL DROP TABLE based on a pattern<\/h2>\n\n\n\n<p>Suppose that you have many tables whose names start with <code>test<\/code> in your database and you want to remove all of them using a single <code>DROP TABLE<\/code> statement.<\/p>\n\n\n\n<p>Unfortunately, MySQL does not have the <code>DROP TABLE LIKE<\/code> statement that can remove tables based on pattern matching:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">DROP TABLE LIKE <span class=\"hljs-string\">'%pattern%'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, there are some workarounds. We will discuss one of them here for your reference.<\/p>\n\n\n\n<p>First, create three tables <code>test1<\/code>,<code>test2<\/code>, <code>test4<\/code> for demonstration:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> test1(\n  <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n  PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(<span class=\"hljs-keyword\">id<\/span>)\n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> test2 <span class=\"hljs-keyword\">LIKE<\/span> test1;\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> test3 <span class=\"hljs-keyword\">LIKE<\/span> test1;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Suppose you want to remove all <code>test*<\/code> tables.<\/p>\n\n\n\n<p>Second, declare two variables that accept database schema and a pattern that you want to the tables to match:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">--<\/span> <span class=\"hljs-selector-tag\">set<\/span> <span class=\"hljs-selector-tag\">table<\/span> <span class=\"hljs-selector-tag\">schema<\/span> <span class=\"hljs-selector-tag\">and<\/span> <span class=\"hljs-selector-tag\">pattern<\/span> <span class=\"hljs-selector-tag\">matching<\/span> <span class=\"hljs-selector-tag\">for<\/span> <span class=\"hljs-selector-tag\">tables<\/span>\n<span class=\"hljs-selector-tag\">SET<\/span> <span class=\"hljs-keyword\">@schema<\/span> = <span class=\"hljs-string\">'classicmodels'<\/span>;\n<span class=\"hljs-selector-tag\">SET<\/span> <span class=\"hljs-keyword\">@pattern<\/span> = <span class=\"hljs-string\">'test%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, construct a dynamic <code>DROP TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\">-- construct dynamic sql (DROP TABLE tbl1, tbl2...;)<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">CONCAT<\/span>(<span class=\"hljs-string\">'DROP TABLE '<\/span>,<span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">CONCAT<\/span>(@<span class=\"hljs-keyword\">schema<\/span>,<span class=\"hljs-string\">'.'<\/span>,table_name)),<span class=\"hljs-string\">';'<\/span>)\n<span class=\"hljs-keyword\">INTO<\/span> @droplike\n<span class=\"hljs-keyword\">FROM<\/span> information_schema.tables\n<span class=\"hljs-keyword\">WHERE<\/span> @<span class=\"hljs-keyword\">schema<\/span> = <span class=\"hljs-keyword\">database<\/span>()\n<span class=\"hljs-keyword\">AND<\/span> table_name <span class=\"hljs-keyword\">LIKE<\/span> @pattern;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query instructs MySQL to go to the information_schema\u00a0 table, which contains information on all tables in all databases, and concatenates all tables in the database <code>@schema<\/code> ( <code>classicmodels<\/code> <em>) <\/em>that matches the pattern <code> @pattern<\/code> <em>( <code>test%<\/code> ) <\/em>with the prefix <code>DROP TABLE<\/code> <em>. <\/em>The <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-group_concat\/\">GROUP_CONCAT<\/a><\/code> function creates a comma-separated list of tables.<\/p>\n\n\n\n<p>Fourth, display the dynamic SQL to verify if it works correctly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\">-- display the dynamic sql statement<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> @droplike;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"424\" height=\"43\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2010\/01\/mysql-drop-table-like.png\" alt=\"mysql drop table like\" class=\"wp-image-1171\" title=\"mysql drop table like\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2010\/01\/mysql-drop-table-like.png 424w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2010\/01\/mysql-drop-table-like-300x30.png 300w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/figure>\n\n\n\n<p>As you can see, the output is what we expected.<\/p>\n\n\n\n<p>Fifth, execute the statement using a <a title=\"MySQL Prepared Statement\" href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-prepared-statement\/\">prepared statement <\/a>&nbsp;as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\">-- execute dynamic sql<\/span>\n<span class=\"hljs-keyword\">PREPARE<\/span> stmt <span class=\"hljs-keyword\">FROM<\/span> @droplike;\n<span class=\"hljs-keyword\">EXECUTE<\/span> stmt;\n<span class=\"hljs-keyword\">DEALLOCATE<\/span> <span class=\"hljs-keyword\">PREPARE<\/span> stmt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Putting it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\">-- set table schema and pattern matching for tables<\/span>\n<span class=\"hljs-keyword\">SET<\/span> @<span class=\"hljs-keyword\">schema<\/span> = <span class=\"hljs-string\">'classicmodels'<\/span>;\n<span class=\"hljs-keyword\">SET<\/span> @pattern = <span class=\"hljs-string\">'test%'<\/span>;\n\n<span class=\"hljs-comment\">-- build dynamic sql (DROP TABLE tbl1, tbl2...;)<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">CONCAT<\/span>(<span class=\"hljs-string\">'DROP TABLE '<\/span>,<span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">CONCAT<\/span>(@<span class=\"hljs-keyword\">schema<\/span>,<span class=\"hljs-string\">'.'<\/span>,table_name)),<span class=\"hljs-string\">';'<\/span>)\n<span class=\"hljs-keyword\">INTO<\/span> @droplike\n<span class=\"hljs-keyword\">FROM<\/span> information_schema.tables\n<span class=\"hljs-keyword\">WHERE<\/span> @<span class=\"hljs-keyword\">schema<\/span> = <span class=\"hljs-keyword\">database<\/span>()\n<span class=\"hljs-keyword\">AND<\/span> table_name <span class=\"hljs-keyword\">LIKE<\/span> @pattern;\n\n<span class=\"hljs-comment\">-- display the dynamic sql statement<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> @droplike;\n\n<span class=\"hljs-comment\">-- execute dynamic sql<\/span>\n<span class=\"hljs-keyword\">PREPARE<\/span> stmt <span class=\"hljs-keyword\">FROM<\/span> @droplike;\n<span class=\"hljs-keyword\">EXECUTE<\/span> stmt;\n<span class=\"hljs-keyword\">DEALLOCATE<\/span> <span class=\"hljs-keyword\">PREPARE<\/span> stmt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So if you want to drop multiple tables that have a specific pattern in a database, you just use the script above to save time. <\/p>\n\n\n\n<p>All you need to do is replace the <em>pattern<\/em> and the<em> database schema<\/em> in <code>@pattern<\/code> and <code>@schema<\/code> variables. <\/p>\n\n\n\n<p>If you often have to deal with this task, you can develop a <a title=\"MySQL Stored Procedure\" href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/\">stored procedure<\/a> based on the script and reuse this stored procedure.<\/p>\n\n\n\n<p>In this tutorial, you have learned how to use the MySQL <code>DROP TABLE<\/code> statement to remove existing tables from a database.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1167\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/\"\n\t\t\t\tdata-post-title=\"MySQL DROP TABLE\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1167\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/\"\n\t\t\t\tdata-post-title=\"MySQL DROP TABLE\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":42,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1167","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL DROP TABLE<\/title>\n<meta name=\"description\" content=\"In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL DROP TABLE\" \/>\n<meta property=\"og:description\" content=\"In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T15:18:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/\",\"name\":\"MySQL DROP TABLE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-DROP-TABLE-removes-non-existing-table.png\",\"datePublished\":\"2010-01-21T01:47:36+00:00\",\"dateModified\":\"2023-12-29T15:18:59+00:00\",\"description\":\"In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-DROP-TABLE-removes-non-existing-table.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-DROP-TABLE-removes-non-existing-table.png\",\"width\":307,\"height\":36,\"caption\":\"MySQL DROP TABLE removes non existing table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-drop-table\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL DROP TABLE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL DROP TABLE","description":"In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/","og_locale":"en_US","og_type":"article","og_title":"MySQL DROP TABLE","og_description":"In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T15:18:59+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/","name":"MySQL DROP TABLE","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png","datePublished":"2010-01-21T01:47:36+00:00","dateModified":"2023-12-29T15:18:59+00:00","description":"In this tutorial,you will learn how to use the DROP TABLE statement to delete one or more tables from the database.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-DROP-TABLE-removes-non-existing-table.png","width":307,"height":36,"caption":"MySQL DROP TABLE removes non existing table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-drop-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL DROP TABLE"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/1167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=1167"}],"version-history":[{"count":2,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/1167\/revisions"}],"predecessor-version":[{"id":13839,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/1167\/revisions\/13839"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}