charは文字列を格納する文字型。
https://www.postgresql.jp/docs/9.4/datatype-character.html
固定長であり、後ろの余った部分は空白で埋められる。
ただ、LENGTHでは、後ろの空白をカウントしないので、OCTET_LENGTHで実際に使用されている文字数を出してみる。
postgres@vbox:~$ psql -l
ユーザー postgres のパスワード:
データベース一覧
名前 | 所有者 | エンコーディング | ロケールプロバイダー | 照合順序 | Ctype(変換演算子) | ICUロケール | ICUルール: | アクセス権限
--------------+----------+------------------+----------------------+-------------+-------------------+-------------+------------+-----------------------
postgres | postgres | UTF8 | libc | ja_JP.UTF-8 | ja_JP.UTF-8 | | |
postgresdb1 | postgres | UTF8 | libc | ja_JP.UTF-8 | ja_JP.UTF-8 | | |
postgrestest | postgres | UTF8 | libc | ja_JP.UTF-8 | ja_JP.UTF-8 | | |
template0 | postgres | UTF8 | libc | ja_JP.UTF-8 | ja_JP.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | ja_JP.UTF-8 | ja_JP.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(5 行)
postgres@vbox:~$ psql -d postgresdb1
ユーザー postgres のパスワード:
psql (16.8)
"help"でヘルプを表示します。
postgresdb1=# \dt
リレーションが見つかりませんでした。
postgresdb1=# CREATE TABLE chartest (name char(10));
CREATE TABLE
postgresdb1=# \dt
リレーション一覧
スキーマ | 名前 | タイプ | 所有者
----------+----------+----------+----------
public | chartest | テーブル | postgres
(1 行)
postgresdb1=# INSERT INTO chartest (name) VALUES('abcde');
INSERT 0 1
postgresdb1=# SELECT name, LENGTH(name), OCTET_LENGTH(name) FROM chartest;
name | length | octet_length
------------+--------+--------------
abcde | 5 | 10
(1 行)
postgresdb1=#charでは'abcde'はLENGTHで5文字だが、OCTET_LENGTHだと10文字使ってるのがわかる。