Jump to content

Modifications to an existing table


WarKirby

Recommended Posts

Hi everyone. I'm very new to mysql and php, learning as I go. Working on a database which has a problematic design oversight. The primary key is a combination of two fields, on the assumption that these fields would never be the same, but that is not the case as I find the need to create multiple records with identical values in these fields

 

So I want to alter this table, to add a new AUTO INCREMENT column, and change the primary key to that.  The table currently has just under 10,000 records in it which are valuable, so I can't afford to mess this up. Can anyone advise as to what I should do? I'd assume the ALTER query will be needed.

 

Thank you in advance for replies

Link to comment
Share on other sites

1st rule of database tinkering. Never work on live database. Do all your tests on separate database.

This way there is no possibility of you messing anything up.

 

Perhaps the easiest way is to:

1. Create new table with AUTO_INCREMENT column in it and all other fields from original table.

2. Use INSERT INTO ... SELECT to fill the new table with values (and automatically generate AUTO_INCREMENT values)

3. Drop the original table

4. Rename your new table, to what it is supposed to be called

Link to comment
Share on other sites

1st rule of database tinkering. Never work on live database. Do all your tests on separate database.

This way there is no possibility of you messing anything up.

 

I was planning to do all tests and stuff on a backup of the live database first, and then run it on the live one when I'm sure everything is fine. That a bad idea?

 

Perhaps the easiest way is to:

1. Create new table with AUTO_INCREMENT column in it and all other fields from original table.

2. Use INSERT INTO ... SELECT to fill the new table with values (and automatically generate AUTO_INCREMENT values)

3. Drop the original table

4. Rename your new table, to what it is supposed to be called

 

Does this mean the auto increment column won't auto fill itself when it's added? I was hoping it would do that.

 

So if I understand right, I just run a loop that copies all the data one row at a time from the old table, to the new? (ordered by date of course) And I don't put anything into the auto increment column, because it will auto fill itself as each row is added?

 

Link to comment
Share on other sites

 

I was planning to do all tests and stuff on a backup of the live database first, and then run it on the live one when I'm sure everything is fine. That a bad idea?

 

 

Let's straighten a few things:

1. Production database - that's what's being used by your application/website everyday. You really do not want to screw this up.

2. Backup - you do (you really should anyway) do regular copies of your production database, so that you minimise the risk of loosing your data

3. Develoment database - that's something you preferably set up on your own PC, so that you can do all sorts of experiments with it, without worrying about doing something wrong.

 

Does this mean the auto increment column won't auto fill itself when it's added? I was hoping it would do that.

 

As far as I remember it won't. But I might be wrong. You can always try ;)

 

So if I understand right, I just run a loop that copies all the data one row at a time from the old table, to the new? (ordered by date of course) And I don't put anything into the auto increment column, because it will auto fill itself as each row is added?

 

No. By using somethin like

INSERT INTO newTable (field1,field2,field3) SELECT field1,field2,field3 FROM oldTable

you virtually copy all contents from oldTable to newTable on one go.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.