全栈开发
数据库
【Q229】在数据库中一个字段如果是字符串类型的,那应该设置哪种数据类型

在数据库中一个字段如果是字符串类型的,那应该设置哪种数据类型

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 230 (opens in a new tab)

Author 回答者: shfshanyue (opens in a new tab)

以下答案仅说明在 postgres 中的情况:

在 postgres 有三种 varcharchar 以及 text,其中三者没有性能差异,见官方文档

Different from other database systems, in PostgreSQL, there is no performance difference among three character types. In most situation, you should use text or varchar, and varchar(n) if you want PostgreSQL to check for the length limit

所以,选择 text 是最好的方案