How to achieve redundancy between 2 tables of different databases? - mysql

First of all I'd like to start saying that I've checked these two questions:
Sync 2 tables of different databases - MySQL
How to synchronize two tables of different databases on the same machine (MySql)
But while similar they are not what i need.
I have 2 databases in the same server.
Db1 and Db2
Both databases have the exact copy of a single table called "user":
userid
login
name
lastname
password
level
How can I achieve some sort of redundancy between these two tables in different databases?
If db1.user gets a new record then db2.user has to have that record, if a record is modified then the other one is modified and if deleted the then the other one gets deleted too.
To be more specific, db2.user needs to be a reflection of db1.user using triggers.
EDIT: there is this question: Mysql replication on single server and that is not even remotely close to what i want to do. I updated a little bit at the very end of what i previously posted with how I'd like to achieve this thanks to a suggestion.

As proposed you can use triggers as stated in this standard documentation.
You define AFTER INSERT,AFTER UPDATE and AFTER DELETE triggers on db1.user and within this trigger you have the NEW.object to pass the information into the db2.user table.

Related

Operations between linked tables and native tables

I have three identical tables, one on MySQL, one linked to this one on Access by ODBC, and a native in the same Access database.
When I update the table on MySQL, the linked table on Access updates, and vice versa. But I would like to know if it is possible that the linked table updates the native table (and vice versa)?
Access table
MySQL table
It really depends on how the local Access table is being updated. If it is ALWAYS updated say by a few forms, then you could add a after update even to those few forms, and put in code to update the MySQL table.
Another approch (again you only/always update the local tables) is to add a table trigger to the local table. In this table code event, you can actually have it call some VBA code, and that VBA code could then update/insert to the linked MySQL table. Once again, then the two tables will automatic remain in sync.
The other possible would be to add a time + date stamp column to the tables (both on MySQL side, and on the Access side). You could then write some VBA code to sync up the tables. Such code is not too hard, but in a multi-user setting, this can become quite a challenge, since while you are syncing the data, other users might also update the MySQL tables and thus your sync routines might well miss some tables. Database sync software and this subject can fill a few books the size of medical texts, and is a VERY complex subject.
However, why not just always use linked tables to MySQL, and be done with any requirements to sync data? Access makes a great client to SQL server or MySQL. If you eliminate the local tables, then you eliminate the need to sync your data.

SQL Server 2008 : Update table records from one database to another database present on two different servers

I have two databases with the same schema, name, stored procedures, same tables, same records on two different servers.
For example database test is present on following mentioned servers with everything same including data. server 1 = 123.155.12.1 and server 2 = 123.155.12.2
Now I need to update records in table on server 2 accordingly if there is any update done in same table on server 1. For this any query syntax exists..?
(excluding replication option) if yes please help me with example.
Thanks in advance!
Regards.
Aksh
Hi #Akki you could make use of the OPENQUERY option to achieve this, I am not sure if you want to perform this action for every update done on the Table in SERVER1 or if this is just a one time update.

How does mysql.proc table get entries?

I want to know that in mysql when I create any procedure or function an entry for the corresponding create gets an entry in the mysql.proc table. I want to know by which process or trigger in mysql , It gets updated.
Thanks,
Nitesh Kumar
The tables in the mysql database aren't tables in the sense that your application tables are. Instead, they're internal mysql software data structures that happen to be made visible to the outside world as if they were tables.
So, the processes by which rows are made visible in those tables are entirely internal to the logic of mysqld.

Using a table in another database

I've been asked to build a module for a web application, which will also be used as a stand alone website. Since this is the case, I wanted to use a separate database, and wondered if there was a way of having a table in one database, be a "pointer" in another database.
For example, I have databases db1 and db2
db1 has table users, so I want to have db2.users point to db1.users.
I know I could setup triggers and what not to sync two seperate tables but this sounds cooler :)
EDIT
So in my code I'm using sql such as
select * from users
Now, at the database level, I want "users" to actually be db1.users. Then, if I want to, I can remove the alias/pointer and "select * from users" will point to the users table in the current database. I guess what I'm looking for is a "global alias" type of thing.
Just use it directly from another database?
SELECT ... FROM `db1`.`users` LEFT JOIN `db2`.`something`
The federated storage engine offers something similar to the feature you asked for.
And if your databases are on the same database server, the federated storage enging sounds a bit like an overkill to me. You may want to create a view instead.
Both methods won't be useful if db1 is not available. As Emmerman already points out, you need to store the data in db2 if you want to prepare for the case of db1 being unavailable.

Setting up a master database to control the structure of other databases

I got a case where I have several databases running on the same server. There's one database for each client (company1, company2 etc). The structure of each of these databases should be identical with the same tables etc, but the data contained in each db will be different.
What I want to do is keep a master db that will contain no data, but manage the structure of all the other databases, meaning if I add, remove or alter any tables in the master db the changes will also be mirrored out to the other databases.
Example: If a table named Table1 is created in the master DB, the other databases (company1, company2 etc) will also get a table1.
Currently it is done by a script that monitors the database logs for changes made to the master database and running the same queries on each of the other databases. Looked into database replication, but from what I understand this will also bring along the data from the master database, which is not an option in this case.
Can I use some kind of logic against database schemas to do it?
So basicly what I'm asking here is:
How do I make this sync happen in the best possible way? Should I use a script monitoring the logs for changes or some other method?
How do I avoid existing data getting corrupted if a table is altered? (data getting removed if a table is dropped is okay)
Is syncing from a master database considered a good way to do what I wish (having an easy maintainable structure across several datbases)?
How will making updates like this affect the performance of the databases?
Hope my question was clear and that this is not a duplicate of some other thread. If more information and/or a better explantion of my problem is needed, let me know:)
You can get the list of tables for a given schema using:
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='<master table name>';
Use this list for a script or stored procedure ala:
create database if not exists <name>;
use <name>;
for each ( table_name in list )
create table if not exists <name>.table_name like <master_table>.table_name;
Now that Im thinking about it you might be able to put a trigger on the 'information_schema.tables' db that would call the 'create/maintain' script. Look for inserts and react accordingly.