My question :
How can i sync 2 mysql databases (offline local database with master online database) ?
problem is database is relational,and id as always is auto incremenate, so if i just sync using insert it will mess with my referals.
this for a clinic management app i made, problem is currently its on
server but sometimes internet connection goes down/slow on my users
clinic, so i need to let him work on offline mode (store every thing
to local db) and manually sync (bi-directional) with remote database
at end of day.
so basically each clinic should have its own local db and let them all sync to central db
example of tables.
db.Cental.users
|id|user|clinic |
|01|demo|day care|
|02|nurs|er |
|03|demX|day care|
db.day care.users
|id|user|clinic |
|01|demo|day care|
|02|demX|day care|
(note id doesnt necessarily match between central and local db, yet structure of db on both is identical)
example:

database info:
-
each user have many visits,plugins.
-
each visit contain 1user as patient_id, and 1user as doctor_id
-
each plugin have one user, many inputs
-
plugin_inputs have one plugin
i have 2 databases 1 on the server and other hosted locally -for offline mode-
what i want is to be able to sync locally db with online one, but since i have more than 1 user, more than one local db so each one will have nearly same id’s while online db should contain all of them combined.
so how can i sync them together ?
i user php/mysql(online)/sqlite(local)
There are a couple of things you can do.
Firstly, you could consider a composite primary key for the user table – for instance, you could include “Clinic” in the primary key. That way, you know that even if the auto increment values overlap, you can always uniquely identify the user.
The “visit” table then needs to include patient_clinic and doctor_clinic in the foreign keys.
Alternatively – and far dirtier – you can simply set the auto increment fields for each clinic to start at different numbers:
ALTER TABLE user AUTO_INCREMENT = 10000;will set the first auto_increment for the user table to 10000; if the next clinic sets it at 20000 (or whatever), you can avoid overlaps (until you exceed that magic number, which is why it’s dirty).