Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you know when to use varchar and when to use text in sql?

It seems like a very arbitrary decision. Both can accomplish the same thing in most cases. By limiting the varchar length seems to me like you're shooting yourself in the foot cause you never know how long of a field you will need.

Is there any specific guideline for choosing VARCHAR or TEXT for your string fields?

I will be using postgresql with the sqlalchemy orm framework for python.

like image 457
TheOne Avatar asked Jan 16 '11 13:01

TheOne


1 Answers

In PostgreSQL there is no technical difference between varchar and text

You can see a varchar(nnn) as a text column with a check constraint that prohibits storing larger values.

So each time you want to have a length constraint, use varchar(nnn).

If you don't want to restrict the length of the data use text

like image 157
a_horse_with_no_name Avatar answered Nov 09 '22 10:11

a_horse_with_no_name