Alter datatypes in the table -mysql - mysql

I need to alter the huge table around 200GB (INNODB).
As i seen many blogs, there is suggestion for creating a new structure, copying the table content to new table and rename the tables.
As given below
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
But if do above steps, How could we handle the newly inserted data into original table. Since many process updating and inserting the records into original table.
I have one master and 8 slaves

Related

MySQL renaming and create table at the same time

I need to rename MySQL table and create a new MySQL table at the same time.
There is critical live table with large number of records. master_table is always inserted records from scripts.
Need to backup the master table and create a another master table with same name at the same time.
General SQL is is like this.
RENAME TABLE master_table TO backup_table;
Create table master_table (id,value) values ('1','5000');
Is there a possibility to record missing data during the execution of above queries?
Any way to avoid missing record? Lock the master table, etc...
What I do is the following. It results in no downtime, no data loss, and nearly instantaneous execution.
CREATE TABLE mytable_new LIKE mytable;
...possibly update the AUTO_INCREMENT of the new table...
RENAME TABLE mytable TO mytable_old, mytable_new TO mytable;
By renaming both tables in one statement, they are swapped atomically. There is no chance for any data to be written "in between" while there is no table to receive the write. If you don't do this atomically, some writes may fail.
RENAME TABLE is virtually instantaneous, no matter how large the table. You don't have to wait for data to be copied.
If the table has an auto-increment primary key, I like to make sure the new table starts with an id value greater than the current id in the old table. Do this before swapping the table names.
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='mydatabase' AND TABLE_NAME='mytable';
I like to add some comfortable margin to that value. You want to make sure that the id values inserted to the old table won't exceed the value you queried from INFORMATION_SCHEMA.
Change the new table to use this new value for its next auto-increment:
ALTER TABLE mytable_new AUTO_INCREMENT=<increased value>;
Then promptly execute the RENAME TABLE to swap them. As soon as new rows are inserted to the new, empty table, it will use id values starting with the increased auto-increment value, which should still be greater than the last id inserted into the old table, if you did these steps promptly.
Instead of renaming the master_backup table and recreating it, you could
just create a backup_table with the data from the master_table for the first backup run.
CREATE TABLE backup_table AS
SELECT * FROM master_table;
If you must add a primary key to the backup table then run this just once, that is for the first backup:
ALTER TABLE backup_table ADD CONSTRAINT pk_backup_table PRIMARY KEY(id);
For future backups do:
INSERT INTO backup_table
SELECT * FROM master_table;
Then you can delete all the data in the backup_table found in the master_table like:
DELETE FROM master_table A JOIN
backup_table B ON A.id=B.id;
Then you can add data to the master_table with this query:
INSERT INTO master_table (`value`) VALUES ('5000'); -- I assume the id field is auto_incrementable
I think this should work perfectly even without locking the master table, and with no missing executions.

In mysql how to insert a column with huge data contained in table with no downtime

If a table in MySQL containing suppose 1 million record, how can I add a column at any position with no downtime expected.
MySQL's ALTER TABLE performance can become very frustrating with very large tables. ALTER statements makes a new temporary table, copies records from your existing table into the new table even if the data wouldn't strictly need to be copied, and then replaces the old table with the new table.
Suppose you have a table with one million records and if you try to add 3 columns in it, then it will certainly copy the table 3 times, which means coping 3 million records.
A faster way of adding columns is to create your own new table, then select all of the rows from the existing table into it. You can create the structure from the existing table, then modify the structure however you’d like, then select in the data. Make sure that you select the information into the new table in the same order as the fields are defined.
1. CREATE TABLE new_table LIKE table
2. INSERT INTO new_table SELECT * FROM table
3. RENAME TABLE table = old_table, table = new_table;
If you have foreign key constraints you can handle these foreign keys using
SET FOREIGN_KEY_CHECKS = 0;

MySQL table "crashed" after column type update

My MySQL table "crashed" after updating column types. I changed VARCHAR to INT and added some new columns too.
After that, when I'd like to view the table entries, every software just keeps loading and crashing... I can't even make any queries to the table. All I can do is look at the list of columns of the table. (I've tried with PhpMyAdmin, HeidiSQL and MySQL Workbench). Changes are made with HeidiSQL.
What should I do? This is the first time this happens and I've been using HeidiSQL for a long time.
You have changed VARCHAR to INT in the table schema. At first sight we are correct, just change alter the column and mysql server do as without showing any error But when you alter the table, mysql server have to do lot of work. first store the records temporary and recreate the schema and insert the record. If your table have many records then it is tough to alter. So either truncate table and alter the schema and reinsert or create other table and rename it latter.
In my opinion the solution is create table as you want schema.
Insert the record in new table from table.
eg- insert into new_table (col1, col2) select col1, col2 from old_table;
drop the old table
drop table old_table
rename the new table with old table
rename new_table to old_table

TEMPORARY table in Mysql

If two users create two TEMPORARY tables at the same time in my mysql database using a PHP script, will it create two different tables? Can those users use their own table without facing any trouble and will those table be automatically deleted?
thanks :)
CREATE TEMPORARY TABLE TempTable ( ID int, Name char(100) ) TYPE=HEAP;
INSERT INTO TempTable VALUES( 1, "Foo bar" );
SELECT * FROM TempTable;
DROP TABLE TempTable;
As stated in the manual:
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)
Temporary tables are created in a current session; so, two users can create two temp. tables at the same time in their threads. These tables will be removed on session closing.
You can use [IF NOT EXISTS] in this query...
reference

How can i synchronize a temp table and a normal table?

i have a shop and the products are stored in a csv file. The csv file are imported by a cronjob in a temp table. my problem is to synchronize the temp table with the normal table (productive). some rows must be updated, added or deleted. I can't import the CSV file in the productive table because the intervall must be every 30 minutes.
Does anybody knows a program for this problem? For windows and mysql 5.0.x?
greetings!
You could just replace the original table with the temporary table:
DROP TABLE `ORIGINAL_TABLE`;
RENAME TABLE `TEMPORARY_TABLE` TO `ORIGINAL_TABLE`;
Or even safer...back up the original table first...
DROP TABLE IF EXISTS `BACKUP_TABLE`;
CREATE TABLE `BACKUP_TABLE` LIKE `ORIGINAL_TABLE`;
INSERT INTO `BACKUP_TABLE` SELECT * FROM `ORIGINAL_TABLE`;
DROP TABLE `ORIGINAL_TABLE`;
RENAME TABLE `TEMPORARY_TABLE` TO `ORIGINAL_TABLE`;