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

20

169

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.

Volitive answered 22/3, 2011 at 6:27 Comment(3)
export single table only and importWillow
See also this duplicate question: https://mcmap.net/q/116219/-can-i-restore-a-single-table-from-a-full-mysql-mysqldump-file/385571 with its more relevant answersKittiekittiwake
Possible duplicate of Can I restore a single table from a full mysql mysqldump file?Epigone
A
371

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 table into database

    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.

Alyss answered 22/3, 2011 at 6:32 Comment(6)
the -u was missed ;) mysqldump -u username -p databasename tableName > path/example.sqlHurricane
For importing you don't need to use mysql dump. mysql -u username -p databasename tableName < path/example.sql It should do your jobGev
The tableName name does not needed for all MySQL versions and it produces errors, so you may need to omit it!Haplology
I was getting a syntax error until I realized to be in bash and not mysql when executing.Seafowl
This would work only if I have access to the database from which I can export a specific table. However, if I have a full dump of the database and want to cherry-pick the table(s) to be imported, this solution won't work. The linked post Can I restore a single table from a full mysql mysqldump file? addresses that issue.Epigone
For importing table following is worked for me. mysql -u username -p -D dbname < tableName.sqlFlaw
O
49

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
Oviparous answered 4/8, 2013 at 20:46 Comment(0)
G
23

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

Graben answered 3/12, 2014 at 6:41 Comment(0)
F
6

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.

Fiduciary answered 16/9, 2019 at 8:12 Comment(0)
G
5

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.

Gev answered 6/4, 2015 at 12:27 Comment(2)
You do not need to specify the table name in the command line, as the exported SQL file has the relevant call to create the table. All you need is mysql -u your_user_name -p database_name < test.sql.Seaquake
Here's an example from an exported table, ` 22 DROP TABLE IF EXISTS account_product_prices; 23 /*!40101 SET @saved_cs_client = @@character_set_client /; 24 /!40101 SET character_set_client = utf8 */; 25 CREATE TABLE account_product_prices (`Seaquake
D
4

It works correctly...

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

please note .sql file specified your current database..

Dee answered 21/2, 2013 at 7:45 Comment(0)
S
4

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

Stringency answered 1/10, 2020 at 3:42 Comment(0)
N
3

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
Nucleoside answered 4/3, 2015 at 13:53 Comment(0)
B
2
  • 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

Bevel answered 22/3, 2011 at 6:56 Comment(0)
R
2

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
Rubric answered 11/4, 2014 at 11:58 Comment(0)
H
2

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 ;

Hillinck answered 9/11, 2016 at 9:56 Comment(0)
R
2

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
Risser answered 12/11, 2018 at 10:31 Comment(0)
P
2

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
Preselector answered 4/12, 2018 at 10:42 Comment(1)
For beginers please note u have to operate from terminal not inside the mysql after hitting the command it will ask for the password the db_passwordPreselector
B
1

To import a particular table in database follow below command. Here table_name.sql is dump of taht particular table that you are going to import

mysql -u root -p database_name table_name < table_name.sql

To export a particular table from database follow below command.

mysqldump -u root -p database_name table_name > table_name.sql
Burns answered 21/3, 2017 at 11:23 Comment(1)
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how and why this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.Astray
U
1

Use the below command to import a single table into the database on RDS

mysql -h rds_end_point -u username -p databasename < example.sql

Ununa answered 28/7, 2022 at 17:51 Comment(0)
P
0
-> mysql -h host -u user -p database_name table_name < test_table.sql
Permit answered 10/12, 2019 at 7:12 Comment(1)
the table_name param is not available in latest mysql versionsPersonal
C
0

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
Castlereagh answered 17/12, 2019 at 23:21 Comment(0)
F
0

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.

Fabrication answered 18/4, 2020 at 14:22 Comment(0)
S
0

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.

Serle answered 15/3, 2022 at 11:46 Comment(0)
B
-1

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.

Bandanna answered 17/3, 2021 at 4:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.