简体   繁体   English

在表中存储多种格式

[英]storing multiple formats in a table

So here's the basic problem: I'd like to be able to store various fields in a database. 因此,这是一个基本问题:我希望能够将各种字段存储在数据库中。 These can be short textfields (maybe 150 characters max, probably more like 50 in general) and long textfields (something that can store a whole page full of text). 这些可以是短文本字段(最多150个字符,通常更可能是50个字符)和长文本字段(可以存储整页文本的东西)。 Ideally more types can be added later on. 理想情况下,以后可以添加更多类型。

These fields are group by common field_group id s, and their type shouldn't really have anything to do with categorization. 这些字段按通用的field_group id进行分组,并且它们的类型实际上与分类没有任何关系。

So what's the best way to represent this in MySQL? 那么,在MySQL中代表这一点的最好方法是什么? One table with a short_text and long_text columns of differing types, one of which is to be NULL ? 一个表的short_textlong_text列的类型不同,其中之一为NULL Or is there a more elegant solution? 还是有一个更优雅的解决方案?

(I'd like this to be primarily driven by ease to select all fields with a given field_group_id.) (我希望这主要是通过轻松选择具有给定field_group_id的所有字段来驱动的。)

Clarification 澄清

I'm essentially attempting to allow users to create their own tables, but without actually creating tables. 我实质上是试图允许用户创建自己的表,但实际上没有创建表。

So you'd have a 'Book' field group, which would have the fields 'Name' (short text), 'Summary' (long text). 因此,您将拥有一个“ Book”字段组,其中将具有“ Name”(短文本),“ Summary”(长文本)字段。 Then you would be able to create entries into that book. 然后,您将可以在该书中创建条目。 I realize that this is essentially the whole point of MySQL, but I need to have a LOT of these and don't want users creating whole tables in my database. 我意识到这实质上是MySQL的全部内容,但是我需要拥有很多这样的内容,并且不希望用户在数据库中创建整个表。

What you are looking for is called an EAV. 您正在寻找的被称为EAV。 With an EAV model you can build any freaking database in the world with only inserts. 使用EAV模型,您仅需插入就可以构建世界上任何奇怪的数据库。 But it's really horrible for a lot of reasons but yours sounds so looney-tunes, it could work. 但这确实很可怕,原因有很多,但是您的声音听起来很松朗,可以用。

Build an Entity table 建立实体表

In here you'd list 在这里你会列出

  • Car 汽车
  • Person
  • Plant

Build an Attribute Table. 建立一个属性表。

Here you'd list the PK from Entity and the list of attributes. 在这里,您将列出Entity中的PK和属性列表。

I'll use the word instead of a number PK. 我将使用单词而不是数字PK。

  • Car | 汽车| Engine Cylinders 发动机油缸
  • Car | 汽车| Doors
  • Car | 汽车| Make 使
  • Person | First Name 名字
  • Person | Last Name

then in a third table you'd list the actual values for each one, again using the words but you'd have numbers. 然后在第三个表格中,您将列出每个表格的实际值,再次使用单词,但您会有数字。

  • Car | 汽车| Engine Cylinders | 发动机气缸| 4 4
  • Car | 汽车| Doors | 门| 4 4
  • Car | 汽车| Make | 制作| Honda 本田
  • Person | First Name | 名| Stephanie 斯蒂芬妮
  • Person | Last Name | 姓| Page

If you want to get tricky instead on one column for value you could have 4 columns 如果您想在某一列获取麻烦,则可以选择4列

  • a number 一个号码
  • a varchar varchar
  • a date 一个约会
  • a clob 血块

then in the Attribute table you could add a column that says which column to put the data. 然后可以在“属性”表中添加一列,说明要放置数据的列。

If you plan on this database being "Multitenent" you'll need to add an OWNER table as the parent of the entity table, so you and I could both have a Car entity. 如果您计划将此数据库设置为“多租户”,则需要添加一个OWNER表作为实体表的父级,因此您和我都可以拥有Car实体。

But this SUCKS to query, SUCKS to index, SUCKS to use for anything else but a toy app. 但是这个要查询的SUCKS,要索引的SUCKS,要用于玩具应用程序之外的其他任何东西的SUCKS。

I don't know exactly what you mean by "field group", but if the information (short text, long text) all belongs to a certain entry, you can create a single table and include all those columns. 我不完全了解“字段组”的含义,但是如果信息(短文本,长文本)全部属于某个条目,则可以创建一个表并包括所有这些列。

Say you have a bunch of books with a title and a summary: 假设您有一堆带有标题和摘要的书:

table: `books`
- id, int(11) // unique for each book
- title, varchar(255)
- writer, varchar(50)
- summary, text
- etc

Fields that don't necessarily need to be set can be set to NULL by default. 默认情况下,可以将不需要设置的字段设置为NULL。

To retrieve the information, simply select all the fields: 要检索信息,只需选择所有字段:

SELECT * FROM books WHERE id = 1

Or some of the fields: 或某些字段:

SELECT title, writer FROM books ORDER BY title ASC

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

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