Example uses of MySQL SHOW syntax.

In this blog post, I will look at a few handy uses of the SELECT and SHOW commands to retrieve information in MySQL. SHOW returns useful information on databases, tables, etc…


words-information-on-mail-box

Photo by Roman Kraft on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:

  • Xubuntu Linux 16.04.5 LTS (Xenial Xerus)
  • MySQL 5.7.23

What database am I currently in?

1
2
3
4
5
6
7
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| asbuilt    |
+------------+
1 row in set (0.00 sec)

What tables, if any, are present in the current database?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mysql> SHOW TABLES;
+----------------------+
| Tables_in_asbuilt    |
+----------------------+
| asset_staging        |
| category             |
| closer_look          |
| coating_details      |
| degree_value         |
| diameter             |
| find_degree          |
| flagged_asset        |
| grade                |
| manufacturer         |
| pipe                 |
| pipe_category        |
| pipe_coating_details |
| pipe_grade           |
| pipe_manufacturer    |
| pw_wall_thickness    |
| wall_thickness       |
+----------------------+
17 rows in set (0.01 sec)

I am curious about table degree_value. What columns, data types, and indexes (if any) does it have?
The DESC (or DESCRIBE) keyword will get you the columns and data types.

1
2
3
4
5
6
7
8
mysql> DESC degree_value;
+---------------+-----------------------+------+-----+---------+-------+
| Field         | Type                  | Null | Key | Default | Extra |
+---------------+-----------------------+------+-----+---------+-------+
| pipe_id       | smallint(6)           | YES  | MUL | NULL    |       |
| degree_amount | decimal(4,2) unsigned | YES  |     | NULL    |       |
+---------------+-----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

But, it leaves out any indexes. That’s where SHOW coupled with the CREATE TABLE command returns more information than DESC:

1
2
3
4
5
6
7
8
9
10
mysql> SHOW CREATE TABLE degree_value\G
*************************** 1. row ***************************
       Table: degree_value
Create Table: CREATE TABLE `degree_value` (
  `pipe_id` smallint(6) DEFAULT NULL,
  `degree_amount` decimal(4,2) unsigned DEFAULT NULL,
  KEY `degree_value_ibfk_1` (`pipe_id`),
  CONSTRAINT `degree_value_ibfk_1` FOREIGN KEY (`pipe_id`) REFERENCES `pipe` (`pipe_id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.02 sec)

What user am I logged in as?
Try this:

1
2
3
4
5
6
7
mysql> SELECT USER();
+------------------+
| USER()           |
+------------------+
| j2112o@localhost |
+------------------+
1 row in set (0.00 sec)

To note: SELECT CURRENT_USER(); works as well.

What stored procedures or functions do I have written in this database? I vaguely remember a part of the name. Here you are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> SHOW PROCEDURE STATUS WHERE Db = 'asbuilt' AND Name LIKE '%short%'\G
*************************** 1. row ***************************
                  Db: asbuilt
                Name: proc_short_pipe
                Type: PROCEDURE
             Definer: j2112o@localhost
            Modified: 2018-06-26 10:34:08
             Created: 2018-06-26 10:34:08
       Security_type: DEFINER
             Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8mb4_unicode_ci
1 row in set (0.00 sec)

You can then use CREATE PROCEDURE syntax with SHOW to retrieve details of the procedures definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mysql> SHOW CREATE PROCEDURE proc_short_pipe\G
*************************** 1. row ***************************
           Procedure: proc_short_pipe
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`j2112o`@`localhost` PROCEDURE `proc_short_pipe`(IN p_wt_amt DECIMAL(4,3), OUT p_pipe_name VARCHAR(25), OUT p_pipe_length DECIMAL(4,2))
BEGIN
    DECLARE v_p_name VARCHAR(25);
    DECLARE v_length DECIMAL(4,2);
        SELECT p.pipe_name, p.pipe_length
        INTO v_p_name, v_length
        FROM pipe AS p
        INNER JOIN pw_wall_thickness AS pw
        ON p.pipe_id = pw.pw_pipe_id
        INNER JOIN wall_thickness AS wt
        ON pw.pw_thickness_id = wt.w_thickness_id
        WHERE wt.wall_thickness_amount = p_wt_amt
        ORDER BY p.pipe_length ASC
        LIMIT 1;
    SELECT CONCAT(v_p_name, ' has the shortest length of ', v_length);
END
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8mb4_unicode_ci
1 row in set (0.01 sec)

For more information, be sure and visit the SHOW Syntax Documentation page for other uses of SHOW.

These are but a few usage examples of SHOW. Explore the SHOW command more to discover its many uses to retrieve useful information. Feel free to share your comments below. Thanks for reading.
Explore the official MySQL 5.7 Online Manual for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.

Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

One thought on “Example uses of MySQL SHOW syntax.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.