How do I keep two article tables synced, but keep stock separate

只谈情不闲聊 提交于 2020-01-05 15:50:07

问题


This is probably a more conceptual problem, but I couldn't find an easy solution.

Scenario: Two shops (say 'M' and 'S']. M is the master and determines the articles in the databases. Each maintains an independent stock. I have M's article table replicating to S, and I separated stock into a separate table with a common reference.

Now when new articles are added in M, they arrive at S too, but they won't have an entry in S's stock table. Similar problem with delete articles. Possible solutions:

  • Do I create an entry in S's stock table each time a request is made for a new (not-test-existing) article?

  • Do I have to scan regularly to check for missing stock entries.

Isn't there a more elegant way to solve this?

NOTE: To clarify, let me explain another way:

M already replicates the 'articles' table to S (using MySQL's replication mechanism. This works fine.

The problem is that M and S have 'stock' tables which are local to each M and S. What is the normal procedure when, for example, a new product is added (in M) to the 'articles' table, and transferred to S. Now there is new entry which doesn't have a corresponding entry in S's stock table.

I'm guessing this is not an unusual situation - what would be the normal procedure to solve this?


回答1:


Unless of course if you have two databases located on two different DB servers, why don't you simply create a table articles and a table stock referencing it. You could add the shop (ideally the shop_id) as an extra column of that latter. Something like that:

CREATE TABLE articles(id int primary key not null,
                      name char(20) not null);

CREATE TABLE stock(article_id int not null,
                   shop ENUM('M', 'S') not null,
                   qty int not null,
                  FOREIGN KEY(article_id) REFERENCES articles(id),
                  UNIQUE(article_id, shop));

Please see http://sqlfiddle.com/#!2/e6eca/5 for a live example.

If you really need to restrict creation of items on table articles to shop M that could be achieved by creating different users for your DB (*user_m*, *user_s*) and using REVOKE and/or GRANT to setup the various access rights.


EDIT: If the two servers are on distant sites, you would probably be able to use MySQL replication capabilities to keep one or many tables in sync between the two sites. This will require a network access between the two sites. As of myself, for obvious reasons, I would consider using a secure tunnel between the two sites. Finally you still probably have to set-up permissions at DB-level to only allow insert from one site and not the other.

As a "poor's man" solution, you finally have the possibility to backup on regular basis the required tables from one server to update the tables on the second server. A combination of mysqldump + cronjob would be good starting point.

As of myself, I would though push to MySQL replication. The setup is probably more complex. But this will perform better, scale better and have lower latency.



来源:https://stackoverflow.com/questions/18036299/how-do-i-keep-two-article-tables-synced-but-keep-stock-separate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!