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

MysqlImport

Mysql Problem Overview


I had successfully imported a database using command line, but now my pain area is how to import a single table with its data to the existing database using command line.

Mysql Solutions


Solution 1 - Mysql

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 a single database 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.

Solution 2 - Mysql

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

Solution 3 - Mysql

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

Solution 4 - Mysql

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.

Solution 5 - Mysql

First of all, login to your database and check whether the database table which you want to import is not available on your database.

If it is available, delete the table using the command. Else it will throw an error while importing the table.

DROP TABLE Table_Name;

Then, move to the folder in which you have the .sql file to import and run the following command from your terminal

mysql -u username -p  databasename  < yourtable.sql

The terminal will ask you to enter the password. Enter it and check the database.

Solution 6 - Mysql

It works correctly...

C:\>mysql>bin>mysql -u USERNAME DB_NAME < tableNameFile.sql

please note .sql file specified your current database..

Solution 7 - Mysql

We can import single table using CMD as below:

D:\wamp\bin\mysql\mysql5.5.24\bin>mysql -h hostname -u username -p passowrd databasename < filepath

Solution 8 - Mysql

  • It would be combination of EXPORT INTO OUTFILE and LOAD DATA INFILE

  • You need to export that single table with EXPORT INTO OUTFILE, this will export table data to a file. You can import that particular table using LOAD DATA INFILE

  • Refer doc1 , doc2

Solution 9 - Mysql

Also its working. In command form

cd C:\wamp\bin\mysql\mysql5.5.8\bin //hit enter
mysql -u -p databasename //-u=root,-p=blank

Solution 10 - Mysql

if you already have the desired table on your database, first delete it and then run the command below:

 mysql -u username -p  databasename  < yourtable.sql

Solution 11 - Mysql

From server to local(Exporting)

mysqldump -u username -p db_name table_name > path/filename.sql;
mysqldump -u root -p remotelab welcome_ulink > 
/home_local/ladmin/kakalwar/base/welcome_ulink.sql;

From local to server(Importing)

mysql -u username -p -D databasename < path/x/y/z/welcome_queue.sql
mysql -u root -p -D remotelab < 
/home_local/ladmin/kakalwar/instant_status/db_04_12/welcome_queue.sql

Solution 12 - Mysql

If you're in the pwd of an SQL dump and you need a table from that, do this:

sed -n '/-- Table structure for table `'TableNameTo_GrabHere'`/,/-- Table/{ /^--.*$/d;p }' dump_file_to_extract_from.sql > table_name_here.sql

Then just import the table you extracted from the above into the needed database

Solution 13 - Mysql

you can do it in mysql command instead of linux command.
1.login your mysql.
2.excute this in mysql command:
use DATABASE_NAME;
SET autocommit=0 ; source ABSOLUTE_PATH/TABLE_SQL_FILE.sql ; COMMIT ;

Solution 14 - Mysql

mysql -u root -p -D dbname < tablename.sql

Solution 15 - Mysql

-> mysql -h host -u user -p database_name table_name < test_table.sql

Solution 16 - Mysql

Using a temporary database could be a solution depending on the size of the database.

mysql -u [username] -p -e "create database tempdb"
mysql -u [username] -p tempdb < db.sql
mysqldump -u [username] -p tempdb _table_to_import_ > table_to_import.sql
mysql -u [username] -p maindb < table_to_import.sql

Solution 17 - Mysql

To import a table into database, if table is already exist then add this line in your sql file DROP TABLE IF EXIST 'table_name' and run command mysql -u username -p database_name < import_sql_file.sql. Please verify the sql file path before execute the command.

Solution 18 - Mysql

Open the backup file in the VScode and search the table name copy the create table and insert command for the table. Copy and execute those two commands in the database where it is required.

Solution 19 - Mysql

First of all take backup of your both database, step 2 select table which you want to export now select export button now download sql file now you have to import into another database simply select database and then import sql file ... simple and easy.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontismonView Question on Stackoverflow
Solution 1 - MysqlsushView Answer on Stackoverflow
Solution 2 - Mysqluser285594View Answer on Stackoverflow
Solution 3 - MysqlTanmay PatelView Answer on Stackoverflow
Solution 4 - MysqlPrabhakar UndurthiView Answer on Stackoverflow
Solution 5 - MysqlVarun P VView Answer on Stackoverflow
Solution 6 - MysqlRanjithView Answer on Stackoverflow
Solution 7 - MysqlDigishaView Answer on Stackoverflow
Solution 8 - MysqlShamit VermaView Answer on Stackoverflow
Solution 9 - MysqlmoneyView Answer on Stackoverflow
Solution 10 - MysqljsinaView Answer on Stackoverflow
Solution 11 - MysqlVenkatesh KakalwarView Answer on Stackoverflow
Solution 12 - MysqlSgt. StadinkoView Answer on Stackoverflow
Solution 13 - MysqlGeekLeiView Answer on Stackoverflow
Solution 14 - MysqlNids BarthwalView Answer on Stackoverflow
Solution 15 - MysqlKesha VivekiView Answer on Stackoverflow
Solution 16 - MysqleaorakView Answer on Stackoverflow
Solution 17 - MysqlVijayView Answer on Stackoverflow
Solution 18 - MysqlMahesh ShivaiahView Answer on Stackoverflow
Solution 19 - MysqlPankaj YadavView Answer on Stackoverflow