mysqldump table without dumping the primary key

后端 未结 10 1412

I have one table spread across two servers running MySql 4. I need to merge these into one server for our test environment.

These tables literally have millions of r

相关标签:
10条回答
  • 2020-12-24 01:43

    Use a dummy temporary primary key:

    Use mysqldump normally --opts -c. For example, your primary key is 'id'. Edit the output files and add a row "dummy_id" to the structure of your table with the same type as 'id' (but not primary key of course). Then modify the INSERT statement and replace 'id' by 'dummy_id'. Once imported, drop the column 'dummy_id'.

    0 讨论(0)
  • 2020-12-24 01:43

    jimyi was on the right track.

    This is one of the reasons why autoincrement keys are a PITA. One solution is not to delete data but add to it.

    CREATE VIEW myView AS
    SELECT id*10+$x, name, email FROM users
    

    (where $x is a single digit uniquely identifying the original database) either creating the view on the source database (which you hint may not be possible) or use an extract routine like that described by Autocracy or load the data into staging tables on the test box.

    Alternatively, don't create the table on the test system - instead put in separate tables for the src data then create a view which fetches from them both:

    CREATE VIEW users AS
    (SELECT * FROM users_on_a) UNION (SELECT * FROM users_on_b)
    

    C.

    0 讨论(0)
  • 2020-12-24 01:50

    I like the temporary table route.

    create temporary table my_table_copy
    select * from my_table;
    
    alter table my_table_copy drop id;
    
    // Use your favorite dumping method for the temporary table
    

    Like the others, this isn't a one-size-fits-all solution (especially given OP's millions of rows) but even at 10^6 rows it takes several seconds to run but works.

    0 讨论(0)
  • 2020-12-24 01:52

    You can create a view of the table without the primary key column, then run mysqldump on that view.

    So if your table "users" has the columns: id, name, email

    > CREATE VIEW myView AS
      SELECT name, email FROM users
    

    Edit: ah I see, I'm not sure if there's any other way then.

    0 讨论(0)
  • 2020-12-24 02:00

    This is a total pain. I get around this issue by running something like

    sed -e "s/([0-9]*,/(/gi" export.sql > expor2.sql 
    

    on the dump to get rid of the primary keys and then

    sed -e "s/VALUES/(col1,col2,...etc.) VALUES/gi" LinxImport2.sql > LinxImport3.sql
    

    for all of the columns except for the primary key. Of course, you'll have to be careful that ([0-9]*, doesn't replace anything that you actually want.

    Hope that helps someone.

    0 讨论(0)
  • 2020-12-24 02:02
    1. Clone Your table
    2. Drop the column in clone table
    3. Dump the clone table without the structure (but with -c option to get complete inserts)
    4. Import where You want
    0 讨论(0)
提交回复
热议问题