简体   繁体   English

用户创建的数据库结构:非关系数据库还是关系数据库?

[英]User-created database structure: non-relational or relational databases?

I want to have dynamic fields in my database records. 我想在数据库记录中包含动态字段。

For example: I want to build an application for users to create their own forms. 例如:我想构建一个应用程序,供用户创建自己的表单。

A user could create the following forms: 用户可以创建以下表单:

Personal profile: 个人资料:

  • Full Name 全名
  • Street
  • Job 工作
  • Phone 电话
    • Home
    • Work 工作
    • Mobile 移动
  • Interests 兴趣爱好
    • Interest 1 利息1
    • Interest 2 兴趣2
    • Interest 3 兴趣3

Work: 工作:

  • First name 名字
  • Last name
  • Work 工作
    • Department
      • Specialty 1 专业1
      • Specialty 2 专业2
    • Department
      • Specialty 1 专业1
      • Specialty 2 专业2

Countries: 国家:

  • United states 美国
    • States 状态
      • New York 纽约
        • Cities 城市
          • New York 纽约
          • Foo oo
      • Alabama 阿拉巴马州
        • Cities 城市
          • Bar 酒吧
          • Baz 巴兹

As you can see this is a very dynamical structure: 如您所见,这是一个非常动态的结构:

  • No predefined number of fields 没有预定义的字段数
  • No predefined field names 没有预定义的字段名称
  • User creates the structure of the database 用户创建数据库的结构

So I wonder, what is the best database for this: relational (mysql/postgresql) or non-relational like mongodb/couchdb/cassandra or even xml databases like xindice? 所以我想知道,最好的数据库是什么:关系型(mysql / postgresql)或非关系型(如mongodb / couchdb / cassandra)甚至是xml数据库(如xindice)?

And even if I choose non-relational databases for this, would it be smart to store security-critical information on it like customer and billing information? 即使我为此选择了非关系型数据库,将诸如客户和账单信息之类的安全关键信息存储在其上是否明智?

I have heard people say that if your information require uniqueness then use relational database. 我听说有人说,如果您的信息需要唯一性,请使用关系数据库。 "We don't want to risk to bill our customers twice". “我们不想冒两次向客户收费的风险”。 What problems on non-relational databases do they actually mean? 非关系数据库实际上意味着什么问题? Can't you store unique data in non-relational databases? 您不能在非关系数据库中存储唯一数据吗?

Another thing I was thinking about: Won't saving data in non-relational databases mean that I will have duplicated entries? 我正在考虑的另一件事:是否将数据保存在非关系数据库中意味着我将有重复的条目?

Consider this example: 考虑以下示例:

Categories: 分类:

  • Office 办公室

    • Applications 应用领域
      • Textmate 文字伴侣
        • Author : Foobar 作者:Foobar
        • Price : 120 价钱:120
      • Foo oo
        • Author : Foobar 作者:Foobar
        • Price : 120 价钱:120
  • Office 办公室

    • Applications 应用领域
      • Textmate 文字伴侣
        • Author : Foobar 作者:Foobar
        • Price : 120 价钱:120
      • Bar 酒吧
        • Author : Foobar 作者:Foobar
        • Price : 120 价钱:120

As you see there are situations for identical entries. 如您所见,存在相同条目的情况。 How do non-relational databases handle these? 非关系数据库如何处理这些? Im so used to relational databases. 我是如此习惯于关系数据库。

I sum up my questions: 我总结一下我的问题:

  • What type of database for user-created database structure? 用户创建的数据库结构是哪种类型的数据库?
  • Are non-realtional databases for storing security critical information? 用于存储安全性关键信息的非传统数据库吗?
  • How do non-realtional databases handle duplications? 非真实数据库如何处理重复项?

I highly recommend you check out CouchDB for this. 我强烈建议您为此查看CouchDB

  1. You communicate with CouchDB using a straightforward REST API. 您使用简单的REST API与CouchDB通信。 In other words, it is " Made of the Web" rather than simply being a backend db like MongoDB and others. 换句话说,它是“ Web构成”,而不是像MongoDB等其他后端数据库。 CouchDB can actually serve the forms and receive submissions since has a built-in web server. 由于具有内置的Web服务器,因此CouchDB实际上可以提供表单并接收提交。
  2. Being a JSON document store it is well-suited for storing structured-yet-schemaless data. 作为JSON文档存储,它非常适合存储尚未结构化的数据。 (Forms and their submissions are really documents and it makes more sense to model them this way, IMO.) (表格及其提交的内容实际上是文档,以这种方式对它们进行建模更有意义,IMO。)
  3. You could easily store a JSON document that describes each web form in the same "bucket" as the form submissions. 您可以轻松地将描述每个Web表单的JSON文档存储在与表单提交相同的“存储桶”中。 (CouchDB can even parse form POSTs and turn them into JSON docs however you see fit. Having it automatically timestamp form submissions, for example, is simple.) (CouchDB甚至可以解析表单POST,然后将其转换为JSON文档,但您认为合适。例如,让它自动为表单提交添加时间戳很简单。)
  4. You could write what is known as a "_show" function to actually generate each form's html code within CouchDB. 您可以编写所谓的“ _show”函数在CouchDB中实际生成每个表单的html代码。 Also check out "_update" and validation functions. 还要检查“ _update”和验证功能。
  5. It has the security features you would need. 它具有您需要的安全功能。
  6. Document conflicts can be identified easily. 可以轻松识别文档冲突。 Even better, CouchDB automatically determines a "winning" version of the document but you will continue to have access to the "losing" document versions (until you tell CouchDB to compact the database, which removes old revisions.) 更好的是,CouchDB会自动确定文档的“胜出”版本,但您仍可以访问“丢失”的文档版本(直到您告诉CouchDB压缩数据库,从而删除了旧修订版。)
    • Regarding uniqueness: instead of having CouchDB generate unique doc _id's you'll want to assign an _id that truly represents a unique form submission. 关于唯一性:您不希望CouchDB生成唯一的doc _id,而是要分配一个_id来真正代表唯一的表单提交。 If each user is only allowed one submission per form then use something along these lines for each JSON document created from a form submission: submission:user:5:form:a3df2a712 如果每个用户只允许每个表单提交一次,则对从表单提交创建的每个JSON文档使用以下内容submission:user:5:form:a3df2a712

Using CouchDB you can avoid the pain of dynamically creating unique tables for every form a user might create. 使用CouchDB,您可以避免为用户可能创建的每种表单动态创建唯一表的麻烦。

If your data fits the relational model pretty well, but you need to store some dynamically formatted data that isn't enormous, then you will probably be better off storing JSON, XML, or similar into a column. 如果您的数据非常适合关系模型,但是您需要存储一些动态格式化的数据,这些数据不是很大,那么最好将JSON,XML或类似的数据存储到列中。 Although you lose some advantages of first-class SQL typing by doing this (indexing, foreign key constraint checking, type checking, etc.), it's good for storing dynamically-structured documents when your queries don't care much about their internals. 尽管这样做会使您失去一流SQL输入的某些优势(索引编制,外键约束检查,类型检查等),但是当您的查询不太在意它们的内部时,它对于存储动态结构化文档非常有用。

If you're interested in storing mostly relational data with a touch of JSON/XML/etc., I recommend looking to PostgreSQL. 如果您有兴趣通过JSON / XML / etc等来存储大部分关系数据,建议您使用PostgreSQL。 PostgreSQL has an XML data type, but I don't recommend using it since I hate XML :P . PostgreSQL有XML数据类型,但是我不推荐使用它,因为我讨厌XML:P。 Nobody's stopping you from storing JSON in a TEXT field, but PostgreSQL will soon have a JSON data type with supporting functions. 没有人阻止您将JSON存储在TEXT字段中,但是PostgreSQL很快将具有支持功能的JSON数据类型。 The hstore contrib module provides an efficient way to store key/value pairs, and also provides full-text index support. hstore contrib模块提供了一种存储键/值对的有效方法,并且还提供了全文本索引支持。

Although shoving JSON or similar into a SQL database column flies in the face of the relational model, you're usually better off doing it anyway (when it makes sense!). 尽管将JSON或类似的东西推到SQL数据库列中虽然是关系模型的事,但是您通常还是最好这么做(在有意义的时候!)。 Otherwise, you have to explain the entire schema of your application to the database, resulting in a lot of SQL and database mapping code that really doesn't do anything. 否则,您必须向数据库解释应用程序的整个架构,从而导致很多SQL和数据库映射代码实际上什么也没做。

The database to choose depends more on what and how you want to query something moreso than what you want to store. 选择的数据库更多地取决于您要查询的内容和方式,而不是要存储的内容。 All the DBs will let you store pretty much whatever you want. 所有的数据库将使您几乎存储任何您想要的东西。

RDBMS are particularly good at querying based on the relational model, and doing so reasonably arbitrarily. RDBMS特别擅长基于关系模型的查询,并且可以合理地任意进行。 Through ad hoc filters and joins, you can do all sorts of magic. 通过临时过滤器和联接,您可以进行各种魔术。

The NOSQL DBs tend to be less flexible on their queries, but do well at other tasks (such as working better on "unstructured" data for example). NOSQL DB的查询灵活性较差,但在其他任务上却做得很好(例如,对“非结构化”数据的处理更好)。

Given what you've posted here, I'd just use a SQL database and define the tables as the user wants them defined. 鉴于您在此处发布的内容,我将只使用一个SQL数据库并按照用户想要的定义表。 Set up the indexes, set up the queries. 设置索引,设置查询。 Sounds like a real no brainer to me. 听起来对我来说真是不费吹灰之力。 SQL DBs handle all of that "defining fields on the fly" stuff handily, because...that's what they do. SQL DB可以轻松处理所有这些“动态定义字段”的内容,因为...这就是它们的工作。 So use that. 所以用那个。

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

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