简体   繁体   English

如何在 SQL 列中存储用户生成的不同大小的字符串数组?

[英]How to store a user-generated String array of varying size in SQL column?

We are attempting to save a multiple amount of Strings in a database column.我们试图在数据库列中保存多个字符串。 We are using one single column since the amount is varying, it could be anywhere from 15-50 so obviously multiple columns may not be viable.我们使用单列,因为数量在变化,它可能在 15-50 之间的任何地方,所以显然多列可能不可行。

Another issue is that the data is user-generated.另一个问题是数据是用户生成的。 If this wasn't the case, we'd be using splitting characters and there would be no issue, however we can't guarantee that the data won't contain those characters.如果不是这种情况,我们将使用拆分字符并且不会有问题,但是我们不能保证数据不会包含这些字符。

What would you guys recommend to achieve this?你们会推荐什么来实现这一目标?

You can put the strings in a JSON array, stringify that, and store the result.您可以将字符串放入 JSON 数组中,对其进行字符串化,然后存储结果。

To get back the original JSON array, just parse the stored string.要取回原始的 JSON 数组,只需解析存储的字符串即可。

Normally, in DBs, you have 3 options:通常,在数据库中,您有 3 个选项:

  1. [Best, generally] - hey, it's called 'relational database' for a reason, make a relation, Store your strings in a separate table. [最好,通常] - 嘿,它被称为“关系数据库”是有原因的,建立关系,将字符串存储在单独的表中。 eg, you want to store the name(s) of the pet(s) that someone has, which can be any number: from 0 to lots: CREATE TABLE person_pets (id int NOT NULL, person_id int FOREIGN KEY REFERENCES persons(id), pet_name varchar NOT NULL, PRIMARY KEY(id));例如,您想存储某人拥有的宠物的名称,可以是任何数字:从 0 到很多: CREATE TABLE person_pets (id int NOT NULL, person_id int FOREIGN KEY REFERENCES persons(id), pet_name varchar NOT NULL, PRIMARY KEY(id)); Note: Assuming a non-silly DB engine (generally mysql counts as silly, so try not to use that disaster of a db if you can), you can return one row and still have all the pet names, for example: SELECT person.name, ARRAY_AGG(pet.name) AS petNames FROM person LEFT JOIN pets ON pets.person = person.id ORDER BY person.name;注意:假设一个非愚蠢的数据库引擎(通常 mysql 算作愚蠢,所以如果可以的话尽量不要使用数据库的灾难),你可以返回一行并且仍然有所有的宠物名字,例如: SELECT person.name, ARRAY_AGG(pet.name) AS petNames FROM person LEFT JOIN pets ON pets.person = person.id ORDER BY person.name;

  2. Some database engines, such as for example postgres, allow arrays. JDBC lets you read these: CREATE TABLE person (id int NOT NULL, .... other fields...., pet_names text[]);一些数据库引擎,例如 postgres,允许 arrays。JDBC 让你阅读这些: CREATE TABLE person (id int NOT NULL, .... other fields...., pet_names text[]); - use resultSet.getArray() to get this data, and there are plenty of SQL primitives to search through them, see the postgres array documentations. - 使用resultSet.getArray()获取此数据,并且有大量 SQL 原语可以搜索它们,请参阅 postgres 数组文档。

  3. [generally, the worst, by far], handroll a mechanism to squish multiple strings into a single formatted string using escaping mechanisms. [通常,最糟糕的,到目前为止],使用 escaping 机制将多个字符串压缩成单个格式化字符串的机制。 You could toss the strings through a JSON-izer and store the resulting data, ie store ["Joe said. \"Hello,\" to me", "Bye!"] as a string in the DB.您可以通过 JSON-izer 处理字符串并存储结果数据, ["Joe said. \"Hello,\" to me", "Bye!"]作为字符串存储在数据库中。 This is entirely crappy because it makes it next to impossible to use DB queries to search any of this stuff and there is just no good reason to do this.这完全是糟糕的,因为它几乎不可能使用数据库查询来搜索任何这些东西,而且没有充分的理由这样做。 So, don't.所以,不要。 Note that psql actually supports 'json' as a column type.请注意,psql 实际上支持“json”作为列类型。 I don't really see much of a reason to do that vs. using arrays (at the very least, interacting with those from JDBC is a lot more complicated vs. arrays), but at least then you can write a query to, say, select all persons that owns a pet named 'Rover', which is not feasible if you jsonize and then dump the result in a varchar/text column.与使用 arrays 相比,我真的没有看到太多这样做的理由(至少,与 JDBC 中的交互相比数组要复杂得多),但至少你可以编写一个查询,比如, select 所有拥有名为“Rover”的宠物的人,如果您进行 jsonize 然后将结果转储到 varchar/text 列中,这是不可行的。

You're asking how to do #3, but perhaps you haven't really thought this through or you aren't aware of options #1 and #2.您在问如何做 #3,但也许您还没有真正考虑清楚,或者您不知道选项 #1 和 #2。

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

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