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

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`;

Related

Alter datatypes in the table -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

Multiple data sources with different timeintervalls in the same table

I have this situation:
Until recently I had a table in my db which got updated through a python script (running as a cronjob).
I've handled this the following way (commands in mysqldb-python):
DROP TABLE tmp IF EXISTS
CREATE TABLE tmp LIKE original
INSERT INTO tmp...
DROP TABLE original
ALTER TABLE tmp RENAME TO original
Now I have 2 sources of data for the same table. The table is going to be updated from the 2 sources in different timeintervalls (source 1 each 'x minutes', source 2 each 'y minutes'). I can't create a tmp table and insert the data there because the data from the other source isn't going to be included. I thought about adding a field Sourceto the table and then in the scripts:
DELETE FROM original WHERE Source = 'currentsource'
INSERT INTO original ...
Problem with this method is that the ID AUTO_INCREMENT doesn't reset. I don't particularly want to check for the current max ID and set the increment if there's a better solution.
Which solution would you suggest?

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

How insert new data to table and remove old without blocks users to read old data until end of process(MySql)

The case:
Have huge table that every 24 hours need to delete all data and get new data instead.
How can I truncate this table and insert new data without block users to read data from this table?
What I thinking to do: (please write which option better or suggest more proper option or standard solution)
Option1:
1. insert new data to temp table
2. drop old table
3. rename temp table to the table name
Option2(problem: user cant access the table data in middle):
1. truncate the table
2. insert new data to table
1.insert new data to temp table
2.CREATE TABLE newtable select * from oldtable where 1=2
3.drop old table
4.INSERT INTO newtable select * from temp
If you have any old data to keep:
Add a id column to your table. Fill it with a hash value computed by the value of all columns in a row. Load the new data into a temporary table. Update the old data with the changed and new rows.
Otherwise your first solution is the way to go, since the renaming of a table does not take mch time.

Mysql query to copy the structure of a table to create another table

Looking for help in creating a Mysql query to copy the structure of an existing table to create another table.
To create a table as in an exact replica of another table:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
If you want to also copy the contents of the table you can do:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;
If you want to copy the table structure including its keys, then you should use:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
To copy the whole table
CREATE TABLE `new_table_name` SELECT * FROM `old_table_name`;
It will create the table and insert all the data from the old table but without bringing the keys from the old table. So you will need to set the keys for the new table.
Its old thread but if someone needed. If you want create query that will create exactly copy of existing table.
show create table <existing table name>
MySQL query to copy the structure of a table to create another table structure without data another way is...
CREATE TABLE `table_name_new` select * from `table_name_old` limit 0;
also you can use this:
CREATE TABLE 'new_table_name' SELECT * FROM 'pattern_table' where 1=0;