-3

The table of Content has a unknown length, and I have a lot of articles. I want to store the table of content in my MySQL. I don't know if I should use BLOB or not, because then I won't be able to alter them through SQL queries.

I will need to be able to get the table of contents into an array object back into Java.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Louis Hong
  • 1,051
  • 2
  • 12
  • 27
  • 2
    Are you looking for a [One to Many](http://stackoverflow.com/a/12402535/823393) relationship between article and TOC? – OldCurmudgeon Aug 15 '13 at 08:09

3 Answers3

2

I guess that you would want to use Text not a blob as it will handle text not binary right. The Text fields are like a Varchar but with all the size you need.

http://dev.mysql.com/doc/refman/5.0/en/blob.html

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

OK, so your table-of-contents is actually an array of strings.

Your choices are:

  • Create a new table so that each array element is a row in the table. You will probably need columns for external key and each string's index within its table-of-contents. This gives you a proper One-to-Many relationship between the ToC and its elements (or the Article and the ToC elements ... depending on how you chose to model it.) (Borrowed from @oldCurmudgeon's comment.)

  • Use Java serialization to serialize the String[] and use a BLOB column type.

  • Create (or choose) a custom encoding scheme to turn the String[] into a textual representation and store it in a TEXT column.

If you want to be able to query and/or update the individual table-of-content elements, then the first scheme is the way to go.


@ppeterka points out (correctly) that it would be good for you to read up on the topic of Database Normalization ... which is about designing schemas to reduce duplication of information. In this case, it will help you weigh up the alternative ways of turning an abstract model (e.g. a "One to Many relationship") into a database schema.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • For the first option, does it mean I'll need an table for each array? – Louis Hong Aug 15 '13 at 08:10
  • No. You need one table for to represent all of the ToC arrays, and you use the other columns to determine which ToC each row belongs to. This is normal SQL database stuff ... – Stephen C Aug 15 '13 at 08:12
  • It would also be wise to add a brief notion of [database normalization](https://en.wikipedia.org/wiki/Database_normalization), as it might be useful for the OP, and anybody finding this page in the long term. – ppeterka Aug 15 '13 at 08:54
  • @ppeterka - hmmm, maybe. But I don't think this question is particularly searchable. – Stephen C Aug 15 '13 at 09:00
  • @StephenC I've been surprised many times what and how people search the intartubez - who knows? :) – ppeterka Aug 15 '13 at 09:02
-1

See what-is-object-serialization for serialization and deserialization of Objects. Then store the serialized data bytes in a BLOB column in SQL.

Community
  • 1
  • 1