Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most "database independent" way of creating a variable length text field in a database

Tags:

sql

database

jdbc

I want to create a text field in the database, with no specific size (it will store text of length unknown in some case) - the particular text are serialized simple object (~ JSON)

What is the most database independent way to do this : - a varchar with no size specified (don't think all db support this) - a 'text' field, this seems to be common, but I don't believe it's a standard - a blob or other object of that kind ? - a varchar of a a very large size (that's inefficient and wastes disk space probably) - Other ?

I'm using JDBC, but I'd like to use something that is supported in most DB (oracle, mysql, postgresql, derby, HSQL, H2 etc...)

Thanks.

like image 218
Thibaut Colar Avatar asked May 25 '10 18:05

Thibaut Colar


1 Answers

a varchar of a a very large size (that's inefficient and wastes disk space probably)

That's gonna be the most portable option. Limit yourself to 2000 characters and you should be fine for most databases (oracle being the current 2000 limiter, but be wary of old mysql versions as well). I wouldn't worry too much about disk space, either. Most databases only use disk for the actual data saved in the field.

like image 61
Joel Coehoorn Avatar answered Nov 02 '22 01:11

Joel Coehoorn