如何将N数据库表与一个主表绑定? [英] How to bond N database table with one master-table?

查看:125
本文介绍了如何将N数据库表与一个主表绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有N个表为N书店。由于每个表都有不同的方案(列的数量和类型不同),但是对于所有书店表都有相同的列集合,因此我必须在每个书店的单独表中保存有关图书的数据;



现在我想创建一个只有几个列的MasterTable。

  MasterTable | 
| id。 |标题| isbn |
| 1 | abc | 123 |


| MasterToBookstores |
| m_id | tb_id | p_id |
| 1 | 1 | 2 |
| 1 | 2 | 1 |


| BookStore_Foo |
| p_id |标题| isbn |日期|大小|
| 1 | xyz | 456 | 1998 | 3KB |
| 2 | abc | 123 | 2003 | 4 KB |

| BookStore_Bar |
| p_id |标题| isbn |发布商|格式|
| 1 | abc | 123 | H& K | PDF |
| 2 | mnh | 986 |亚马逊| MOBI |

我的问题是,以这种方式保存数据是否正确?关于这种情况和类似情况的最佳做法是什么?我可以给特定的书店表一个数字的别名,这将帮助我管理整套表格。



有更好的方式做这样的事情吗?

解决方案

如果你至少有两列,所有其他表使用它,那么你可以有所有图书的基表,并添加更多的表其余的数据使用id从基表。



更新:



如果使用实体框架连接到数据库,我建议您尝试: / p>

创建实体模型,如下所示:





然后让实体框架为你生成数据库(从模型更新数据库)。注意,这使用继承(不在数据库中)。



如果您有问题,请告知我们。


Lets assume that I have N tables for N Bookstores. I have to keep data about books in separate tables for each bookstore, because each table has different scheme (number and types of columns is different), however there are same set of columns which is common for all Bookstores table;

Now I want to create one "MasterTable" with only few columns.

|   MasterTable   |
|id. | title| isbn|     
| 1  | abc  | 123 |


| MasterToBookstores |
|m_id | tb_id | p_id |
| 1   |   1   |  2   |
| 1   |   2   |  1   |


|       BookStore_Foo          |
|p_id| title| isbn| date | size|     
| 1  | xyz  | 456 | 1998 | 3KB |
| 2  | abc  | 123 | 2003 | 4KB |

|       BookStore_Bar                  |
|p_id| title| isbn| publisher | Format |     
| 1  | abc  | 123 |   H&K     |   PDF  |
| 2  | mnh  | 986 |   Amazon  |   MOBI |

My question, is it right to keep data in such way? What are best-practise about this and similar cases? Can I give particular Bookstore table an aliase with number, which will help me manage whole set of tables?

Is there a better way of doing such thing?

解决方案

If you had at least two columns which all other tables use it then you could have base table for all books and add more tables for the rest of the data using the id from Base table.

UPDATE:

If you use entity framework to connect to your DB I suggest you to try this:

Create your entities model something like this:

then let entity framework generate the database(Update database from Model) for you. Note this uses inheritance(not in database).

Let me know if you have questions.

这篇关于如何将N数据库表与一个主表绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆