Update in two columns, two tables

4

I have to update two columns in separate tables, but I do not know the commands very well, I would not like to use triggers. Will be updated around 100 records.
Here is an example:

Table a

id    nomes  nota
1     SOen   etc1
2     SOpt   etc2


Table b

id    nomes  outros
1     SOen   i
2     SOen   rre
3     SOpt   le
4     SOen   van
5     SOpt   te

That is, when I update a row of the a table, all rows with the same name as the a table in the b should also be updated, leaving the same name changed in the a table.

Example of effect of desired update:

Update a SET nomes = SOpt_BETAP WHERE nomes = SOpt

Table a | Updated table with new names

id    nomes       nota
1     SOen        etc1
2     SOpt_BETA   etc2


Table b | Table b is also updated with new names

id    nomes       outros
1     SOen        i
2     SOen        rre
3     SOpt_BETA   le
4     SOen        van
5     SOpt_BETA   te

It would be beneficial to me and to other users if I could make a brief explanation of each parameter used as ON , JOIN etc, explaining the function of it there, for those who also have doubts.

    
asked by anonymous 15.06.2014 / 00:25

3 answers

2

I think that it is not possible to do this with JOIN between tables, since after the first UPDATE in one of the tables, a href="http://sqlfiddle.com/#!2/f0c677/1"> example ).

But I propose two alternatives:

Alternative 1: Foreign key in second table

If you reference the IDs of table A in table B, instead of having the names repeated in both tables, only table A would need to be updated. Table B would look like this:

id    a_id        outros
1     1           i
2     1           rre
3     2           le
4     1           van
5     2           te

Alternative 2: Make two JOIN s different

I do not quite understand why you want to update the two tables in one operation. It would be much simpler to update each one separately:

UPDATE tabelaA SET nomes = 'SOpt BETA' WHERE nomes = 'SOpt';
UPDATE tabelaB SET nomes = 'SOpt BETA' WHERE nomes = 'SOpt';
    
15.06.2014 / 02:12
1

I do not like the idea more if it is something that already exists you can do the following. Assuming your bank is consistent.

You can add a single index to the nomes column in the a table

alter table a add unique(nomes);

and a foreign key in the b table by binding the tables to the nomes column. by adding the on update cascade clause you are saying that when the nomes column in the parent table is updated propagate the modifications.

alter table b add foreign key (nomes) references a(nomes) on update cascade;

So you'll be able to achieve your goal of updating the two tables with a single query.

Optionally you can also add the on delete cascade clause so that when the parent record is deleted automatically remove the child records, otherwise you will need to remove the child records explicitly before deleting the parent.

update a set nomes="zaz" where nomes="SOen"

See an example here in SQLFiddle

Alternative

I decided to search a little and do some tests and I got the expected result with the following query.

update a, b set a.nome="zaz", b.nome="zaz" where a.nome="SOen" and b.nome="SOen";

However, it was necessary to inform the new values and values of the where clause explicitly.

Example in SQLFiddle

    
15.06.2014 / 23:36
0

To update the data from Table to and b follow the example:

Connection: PHP Data Objects (PDO )

<?php
    $pdo = new PDO('mysql:dbname=generics;host=localhost', 'root', 'senha', 
                    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

Where:

  • PDO - Multiple database access interface with PHP
    In your constructor it has: string $dsn, string $username, string $password, array $driver_options . $dsn in the case was for Mysql : dbname is the name of the database and host can be the localhost or server ip
15.06.2014 / 02:15