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

后端 未结 6 1573
南旧
南旧 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 01:56

    What I usually do is store each and every ALTER TABLE statement run on the development table(s), and apply them to the target table(s) whenever necessary.

    There are more sophisticated ways to do this (like structure comparison tools and such), but I find this practice works well. Doing this on a manual step by step basis also helps prevent accidental alteration or destruction of data by structural changes that change a field's type or maximum length.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-08 02:14

    No it isn't possible because MySql is using mariaDB version. In mariaDB version structure of a table are arranged in memory and that memory shared with your byte data. So when we try to import a structure (or a table) it alter that whole memory block.

    0 讨论(0)
  • 2021-02-08 02:15

    for your case, it might just need to perform an update

    alter table B add column x varchar(255);
    alter table B add column y varchar(255);
    alter table B add column z varchar(255);
    
    update A,B 
    set 
      B.x=A.x,
      B.y=A.y,
      B.z=A.z
    where A.id=B.id; <-- a key that exist on both tables
    
    0 讨论(0)
  • 2021-02-08 02:18

    There is a handy way of doing this but need a little bit editing in a text editor : This takes about Max 10Min in Gedit Under Linux !!


    Export you table & save it in : localTable.sql

    Open it in a text edior (Gedit) You will see something like this :

    CREATE TABLE IF NOT EXISTS `localTable` (
      `id` int(8) NOT NULL AUTO_INCREMENT,
      `date` int(10) NOT NULL,
        # Lot more Fields .....
        #Other Fields Here
    

    After Just Remove :

    • Anything after the closing ) parenthese
    • CREATE TABLE IF NOT EXISTS localTable (
    • Change all , to ; in each line like thats you execute all this once (,\n to ;\n)
    • remove all ADDPRIMARY KEY (id);ADDKEY created_by (created_by) !
    • And just Keep Fields you are interested in

    You will have this

      `id` int(8) NOT NULL AUTO_INCREMENT,
      `date` int(10) NOT NULL,
        # Lot more Fields .....
        #Other Fields Here
    

    Add to the begining of each line ALTER TABLE localTable ADD

     ALTER TABLE `localTable` ADD `id` int(8) NOT NULL AUTO_INCREMENT,
     ALTER TABLE `localTable` ADD  `date` int(10) NOT NULL,
       ALTER TABLE `localTable` ADD #to each more Fields .....
        #Other Fields Here
    

    That's it we can make this ab Automated Script by adding a Shell Script to do this job .

    After you know what you have to do Import it in the 'remoteTable' ;)

    Thanks

    0 讨论(0)
  • 2021-02-08 02:20

    Lazy way: export your old data and struct, import your actual struct, import only your old data. Works to me in the test.

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