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

∥☆過路亽.° 提交于 2021-02-17 21:39:26

问题


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 another, without erasing the contents of the table I'm importing to.

For example say I have copies A and B of a table, and I add fields X,Y,Z to table A. Is there a way to copy the changed structure (fields X,Y,Z) to table B while keeping its content intact?

I tried to use mysqldump, but it seems I can only copy the whole table with its content, overriding the old one, or I can use the "-d" flag to avoid copying data (dumping structure only), but this will create an empty table when imported, again overriding old data.

Is there any way to do what I need with mysqldump, or some other tool?


回答1:


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.




回答2:


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




回答3:


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



回答4:


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




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/4580417/can-i-import-an-updated-structure-into-a-mysql-table-without-losing-its-current

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!