Welcome Guest, Not a member yet? Register   Sign In
Mysql DB move 1 column (w/ data) from one table to another seamlessly?
#3

[eluser]yelirekim[/eluser]
I think probably most people are having a hard time understanding exactly what you're asking, but I'll see if I can't clarify and help a little:

You have two tables, both of them store information for user profiles (the word member is a little ambiguous) on your site, like username, password etc. One of the tables is currently being used live on the site, but doesn't have the functionality you want in it, so you also have another table which you're hoping to move all of the current user data to which includes extra fields and extra functionality.

The easiest way to do this would just be to simply run a for loop which moves all of the data from one table to the other, and maps the columns, like this:

Code:
$query = $this->db->get('live_users_table');

foreach($query->result() as $user)
{
    $new_user_record = array('username' => $user->username, 'password' => $user->password, 'some_other_attribute' => $user->other_attribute);
    $this->db->insert('new_users_table',$new_user_record);
}

But if you have an enormous users table, that might take a while or time out, and you could run into a messy situation with that.

If I were doing this I'd just handle all of it from the MySQL prompt by doing the following things:

1. creating a copy of the old users table:

Code:
CREATE TABLE new_users_table SELECT * FROM live_users_table;

2. Removing any columns from the table that you don't want anymore

Code:
ALTER TABLE new_users_table DROP COLUMN unneeded_user_field;

3. Adding any columns to the table which your authentication library will expect

Code:
ALTER TABLE new_users_table ADD COLUMN user_permissions INT UNSIGNED NOT NULL DEFAULT 0;

4. Use that new table to test your features out and debug, then when you're ready to switch the live site over to using the new table, take the site down, do your application updates, and rename the tables accordingly:

Code:
RENAME TABLE live_users_table TO archive_users_table;
RENAME TABLE new_users_table TO live_users_table;

Hope this helps!


Messages In This Thread
Mysql DB move 1 column (w/ data) from one table to another seamlessly? - by El Forum - 09-17-2008, 06:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB