简体   繁体   中英

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.

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.

To get back the original JSON array, just parse the stored string.

Normally, in DBs, you have 3 options:

  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)); 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;

  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[]); - use resultSet.getArray() to get this data, and there are plenty of SQL primitives to search through them, see the postgres array documentations.

  3. [generally, the worst, by far], handroll a mechanism to squish multiple strings into a single formatted string using escaping mechanisms. 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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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