MySQL SELECT INTO Syntax Part 3 – DUMPFILE with examples.

We have seen in previous blog posts, 2 uses of the MySQL SELECT INTO syntax. Yet, there is a third use to know and learn about which we will explore in this blog post.


computer-code-on-screen

Photo by Markus Spiske on Unsplash (Note: Not MySQL code)

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

Be sure and visit the 2 previous blog posts in the series on MySQL SELECT INTO syntax I have written:

Let’s get an idea of the arbitrary data present in the demonstration table for this blog post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mysql> SELECT *
    -> FROM demo;
+----+---------------+
| id | name          |
+----+---------------+
|  1 | Apples        |
|  2 | Mark          |
|  3 | Ham-n-cheese  |
|  4 | 57 Chevy      |
|  5 | Little River  |
|  6 | Happy Time    |
|  7 | B-52's        |
|  8 | Peanut Butter |
|  9 | Moon Dance    |
| 15 | Demo Table    |
| 16 | Max Ammount   |
| 17 | Happy Days    |
| 18 | Blue Moon     |
| 19 | Pale Rider    |
| 20 | Harmony       |
+----+---------------+
15 rows in set (0.00 sec)

The SELECT INTO DUMPFILE version is somewhat different from the other two variants. The SELECT INTO documentation mentions that this version of the syntax is useful for saving the contents of a BLOB data type to a file.
A key difference is SELECT INTO DUMPFILE only writes one row of data to the target file.

All the same stipulations must exist for a user to run this command as I highlighted in previous blog posts regarding the FILE privilege – the user must have it and the file should not already exist.
In the below command, I’ll query all the data from table demo and save it with the INTO DUMPFILE option:

1
2
3
4
mysql> SELECT *
    -> INTO DUMPFILE '/var/lib/mysql-files/dmp1'
    -> FROM demo;
ERROR 1172 (42000): Result consisted of more than one row

Although an error is returned with that command, let’s check the results of that SELECT INTO DUMPFILE (Note: For my Linux install, the directory location and file must be accessed by the root user):

1
2
shell:/var/lib/mysql-files# cat dmp1
1Apples2Mark

We can see that while multiple rows exist in table demo, only one row was written to the file dmp1. But, the one written row of data is in fact the first 2 rows of table demo. Also make note that there are no separators nor any formatting present for the written data.

For curiosity’s sake, since we know that without an ORDER BY clause in the query, any results set ordering is random and not guaranteed, let’s re-issue that same command, writing the query results to another file:

1
2
3
4
mysql> SELECT *
    -> INTO DUMPFILE '/var/lib/mysql-files/dmp1_a'
    -> FROM demo;
ERROR 1172 (42000): Result consisted of more than one row

Then check that file’s contents:

1
2
shell:/var/lib/mysql-files# cat dmp1_a
1Apples2Mark

Which entails the first two rows being written again.

I’ll use a WHERE clause and filter the query results, saving those as well:

1
2
3
4
5
mysql> SELECT *
    -> INTO DUMPFILE '/var/lib/mysql-files/dmp2'
    -> FROM demo
    -> WHERE id = 1;
Query OK, 1 row affected (0.01 sec)

Checking those results, we can see that only one row was written to the file.

1
2
shell:/var/lib/mysql-files# cat dmp2
1Apples

I noted earlier in the post that the documentation mentions using SELECT INTO DUMPFILE for saving BLOB values to a file.

I have this simple table:

1
2
3
4
5
6
7
mysql> DESC some_pics;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| pic   | blob | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
1 row in set (0.03 sec)

And this data, which is a path to a picture of my two dogs:

1
2
3
4
5
6
7
mysql> SELECT * FROM some_pics;
+------------------------------------------+
| pic                                      |
+------------------------------------------+
| /home/linux_user/Pictures/BooAndAsia.jpg |
+------------------------------------------+
1 row in set (0.00 sec)

picture-of-two-dogs-side-by-side

My dogs Boo and Asia(RIP girl)

Now let’s use the below SELECT INTO DUMPFILE command to save the table data to a file:

1
2
3
4
mysql> SELECT *
    -> INTO DUMPFILE '/var/lib/mysql-files/pic1'
    -> FROM some_pics;
Query OK, 1 row affected (0.19 sec)

And then, check the contents of that created file:

1
2
shell:/var/lib/mysql-files# cat pic1
/home/linux_user/Pictures/BooAndAsia.jpg

SELECT INTO DUMPFILE along with the other two syntax versions of SELECT INTO, are available options for covering your needs to save query results to files and/or variables within MySQL.

Like what you have read? See anything incorrect? Please comment below and 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

Hey thanks for commenting! Leave a Reply

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