php - mysql:存储任意数据

标签 php mysql database csv

背景:
我问的是 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/

相关文章:

php - Bootstrap 模式表单中的数据不会使用 php 传输到 MySQL 表

javascript - 在 PHP 中运行 html 代码 if

php - WordPress中文本小部件的内容存储在哪里?

android - Android监听mysql数据库变化的方法

c# - 使用sdf数据库(SQL Server Compact 3.5数据库文件)无需安装Microsoft sql server C#

php - 评论表单 'success' 消息转到页面顶部但被固定标题隐藏

php - 随机化输出

mysql - mysql在添加具有默认值的新列时是否锁定表以进行写入

mysql - 这个 SQL 查询/连接可能吗?

database - 从旧硬盘传输 postgres 数据库