• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Execute MySQL statements from a text file

Execute MySQL statements from a text file

It’s possible to execute statements in MySQL from a text file from the command line or from the MySQL command line shell. This can be useful, for example, if you’ve saved data using mysqldump and need to load it back into the database.

Example SQL file

The text file must contain SQL statements for this to work, e.g.:

INSERT INTO mytable (name, value) VALUES ('foo', 'bar');

The example above inserts a single record, but the SQL file could have multiple SQL statements, separated by a semi-colon. These SQL statements can do any valid SQL: inserting, deleting, or updating records; creating, modifying or dropping tables; etc.

Executing the file from the command line

If the file is at e.g/ /tmp/myfile.sql you can load it into the "test" database using the "chris" username like so:

mysql -u chris -p test < /tmp/myfile.sql

The -p flag says you want to specify the password and it will ask for the password before the SQL file is executed.

Executing the file from within the MySQL command line shell

If you are already working from within the MySQL command line shell, you can execute the SQL file without exiting from the MySQL shell like so (note that the mysql> bit is the MySQL shell command prompt and you don’t need to type that part in):

mysql> source /tmp/myfile.sql

You can also do it like this:

mysql> . /tmp/myfile.sql

Note that this will only work from within the MySQL command line shell and not another client such as phpMyAdmin.

Check Out These Related posts:

  1. jQuery: show plain text in a password field and then make it a regular password field on focus
  2. Load data into MySQL with foreign key constraint issues Part 2
  3. Toggling a password field between plain text and password
  4. jQuery: show plain text in a password field and then make it a regular password field on focus – Part 2

Filed Under: MySql

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap