Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

import

mysql

People also ask

How do I add a table to a MySQL database?

The general syntax for creating a table in MySQL is: CREATE TABLE [IF NOT EXISTS] table_name( column_definition1, column_definition2, ........, table_constraints ); Note: [IF NOT EXISTS] verifies if there is an identical table in the database. The query will not be executed if an identical table already exists.

How do I export just one table in MySQL?

in order to dump a set of one or more tables, shell> mysqldump [options] db_name [tbl_name ...] Show activity on this post. This will export the tableName to the file tableName.

How do you import a table?

Browse to the source database, select it, and then click Open. Select Import tables, queries, forms, reports, macros, and modules into the current database and click OK to open the Import Objects dialog box. In the Import Objects dialog box, click each tab and select the objects you want.


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.sql
mysqldump --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.


Importing the Single Table

To import a single table into an existing database you would use the following command:

mysql -u username -p -D database_name < tableName.sql

Note:It is better to use full path of the sql file tableName.sql


Command Line

Import / Export for single table:

Exporting table schema

 -> mysqldump -u your_user_name -p your_database_name table_name > test.sql

This will create a file named test.sql and creates table sql command to create table table_name.

Importing data into table

 -> mysql -u your_user_name -p database_name table_name < test.sql

Make sure your test.sql file is in the same directory, if not navigate through the path and then run the command.