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.
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 ;
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.sql
This will create a file named example.sql
at the path mentioned and write the create table
sql command to create table tableName
.
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.
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
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
mysql -u root -p -D dbname < tablename.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.