English 中文(简体)
使用ALTER TABLE实现自定义字段
原标题:Implementing custom fields with ALTER TABLE

我们目前在考虑不同的方式来为我们的Web应用程序实施自定义字段。用户应该能够为特定实体定义自定义字段,并填写/查看此数据(可能后续查询数据)。

我理解实现自定义字段有不同的方式(例如使用名称/值表或使用alter table等),我们目前更倾向于使用ALTER TABLE动态地向数据库添加新用户字段。

在浏览了其他相关的SO话题后,我没有找到这种解决方案的任何重大缺点。相反,能够以快速的方式查询数据(例如,直接使用SQL的where语句)对我们来说是一个很大的优势。

你认为采用这种方式实现自定义字段有什么缺点吗?我们正在谈论一个同时可被100个用户使用的网络应用程序(不是并发请求..),它可以使用MySQL和MS SQL Server数据库。

最佳回答

就最新情况而言,我们决定通过ALTER TABLE在现有数据库表格中增加新的栏目,以实施习俗领域。 在进行了一些研究和测试之后,这看起来像大多数数据库发动机的最佳解决办法。 关于习俗领域的单列表格提供了管理、查询和与习俗领域合作所需的信息。

问题回答

The first drawback I see is that you need to grant your application service with ALTER rights.
This implies that your security model needs careful attention as the application will be able to not only add fields but to drop and rename them as well and create some tables (at least for MySQL).

其次,您如何区分每个用户所需的字段?或者用户A创建的字段是否可以被用户B访问?

请注意,列的基数也可能显著增长。如果每个用户添加了2个字段,我们已经谈论了200个字段。

我个人将采用两种办法之一或组合:

  1. Using a serialized field

我会在表格中添加一个文本字段,在其中存储序列化的字典或多个字典:

{ 
  user_1: {key1: val1, key2, val2,...}, 
  user_2: {key1: val1, key2, val2,...}, 
  ...
}

缺点是这些价值不容易搜索到。

  1. Using a multi-type name/value table

领域表:

user_id: int
field_name: varchar(100)
type: enum( INT , REAL , STRING )

价值观表:

field_id: int
row_id: int # the main table row id
int_value: int
float_value: float
text_value: text

当然,它需要一个联接并且实现起来有点复杂,但是更通用,如果正确索引,效率相当高。

我认为在数据库表中添加新的自定义字段没有问题。

采用这种方法,可以使用特定/最适合的类型,例如需要一个int字段?将其定义为int。而对于一个名字/值类型的表来说,你会将多种数据类型存储为一个类型(可能是nvarchar)——除非你使用多列不同类型的名称/值表并填充适当的列,但那有点可怕。

此外,添加新列使查询更容易/无需涉及连接到新的名称/值表。

可能没有那么普遍,但我认为这比拥有一个“一刀切”的名称/值表要好。

From an SQL Server point of view (2005 onwards)....
An alternative, would be to store create 1 "custom data" field of type XML - this would be truly generic and require no field creation or the need for a separate name/value table. Also has the benefit that not all records have to have the same custom data (i.e. the one field is common, but what it contains doesn t have to be). Not 100% on the performance impact but XML data can be indexed.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签