Thread: please advise on column data type

please advise on column data type

From
"jack"
Date:
When I create a table can I always use the rule, use VARCHAR for all
variable length string data and use CHAR for only fixed length string data.
Or I have to consider if the column will be set as an index or primary key,
it would be better to use CHAR for better performance.

JACK



Re: please advise on column data type

From
"Josh Berkus"
Date:
Jack,

Please post your questions to PGSQL-NOVICE in the future.  That is the
more appropriate venue for your type of question.  PGSQL-SQL is more
appropriate for advanced SQL questions and arguments about the SQL-92
standard.

> When I create a table can I always use the rule, use VARCHAR for all
> variable length string data and use CHAR for only fixed length string
> data.
> Or I have to consider if the column will be set as an index or
> primary key,
> it would be better to use CHAR for better performance.

In some databases, yes.  However, in PostgreSQL, there is no
performance difference between CHAR, VARCHAR, and TEXT.  So use what's
appropriate for the data being stored.

-Josh Berkus




Re: please advise on column data type

From
Tom Lane
Date:
"Josh Berkus" <josh@agliodbs.com> writes:
> In some databases, yes.  However, in PostgreSQL, there is no
> performance difference between CHAR, VARCHAR, and TEXT.  So use what's
> appropriate for the data being stored.

In fact, char() is probably *worse* than the other alternatives, at
least for data that doesn't have a clearly defined length, because
it forces blank-padding on you.

I'd use char(N) for US postal abbreviations (state = char(2), etc)
and not a lot else.  If you have an *error checking* reason to use
char(N), then use it.  If you think you are getting a performance
improvement, then you are far out in left field.
        regards, tom lane