此场景的数据库架构
嗯,这个架构问题已经在我脑海中徘徊了一段时间。假设以下场景:
我有一个 Licenses
表。从概念上讲,每个许可证可以限制为(许可证类型):
- 类型 1:尝试次数。 (例如允许运行7次)
- 类型2:试用(限时)。
- 类型 3:完整
- ...
因此,每个许可证都应该存储一些自定义值。 (类型 1:整数,类型 2:日期时间,类型 3:空)
此场景的最佳架构是什么?
- 如果我决定将所有许可证放入一个表中,则每一行至少有 1 个未使用的列(对于类型 1,EndDate 将为空,对于 EndDate,TryTimes 将为空,对于类型 3,两者都将为空):
LicenseID---LicenseType---CustomerID---EndDate---TryTimes
另一方面,我希望我的设计尽可能灵活(将来可能会有更多许可证类型?) - 另一种可能的解决方案是使用一些类似元数据的方法:
许可证ID---许可证定义---客户ID---
其中 LicenseDefinition 包含有关许可证类型和限制的信息,并在代码端进行解析。
您建议哪一种更传统?您建议任何其他方式来实现它吗?
更新: 刚刚发现稀疏
列是SQL Server。听起来很有希望...
Well, this architectural issue has been wandering in my mind for a while. Suppose the following scenario:
I have a Licenses
table. Conceptually, each license can be limited to (License Types):
- Type 1: Number of tries. (e.g. allowing to run 7 times)
- Type 2: Trial (Time-limited).
- Type 3: Full
- ...
So, each license should store some custom value. (Type 1: Integer, Type 2: DateTime, Type 3: null)
What's the best architecture for this scenario?
- If I decide to put all the licenses in one table, I have at least 1 unused column for every row (EndDate would be null for Type 1, TryTimes would be null for EndDate and both would be null for Type 3):
LicenseID---LicenseType---CustomerID---EndDate---TryTimes
On the other hand, I'd like my design to be as flexible as possible (Maybe more license types in the future?) - Another possible solution would be using some metadata-like approach:
LicenseID---LicenseDefinition---CustomerID---
Where LicenseDefinition contains information about the type and limit of the license and is parsed on the code side.
Which one do you suggest to be more conventional? Do you suggest any other way to implement it?
UPDATE:
Just found out about Sparse
columns is SQL Server. Sounds very promising...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我突然想到(我还没有实现类似的东西),我可能会做这样的事情:
为
Customer
和License
创建数据库表。在Customer
中,连同所有其他通用客户信息,我将添加一个licenseType
列,它将引用
License< /代码>表。
License
将存储licenseId
以及任何相关元数据。 这不会包括特定于许可证的规则。在代码方面,我将创建一个
LicenseFactory
类,它将创建许可证接口的实例(ILicense)。
ILicense
可能看起来像这样(在 PHP 中):然后,我会有特定于许可证的实现:
工厂类看起来像这样:
所以现在,我的应用程序代码可能看起来像这样:
没有这已经过测试,只是我(相当冗长)的初步想法。希望它能有所帮助(即使您的应用程序可能不是 php 驱动的:))
编辑:
忘记提及 - 这种方法的强大之处在于可扩展性。每当您想要添加新的许可证类型时,只需向
License
表中添加一个新行,并添加一个新的ILicense
实现(其中包含所需的任何业务规则)。Off the top of my head (I haven't implemented anything like this yet), I might do something like this:
Create database tables for
Customer
andLicense
. WithinCustomer
, along with all of the other generic customer information I would add a column forlicenseType
, which wouldreference
theLicense
table.License
would store thelicenseId
as well as any relevant meta data. This would not include license-specific rules.On the code side, I would create a
LicenseFactory
class which would create an instance of a license interface (ILicense
).ILicense
might look something like this (in PHP):Then, I would have license-specific implementations:
The factory class would look something like:
So now, my application code might look something like this:
None of this has been tested and are just my (rather long winded) initial thoughts. Hope it helps a little though (even though your app may not be php driven :))
Edit:
Forgot to mention - the power in this approach is the extensibility. Any time you want to add a new license type, you'd simply add a new row to the
License
table and a newILicense
implementation with whatever business rules are required.您应该为每种许可证类型保留一个单独的表:
并保持数据完整性,例如只有计时许可证才会进入第二个表。
这也将是一个相当可扩展的模型,您将需要添加一个表、一个完整性规则和一个接口,以便添加另一种许可证类型。
You should keep a separate table for each license type:
and also maintain data integrity such as only time-trial licenses would come into the second table.
This will be also quite a scalable model, you will need to add one more table, one more integrity rule and one more interface in order to add another license type.
您可以将单个表作为父表,并使用其他三个表来引用扩展架构的基表。
这是第一个选择。
另一种选择是使用占用空间小的简单NoSql数据库或OO数据库(如db4o),甚至简单的数据存储(如加密
XML
)文件。You can have a single table as a parent and three other tables referencing the base table extending the schema.
That's the first option.
Another option is using a simple NoSql Database with low footprint or an OO database like db4o or even simple data stores like an encrypted
XML
file.