Question: I have two tables in mysql. When I insert/delete values in the first table I want that the values get duplicated in table 2 to keep them "aligned".

table1:  

id - username  

1 - test\_user  

table2:  

Same id as table1 and username as table1 (on insert/delete)  

I want to keep the data between the tables aligned without doing multiple queries. I've read about triggers not sure if it's the correct road, i am a beninner.  

I said two tables but i will need to do this in multiple tables.

Answer: 
You can use Mysql triggers. This way you can auto insert/update/delete datas from second table.

[MySql Using Triggers](http://dev.mysql.com/doc/refman/5.0/en/triggers.html)
You can certainly put triggers on your table1 to make parallel changes to your other tables as your application changes table1.

See here for the documentation: <http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html>

But, you should think over your design. It will take multiple queries to do your inserts and updates; they'll just be done "behind your back" on the server. They'll still take time. Triggers can really slow things down.

Also, triggers are a little bit fragile. If you add a column to a table, you'll have to rework your triggers. Triggers are generally a pain in the neck to keep in a source-control system and a huge pain in the neck to test, so using them will make your application more troublesome to maintain.

Could you think of another approach to handling this need for duplication? Could you, for example, use a view or a join to present the data you need to your application program without actually duplicating tables and the rows in them? If you figure out how to do that you'll be much happier in the long run.

```
 CREATE VIEW table2 AS 
      SELECT * 
       FROM table1;

```

will produce a "fake" table2 with the contents of table1.

Or if you're hoping to view only the test users in a second table, a view can do that for you too, for example:

```
CREATE VIEW table3 AS
     SELECT * 
       FROM table1
      WHERE usertype = 'test_user' ;

```

If you're using duplicate tables for "backup," that's a bad way to make sure your information is safe. Instead, you need to back up your MySQL server instance.

Formal relational database design principles teach us to duplicating data, but instead use view and joins to structure the data the way applications need to see it.