I need to merge two MySql databases.
DBtwo is a copy of DBone.
I added data to a table_x in DBone, and I added datas to a table_y in DBtwo.
I need to keep data added in DBOne, while merging all other edits –such as deleted entries- made in DBTwo.
Schemas, then, are identical, while each DB have different data from different tables.
Thank you all for your help.
Edit:
I was forgetting. I added columns to table_x that are foreign keys to table_y. So DBone has some entries that have been inserted without columns I added to table_x in DBtwo. :/
Do you have a copy of the original database before the merge? If not, what you are asking is impossible. Here is the problem:
The original database has these records:
{a, b, c}
In table_x, you add a d, so now you have:
{a, b, c, d}
In table y, you remove a b, so you have:
{a, c}
You now attempt to merge these two data sets:
{a, b, c, d}
{a, c}
Without reference to the original, how do you know whether b and d should be in the new set. If they weren't in the original and you added them since the fork, they should be included. If they were in the original and you deleted them in table_y since the merge, they should not be included.
Assuming you do have access, you'll want to do something like:
insert into table_y
select *
from table_x x
where x.id not in (select id from table_x_original)
And then rename table_y to table_x (if you want table_x to be the new data source). If your ids are generated and you may have conflicts, you'll want to replace the * with a list of all the columns other than the id column to generate a new sequential id.
You could use a trigger. Please see the MySQL documentation for your implementation regarding, triggers, functions and procedures.
Related
I have created a SSIS Package that reads data from a CSV file and loads into table1 . the other data flow tasks does a look up on table 1 .Table1 has columns x , y, z, a ,b . Table 2 has columns a , b ,y,z Lookup is done based on columns y and z . Based on the column y and z , it is picking up a and b from table 1 and updating table 2 . The problem is the data gets updated but i get multiple rows of data thats is one without updation and one after updation .
I can provide more clear explanation if needed .
Fleshing out Nick's suggestion, I would get rid of your second data flow (the one from Table 2 to Table 2).
After the first Dataflow that populates table 1, then just do an EXECUTE SQL task that performs an UPDATE on Table 2, and joins to Table 1 to get the new data.
EDIT in response to comment:
You need to use a WHERE clause that will match rows uniquely. Apparently Model_Cd is not a UNIQUE column in JLRMODEL_DIMS. If you cannot make the WHERE clause unique because of the relationship between the two tables, then you need to select either an aggregate [Length (cm)] like MIN(), MAX() etc, or you need to use TOP 1, so that you only get one row from the subquery.
I am new in mysql. What I would to do is create a new table which is a copy of the original one table with one more column under a specific condition. Which condition appears as a new column is the new table. I mean:
Let table be a sequence of given point (x,y) I want to create the table temp being (x,y,r) where r = x^2 + y^2<1 But what I did is
CREATE temp LIKE table;
ALTER TABLE temp ADD r FLOAT;
INSERT INTO temp (x,y) SELECT * FROM table WHERE x*x+y*y<1;
UPDATE temp SET r=x*x+y*y;
It is ok, it gives what I want, but my database is much more bigger than this simple example and here I calculate twice the radius r in two table. It is not so good about optimization.
Is there a way to pass the clause into the new column directly?
Thanks in advance.
You should (almost) never store calculated data in a database. It ends up creating maintenance and application nightmares when the calculated values end up out of sync with the values from which they are calculated.
At this point you're probably saying to yourself, "Well, I'll do a really good job keeping them in sync." It doesn't matter, because down the road at some point, for whatever reason, they will get out of sync.
Luckily, SQL provides a nice mechanism to handle what you want - views.
CREATE VIEW temp
AS
SELECT
x,
y,
x*x + y*y AS r
FROM My_Table
WHERE
x*x + y*y < 1
You don't actually need to worry about doing the calculation twice. There is more overhead to doing an insert and update. So, you should do those calculations at the same time.
MySQL extends the use of the having clause, so this is easy:
CREATE temp LIKE table;
ALTER TABLE temp ADD r FLOAT;
INSERT INTO temp(x, y, r)
SELECT x, y, x*x+y*y as r
FROM table
HAVING r < 1;
It is quite possible that an additional table is not actually necessary, but it depends on how you are using the data. For instance, if you have rather complicated processing and are referring to temp multiple times and temp is rather smaller than the original data, then this could be a useful optimization.
Also, materializing the calculation in a table not only saves time (when the calculation is expensive, which this isn't), but it also allows building an index on the computed value -- something you cannot otherwise do in MySQL.
Personally, my preference is for more complicated queries rather than a profusion of temporary tables. As with many things with extremes, the best solution often lies in the middle (well, not really in the middle but temporary tables aren't all bad).
Rather than:
INSERT INTO temp (x,y) SELECT * FROM table WHERE x*x+y*y<1;
UPDATE temp SET r=x*x+y*y;
Try this:
INSERT INTO temp (x,y,r)
SELECT x,
y,
x*x+y*y AS r
FROM table
WHERE x*x+y*y<1;
I have a table with several columns Table1(Col A, Col B)
Now I have one more table with one column. Table2 (Col C)
What I want to do is:
Replace Col B of table1 with Col C of tabl 2.
Is it possible in SQL? I am using phpmyadmin to execute queries
Why I need to do this?
- I was playing around with the database structure and changed the type of text to integer which messed up the entries in the column
- Good thing: I have a backup excel file so now i am planning to replace the effected column to by the orginal values in the backedup excel file.
No can do.
You seem to be making an incorrect assumption, namely that the order of rows in a table is significant. Else what's confusing some of the commenters would be clear to you: there's no information in table2 to relate it to table1.
Since you still have the data in Excel, drop table2 and re-create it with rows having the key to table1. Then write a view to join them. Easiest is probably to insert that join result into a third table, and then drop the first two and rename the third.
I have two tables (let's call them A and B) with same structure and i need to synchronize data in them...
There's one primary key field, with same value in both tables, and several fields with value in table A and null (or obsolete value that need to be replaced with current value from table A) in table B... I need to copy value from table A to table B.
Is there any easy way (other than replication) to do this in mySQL 4.1?
thanks in advance
Try this -
UPDATE table_b b, table_a a
SET b.field1 = a.field1, b.field2 = a.field2
WHERE b.primary_key = a.primary_key
add the fields as required.
Can you just do:
INSERT INTO table1 (field1,field2,field3)
SELECT field1,field2,field3
FROM table2;
Or do you actually already have data in table2 and you need to update it rather than insert new columns?
I have an application which collects data into a mysql table. The table has no unique id column, so I can't reference a specific row by id.
I want to write a dump application which every day dumps the new rows added to the table to upload them elsewhere. I could do it by adding a unique id field and storing the last id dumped, but I don't want to add an id column just for that to the table.
So I thought I store the number of rows in the table at every dump and use that number as an offset next time the table is dumped (select * from table limit verylargenumber offset x). Of course, it works only if there is a guarantee new rows always inserted at the end of the table, so all new rows will be after the offset.
I assume I can rely on that. Am I right?
No this isn't the case. The database will move stuff around to optimize and make queries faster. You would have to add an order by clause to your query to ensure any sort of order. You should definitely consider adding a unique id to your table.
No, you aren't. There is no surety as to the order in which the engine will return the rows. A table without a unique ID is generally not such a godo idea anyways. In this case, you definitely have reason enough to use one.
Similar to a file system, unless the table is optimized or defragmented, deleted data will free up a "slot" where new data will be inserted. It isn't always appended to the end of the table.
So say you have 3 rows: A, B, C
If you delete B, then your table will essentially look like A, [free space], C
So if you insert D into your table, it will now look like: A, D, C
Your best bet is to use a unique auto incrementing key. This will also speed up queries.