SQL DB schema best practice for List item table

断了今生、忘了曾经 提交于 2019-12-24 10:57:05

问题


I have a table say Table1 which has following columns

1. Id
2. Name
3. TransportModeId
4. ParkingId
5. ActivityId

Column 3,4,5 are the foreign keys and all three are simple list tables which has following columns

1. Id
2. Item

For simplicity I have shown 3 tables otherwise my actual schema contains almost 25 List table.

What should be the best Practice

Option 1. Keep all list table separate which will create 25 tables but on the other hand i will have a clean modular schema

Option 2. Make a table with self join and add all the items in that table in which ParentId null will represent the name of the table and it can have more than one references in other tables as described above and it has to be kept in some kind of common module

thanks


回答1:


Option 1 is the way how it is normally done when designing a system that is not supposed to be very configurable by end user/implementator. It has several important advantages, two of them:

  1. when you need to add an extra attribute to any of the enumerations (e.g. parking location to the Parking enumeration), it is quite simple and does not produce extra problems.

  2. It is optimized for speed using relation database engine's native algorithms for linking records.

As for Option 2: It is something called Generalization. You take more types with similar attributes (methods) and create a class/table with a structure that fits different purposes.

The self reference, as you speak about it, is not a good idea for Option 2, rather make a reference to another EnumerationType table containing type names like Parking, Activity etc. with id.

Using this approach could make sense in case you need to enable end user to configure the attributes himself within your app. But otherwise it could cause you problems when you find out, that different enumeration tables need to have different structures.



来源:https://stackoverflow.com/questions/43117810/sql-db-schema-best-practice-for-list-item-table

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