背景:
我问的是 question on stack overflow关于在发生此对话的地方动态创建表格:
This smells like a terrible idea! In fact, it smells just like this one.
What in the world do you want to use this for? – deceze
@deceze: very true, However, How else would you store the contents of these CSV files.
They must be stored in mysql for indexing. The only solid fact about them is that they all have a mobile column with a standard format.
The CSV can have an arbitrary amount of columns with an arbitrary amount of rows.
They can (with no exaggeration) range from a single row, 35 column csv to an 80k row single column CSV. I am open to other ideas. – Hailwood
There are many solutions for this, from attribute-value schemas to JSON storage and NoSQL storage. Open a new question about it. Whatever you do though, don't dynamically create tables! – deceze
问题:
所以我的问题是,
您认为存储这些数据的最佳方式是什么?
您是否同意 deceze 关于不创建动态表的观点?
最佳答案
存储任意长记录的一个非常简单的模式是这样的:
Table `records`
===============
id
created
... other meta data ...
Table `record_attributes`
=====================
id
record_id
attribute
value
因此 CSV 记录可以这样存储:
"Foo","Bar","Baz" // column names
42,"Lorem","Jerry" // first record
7,"Ipsum","Tom" // second record
...
records(1, '2010-1-17', ...)
record_attributes(1, 1, 'Foo', 42)
record_attributes(2, 1, 'Bar', 'Lorem')
record_attributes(3, 1, 'Baz', 'Jerry')
records(2, '2010-1-17', ...)
record_attributes(4, 2, 'Foo', 7)
record_attributes(5, 2, 'Bar', 'Ipsum')
record_attributes(6, 2, 'Baz', 'Tom')
另一种方法是将记录数据作为 JSON 打包的 blob 存储在单个列中。如果您不需要搜索数据,这是最紧凑的方式,尽管不是非常 RDBMS。
最合适的可能是 NoSQL database ,如果您有该选项。
关于php - mysql:存储任意数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4709878/