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.
-
3export single table only and importShakti Singh– Shakti Singh2011-03-22 06:31:03 +00:00Commented Mar 22, 2011 at 6:31
-
4See also this duplicate question: stackoverflow.com/q/1013852/385571 with its more relevant answersMattBianco– MattBianco2016-04-29 13:53:44 +00:00Commented Apr 29, 2016 at 13:53
-
2Possible duplicate of Can I restore a single table from a full mysql mysqldump file?codeforester– codeforester2018-10-27 00:01:07 +00:00Commented Oct 27, 2018 at 0:01
20 Answers
Linux :
In command line
mysql -u username -p databasename < path/example.sql
put your table in example.sql
Import / Export for single table:
Export table schema
mysqldump -u username -p databasename tableName > path/example.sqlThis will create a file named
example.sqlat the path mentioned and write thecreate tablesql command to create tabletableName.Import a single table into database
mysql -u username -p databasename < path/example.sqlThis command needs an sql file containing data in form of
insertstatements for tabletableName. All theinsertstatements will be executed and the data will be loaded.
6 Comments
tableName name does not needed for all MySQL versions and it produces errors, so you may need to omit it!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.
Comments
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.
2 Comments
mysql -u your_user_name -p database_name < test.sql.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 (`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
Comments
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
1 Comment
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
1 Comment
-> mysql -h host -u user -p database_name table_name < test_table.sql
1 Comment
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