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

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

提交回复
热议问题