Can I Import an updated structure into a MySQL table without losing its current content?

后端 未结 6 1570
南旧
南旧 2021-02-08 01:30

We use MySQL tables to which we add new fields from time to time as our product evolves. I\'m looking for a way to export the structure of the table from one copy of the db, to

6条回答
  •  臣服心动
    2021-02-08 02:02

    I just had the same problem and solved it this way:

    Export the structure of the table to update. Export the structure of the development table.

    run this code for the first file "update.sql" needs to be changed according to your exported filename.

    cat update.sql|awk -F / '{
     if(match($0, "CREATE TABLE")) {
       { FS = "`" } ; table = $2
     } else {
       if(match($0,"  `")) {
         gsub(",",";",$0)
         print "ALTER TABLE `" table "` ADD" $0
        }
     }
    }' > update_alter.sql
    

    run the same command for the second file

    cat development.sql|awk -F / '{
     if(match($0, "CREATE TABLE")) {
       { FS = "`" } ; table = $2
     } else {
       if(match($0,"  `")) {
         gsub(",",";",$0)
         print "ALTER TABLE `" table "` ADD" $0
        }
     }
    }' > development_alter.sql
    

    run this command to find the differences in the output files

    diff --changed-group-format='%<' --unchanged-group-format='' development_alter.sql update_alter.sql > update_db.sql
    

    In the file update_db.sql there will now be the code you are looking for.

提交回复
热议问题