What is the datatype bytea and when would I use it?

大憨熊 提交于 2019-12-04 02:46:49

问题


In Postgres there is a datatype called bytea

The Postgres docs are here for it: http://www.postgresql.org/docs/9.0/static/datatype-binary.html

I cannot understand when I would ever use this - nor can I really understand the purpose of this datatype.

I have come across this term bytea several times and starting to wonder to myself "It seems like they expect me to understand this... Maybe I should find out what it is."

If anybody can give a simple definition for it and some given circumstance of when I would possibly use it, that would be super helpful.

Thanks.


回答1:


I think the documentation is reasonably clear on the differences between bytea and text:

Binary strings are distinguished from character strings in two ways. First, binary strings specifically allow storing octets of value zero and other "non-printable" octets (usually, octets outside the range 32 to 126). Character strings disallow zero octets, and also disallow any other octet values and sequences of octet values that are invalid according to the database's selected character set encoding. Second, operations on binary strings process the actual bytes, whereas the processing of character strings depends on locale settings. In short, binary strings are appropriate for storing data that the programmer thinks of as "raw bytes", whereas character strings are appropriate for storing text.

http://www.postgresql.org/docs/9.0/static/datatype-binary.html

... it has to do with whether the contents are "text" (subject to the locale and internationalizations settings you've applied to your server configuration and the OS on which you're running it) vs. arrays of "octets" (sequences of 8-bit binary values --- commonly referred to as "bytes").

(There are some technical distinctions between the term "byte" and the term "octet" -- because, historically, some platforms and computing devices used "bytes" with parity and/or stop bits while the term "octets" always means exactly 8-bits; a term that was introduced to clarify specifications and documentation for networking protocols).




回答2:


VARBINARY, which is an equivalent to BYTEA in Postgres, can be used to store OAuth access tokens.

create table oauth_access_token (
  token_id VARCHAR(255),
  token BYTEA,
  .......
)


来源:https://stackoverflow.com/questions/34486931/what-is-the-datatype-bytea-and-when-would-i-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!