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

前端 未结 18 1857
你的背包
你的背包 2020-12-22 15:25

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.

相关标签:
18条回答
  • 2020-12-22 15:41

    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 ;

    0 讨论(0)
  • 2020-12-22 15:42

    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.

    0 讨论(0)
  • 2020-12-22 15:42

    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

    0 讨论(0)
  • 2020-12-22 15:45

    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
    
    0 讨论(0)
  • 2020-12-22 15:46
    mysql -u root -p -D dbname < tablename.sql
    
    0 讨论(0)
  • 2020-12-22 15:48

    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.

    0 讨论(0)
提交回复
热议问题