简体   繁体   English

用户可以在其中创建数据类型的数据库

[英]A database in which the users can create data types

I have a really old application using an SQL database that I need to update. 我有一个使用SQL数据库的旧应用程序,需要更新。 I would like to take also the opportunity to improve the database structure and I would appreciate some advice. 我也想借此机会改善数据库结构,并希望获得一些建议。

The basic problem is that an important part of the database must be user configurable without touching the code. 基本的问题是,数据库的重要部分必须是用户可配置的,而无需接触代码。 To be more concrete, the DB stores products and these products have different specs (ie columns) depending on the type. 更具体地说,数据库存储产品,并且这些产品根据类型具有不同的规格(即列)。 The app must be able to search for any of the columns. 该应用程序必须能够搜索任何列。 There are only a few types (~20) but the administrator must be able to create a new one without touching the code. 仅有少数几种类型(〜20),但是管理员必须能够创建新的类型而无需触及代码。 The data that needs to be stored for each product are either strings or floats, and never more than 7 of each type. 每个产品需要存储的数据是字符串或浮点数,每种类型的数据都不得超过7。

Instead of creating an interface to create and delete tables, the following "solution" was implemented. 代替创建用于创建和删除表的接口,实现了以下“解决方案”。 - In the Products Table, there is one column for the id; -在“产品表”中,一栏代表ID; one column for the ProducTypeID; ProducTypeID的一栏; 7 string columns and 7 float columns - In a ProducType column, there is one column for the ProducTypeID, and 14 string columns indicating the names of the 7 string columns and 7 float columns for each product type. 7个字符串列和7个浮点列-在ProducType列中,有1个ProducTypeID列,以及14个字符串列,分别指示每种产品类型的7个字符串列和7个浮点列的名称。 If a product does not need so many columns, the column name is NULL 如果产品不需要太多列,则列名称为NULL

This works but due to the extra indirection is extremely annoying to maintain the client code. 这是可行的,但是由于额外的间接性非常讨厌维护客户端代码。

The question is: Should I stay with an SQL DB and add a way to create/delete tables or should I use a noSQL DB? 问题是:我应该呆在SQL DB上并添加一种创建/删除表的方法,还是应该使用noSQL DB? Which are the pros and cons in each case? 每种情况下都有哪些优缺点?

Keep in mind that in SQL databases, adding and removing columns on a large table can be a very expensive operation which can take minutes or even hours. 请记住,在SQL数据库中,在大表上添加和删除列可能是非常昂贵的操作,可能需要几分钟甚至几小时。 Doing it on-the-fly is a really bad idea. 即时进行操作是一个非常糟糕的主意。 Adding a bunch of "multi-purpose" columns to a table is not much better. 向表中添加一堆“多用途”列并不是更好。 It's hard to query and you have a limit on how many properties a product can have. 很难查询,并且您对产品可以拥有的属性数量有限制。

The usual by-the-book solution when each product has 0-n dynamic properties is to create a second table ProductID(primary key) | PropertyName(primary key) | PropertyValue 每个产品具有0-n动态属性时,通常的按书解决方案是创建第二个表ProductID(primary key) | PropertyName(primary key) | PropertyValue ProductID(primary key) | PropertyName(primary key) | PropertyValue ProductID(primary key) | PropertyName(primary key) | PropertyValue . ProductID(primary key) | PropertyName(primary key) | PropertyValue This allows each product to have any number of properties. 这允许每个产品具有任意数量的属性。 You can easily JOIN it with the main products table to get all products with their properties. 您可以轻松地将其与main products表JOIN起来,以获取具有其属性的所有产品。

When you are open to switching database technologies, you could also use a document-oriented NoSQL database which doesn't use a fixed schema like MongoDB or CouchDB. 当您愿意使用交换数据库技术时,您也可以使用面向文档的NoSQL数据库,该数据库不使用MongoDB或CouchDB之类的固定模式。 In such databases, each document in a collection can have a different set of fields. 在这样的数据库中,集合中的每个文档可以具有不同的字段集。 But before you decide to make this step, evaluate how such a database would affect other parts of your application. 但是在决定执行此步骤之前,请评估这样的数据库如何影响应用程序的其他部分。 Listing everything that could be positively or negatively affected without knowing your whole application in and out would be too broad of a question. 列出所有 可能会受到正面或负面影响的事物 ,而又不知道整个应用程序的进出范围,这将成为一个广泛的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM