{"id":30158,"date":"2023-02-06T09:01:59","date_gmt":"2023-02-06T17:01:59","guid":{"rendered":"https:\/\/coderpad.io\/?p=30158"},"modified":"2023-06-07T13:37:43","modified_gmt":"2023-06-07T20:37:43","slug":"mysql-show-tables-a-detailed-guide","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/mysql-show-tables-a-detailed-guide\/","title":{"rendered":"MySQL SHOW TABLES: A Detailed Guide"},"content":{"rendered":"\n<p>The MySQL <code>SHOW tables<\/code> command is very appropriately named since it allows you to list tables on a MySQL database.<\/p>\n\n\n\n<p>&nbsp;By the end of the post, you&#8217;ll have learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The definition of the command<\/li>\n\n\n\n<li>How you can use it<\/li>\n\n\n\n<li>Why it&#8217;s useful in the first place, and<\/li>\n\n\n\n<li>An alternative way to list the tables in a database without using the <code>show tables<\/code> command<\/li>\n<\/ul>\n\n\n\n<p>We&#8217;ll also cover how listing tables in MySQL compare with other RDBMSs (relational database management systems.)<\/p>\n\n\n\n<p>Let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Requirements<\/strong><\/h2>\n\n\n\n<p>There are only two prerequisites if you want to follow along with the examples I&#8217;ll give. First, I expect some basic MySQL and SQL knowledge. Also, you should have access to a MySQL local installation.<\/p>\n\n\n\n<p>A great way to do that is to use the <a href=\"https:\/\/coderpad.io\/languages\/mysql\/\">CoderPad MySQL Sandbox<\/a> to quickly and easily get started writing SQL&nbsp;as it\u2019ll be your MySQL client for this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MySQL <em>SHOW tables<\/em>: what and why<\/strong><\/h2>\n\n\n\n<p>In MySQL, the <code>show tables<\/code> command is what you use to list the tables in a database. That&#8217;s pretty much it when it comes to defining the command. But what are the uses cases for it? After all, when using a MySQL GUI (graphical user interface) client, you can easily display the tables inside a database, typically in a tree.<\/p>\n\n\n\n<p>First, you won&#8217;t always have access to a GUI when interacting with MySQL. If the command line is all you have, <code>show tables<\/code> comes in handy. Also, keep in mind that this command is super flexible. When using it, you can leverage clauses such as <code>WHERE<\/code> and <code><a href=\"https:\/\/coderpad.io\/blog\/development\/mysql-like-operator-a-detailed-guide\/\">LIKE<\/a><\/code> to further filter the output.<\/p>\n\n\n\n<p>Finally, since this is a command, you can use it in scripts. So, there&#8217;s an opportunity to automate some tasks that simply wouldn&#8217;t be possible using a GUI.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How do I view tables in MySQL?<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s start with the most basic example. In the CoderPad MySQL sandbox, let&#8217;s create two tables:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> students (\n\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>),\n\n\u00a0\u00a0\u00a0\u00a0email <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>));\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> teachers (\n\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>),\n\n\u00a0\u00a0\u00a0\u00a0email <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/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>Run <code>show tables<\/code> in the sandbox. You should see the tables being listed:<\/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 shcb-wrap-lines\">+<span class=\"hljs-comment\">--------------------+<\/span>\n| Tables_in_coderpad |\n+<span class=\"hljs-comment\">--------------------+<\/span>\n| students           |\n| teachers           |\n+<span class=\"hljs-comment\">--------------------+<\/span><\/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<h2 class=\"wp-block-heading\"><strong>MySQL <em>SHOW tables<\/em>: some practical examples<\/strong><\/h2>\n\n\n\n<p>You&#8217;ve just seen the most basic way you can use the <code>show tables<\/code> command. Now we&#8217;re ready to proceed to more interesting examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to tell tables and views apart<\/strong><\/h3>\n\n\n\n<p>Does the <code>SHOW tables<\/code> command also show views? Despite the name, yes. First, run the following command to create a view:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">VIEW<\/span> students_onlyname <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">FROM<\/span> students;<\/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<p>Now, run <code>show tables<\/code> again, and you&#8217;ll see that the view is included. However, there&#8217;s no way to distinguish the view from the table. For that, you can use a different command:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">full<\/span> <span class=\"hljs-keyword\">tables<\/span>;<\/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>The command above returns the following output, which distinguishes between &#8220;base table&#8221; and &#8220;view&#8221;.<\/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 shcb-wrap-lines\">+<span class=\"hljs-comment\">--------------------+------------+<\/span>\n| Tables_in_coderpad | Table_type |\n+<span class=\"hljs-comment\">--------------------+------------+<\/span>\n| students           | BASE TABLE |\n| students_onlyname | VIEW        |\n| teachers           | BASE TABLE |\n+<span class=\"hljs-comment\">--------------------+------------+<\/span><\/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\"><strong>Filtering the results with a LIKE clause<\/strong><\/h3>\n\n\n\n<p>You often don&#8217;t want to get all of the existing tables, but a subset of them. You can use the <code>LIKE<\/code> clause to filter the results, in the same way, you&#8217;d do in a <code>SELECT<\/code> statement. For instance, running <code>show tables LIKE 'stu%'<\/code> returns the <code>student<\/code> table and the <code>student_onlyname<\/code> view, but not the <code>teachers<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Filtering the results with a WHERE clause<\/strong><\/h3>\n\n\n\n<p>Similarly, you can filter your results using a <code>WHERE<\/code> clause. As you&#8217;ve already observed, when we ask MySQL to list its tables, the results themselves are presented in a tabular form, and the column which contains the table name is called &#8220;Tables_in_&lt;database-name&gt;&#8221;. So, in our case, &#8220;Tables_in_school&#8221;. You can use that name to filter that column using the <code>WHERE<\/code> clause. Examples:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">tables<\/span> <span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">length<\/span>(Tables_in_school) &lt;= <span class=\"hljs-number\">8<\/span>;\n\n<span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">tables<\/span> <span class=\"hljs-keyword\">WHERE<\/span> Tables_in_school = <span class=\"hljs-string\">'students'<\/span>;<\/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>There&#8217;s nothing stopping you from using the <code>SHOW full tables<\/code> command as well, in which case you can also filter using the <code>Table_type<\/code> column:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">full<\/span> <span class=\"hljs-keyword\">tables<\/span> <span class=\"hljs-keyword\">WHERE<\/span> Table_type != <span class=\"hljs-string\">'VIEW'<\/span><\/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<h3 class=\"wp-block-heading\"><strong>Showing tables from a different database<\/strong><\/h3>\n\n\n\n<p>Up until now, all of the examples you&#8217;ve seen show how to list tables from the database you&#8217;re connected to. It&#8217;s also possible to list tables from a different database, and it&#8217;s extremely easy to do so.<\/p>\n\n\n\n<p>First, create a new database, running <code>CREATE database myblog;<\/code>. Then, select it by running <code>use myblog;<\/code>. Finally, you can list the tables from the first database by running <code>show tables from school;<\/code>.<\/p>\n\n\n\n<p>Keep in mind that, for that to work, you must have the necessary privileges on the other database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>An additional way of listing tables in MySQL: The metadata tables<\/strong><\/h2>\n\n\n\n<p>The <code>SHOW tables<\/code> command is non-standard and unique to MySQL, which means it&#8217;s not transferable to other database engines. However, there&#8217;s another you to fetch the list of tables in MySQL, which is transferable to (at least some) other RDBMSs: the <code>INFORMATION_SCHEMA<\/code> views.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p><em>In relational databases, the information schema (information_schema) is an ANSI-standard set of read-only views that provide information about all of the tables, views, columns, and procedures in a database.<\/em><\/p>\n<cite>&#8211; <a href=\"https:\/\/en.wikipedia.org\/wiki\/Information_schema\" target=\"_blank\" rel=\"noopener\">Wikipedia<\/a>.<\/cite><\/blockquote>\n\n\n\n<p>Here&#8217;s how to query the <code>tables<\/code> view to fetch the tables from the <code>school<\/code> database:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> table_name, table_type \n\n<span class=\"hljs-keyword\">FROM<\/span> INFORMATION_SCHEMA.tables \n\n<span class=\"hljs-keyword\">WHERE<\/span> table_schema = <span class=\"hljs-string\">'school'<\/span>;<\/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>Keep in mind that the <code>INFORMATION_SCHEMA<\/code> views contain much more information than just the names of tables. There are views for columns, <a href=\"https:\/\/coderpad.io\/blog\/development\/how-to-use-indexes-to-increase-mysql-database-performance\/\">indexes<\/a>, views, and all other artifacts inside a database.<\/p>\n\n\n\n<p>For instance, the following query displays data about the columns of the <code>students<\/code> table:<\/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 shcb-wrap-lines\"><span class=\"hljs-keyword\">SELECT<\/span> table_name, column_name, is_nullable, data_type, character_maximum_length\n\n<span class=\"hljs-keyword\">FROM<\/span> INFORMATION_SCHEMA.columns \n\n<span class=\"hljs-keyword\">WHERE<\/span> TABLE_SCHEMA = <span class=\"hljs-string\">'school'<\/span> <span class=\"hljs-keyword\">AND<\/span> TABLE_NAME = <span class=\"hljs-string\">'students'<\/span>;<\/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<h2 class=\"wp-block-heading\"><strong>MySQL SHOW tables: add it to your toolkit<\/strong><\/h2>\n\n\n\n<p>MySQL <code>SHOW tables<\/code> is sort of a niche command. As you&#8217;ve seen, it&#8217;s unique to MySQL, since other database engines offer different ways to achieve the same results. Thus, it doesn&#8217;t qualify as a<a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\"> SQL command everyone should know.<\/a> However, that doesn&#8217;t mean it isn&#8217;t useful.<\/p>\n\n\n\n<p>If you&#8217;re accessing a MySQL database through the command line, this command should be a crucial part of your toolkit. With it, you can quickly and easily see and even filter the tables inside a given database. Even if you use a GUI-based MySQL client, learning how to use the filtering options when listing tables can help you better navigate complex<a href=\"https:\/\/coderpad.io\/blog\/development\/optimize-mysql-database-schema\/\"> database schemas.<\/a><\/p>\n\n\n\n<p>In the post, you&#8217;ve also learned that, despite being useful, the MySQL <code>SHOW tables<\/code> command is non-standard, which means you can use it on different database engines. A viable alternative is to use the <code>INFORMATION_SCHEMA<\/code> views: they&#8217;re an ANSI standard, implemented by several database vendors.<\/p>\n\n\n\n<p>Also, feel free to explore the CoderPad blog for more database-oriented posts, such as these:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/sql-functions-and-techniques-every-data-person-should-know\/\">SQL Functions and Techniques Every Data Person Should Know<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/optimize-mysql-database-schema\/\">Optimize MySQL Database Schema<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/mysql-like-operator-a-detailed-guide\/\">MySQL LIKE Operator: A Detailed Guide<\/a><\/li>\n<\/ul>\n\n\n\n<p>Try out the <code>SHOW<\/code> command learned in this article in the MySQL sandbox below:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=241200&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>Thanks for reading,&nbsp; until next time! <em>This post was written by Carlos Schults. <\/em><a href=\"https:\/\/carlosschults.net\" target=\"_blank\" rel=\"noopener\"><em>Carlos<\/em><\/a><em> is a consultant and software engineer with experience in desktop, web, and mobile development. Though his primary language is C#, he has experience with a number of languages and platforms. His main interests include automated testing, version control, and code quality.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The MySQL SHOW TABLES command is very appropriately named since it allows you to list tables on a MySQL database. Learn more about it in this guide.<\/p>\n","protected":false},"author":1,"featured_media":30171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[66],"keyword-cluster":[],"class_list":["post-30158","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/30158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=30158"}],"version-history":[{"count":54,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/30158\/revisions"}],"predecessor-version":[{"id":34545,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/30158\/revisions\/34545"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/30171"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=30158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=30158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=30158"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=30158"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=30158"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=30158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}