How to import a single table in to mysql database using command line How to import a single table in to mysql database using command line mysql mysql

How to import a single table in to mysql database using command line


Linux :

In command line

 mysql -u username -p  databasename  < path/example.sql

put your table in example.sql

Import / Export for single table:

  1. Export table schema

    mysqldump -u username -p databasename tableName > path/example.sql

    This will create a file named example.sql at the path mentioned and write the create table sql command to create table tableName.

  2. Import data into table

    mysql -u username -p databasename < path/example.sql

    This command needs an sql file containing data in form of insert statements for table tableName. All the insert statements will be executed and the data will be loaded.


Export:

mysqldump --user=root databasename > whole.database.sqlmysqldump --user=root databasename onlySingleTableName > single.table.sql

Import:

Whole database:

mysql --user=root wholedatabase < whole.database.sql

Single table:

mysql --user=root databasename < single.table.sql


All of these options are fine, if you have the option to re-export the data.

But if you need to use an existing SQL file, and use a specific table from it, then this perl script in the TimeSheet blog, with enable you to extract the table to a separate SQL file, and then import it.

The original link is dead, so it is a good thing that someone has put it instead of the author, Jared Cheney, on GitHub, in this link.