Berikut adalah tiga cara untuk mendapatkan tipe data kolom tertentu di MariaDB.
\d
Perintah
Dalam psql, \d
perintah menunjukkan informasi tentang tabel, tampilan, tampilan terwujud, indeks, urutan, atau tabel asing.
Kita dapat menggunakan perintah ini untuk memeriksa tipe data kolom dalam tabel yang diberikan:
\d public.actor
Hasil:
Table "public.actor" +-------------+-----------------------------+-----------+----------+-----------------------------------------+ | Column | Type | Collation | Nullable | Default | +-------------+-----------------------------+-----------+----------+-----------------------------------------+ | actor_id | integer | | not null | nextval('actor_actor_id_seq'::regclass) | | first_name | character varying(45) | | not null | | | last_name | character varying(45) | | not null | | | last_update | timestamp without time zone | | not null | now() | +-------------+-----------------------------+-----------+----------+-----------------------------------------+ Indexes: "actor_pkey" PRIMARY KEY, btree (actor_id) "idx_actor_last_name" btree (last_name) Referenced by: TABLE "film_actor" CONSTRAINT "film_actor_actor_id_fkey" FOREIGN KEY (actor_id) REFERENCES actor(actor_id) ON UPDATE CASCADE ON DELETE RESTRICT Triggers: last_updated BEFORE UPDATE ON actor FOR EACH ROW EXECUTE FUNCTION last_updated()
Kita dapat menambahkan tanda plus (+
) untuk mengungkapkan informasi tambahan:
\d+ public.actor
Hasil:
Table "public.actor" +-------------+-----------------------------+-----------+----------+-----------------------------------------+----------+--------------+-------------+ | Column | Type | Collation | Nullable | Default | Storage | Stats target | Description | +-------------+-----------------------------+-----------+----------+-----------------------------------------+----------+--------------+-------------+ | actor_id | integer | | not null | nextval('actor_actor_id_seq'::regclass) | plain | | | | first_name | character varying(45) | | not null | | extended | | | | last_name | character varying(45) | | not null | | extended | | | | last_update | timestamp without time zone | | not null | now() | plain | | | +-------------+-----------------------------+-----------+----------+-----------------------------------------+----------+--------------+-------------+ Indexes: "actor_pkey" PRIMARY KEY, btree (actor_id) "idx_actor_last_name" btree (last_name) Referenced by: TABLE "film_actor" CONSTRAINT "film_actor_actor_id_fkey" FOREIGN KEY (actor_id) REFERENCES actor(actor_id) ON UPDATE CASCADE ON DELETE RESTRICT Triggers: last_updated BEFORE UPDATE ON actor FOR EACH ROW EXECUTE FUNCTION last_updated() Access method: heap
information_schema.columns
Lihat
information_schema.columns
tampilan berisi informasi tentang kolom:
SELECT
column_name,
data_type,
character_maximum_length AS max_length,
character_octet_length AS octet_length
FROM
information_schema.columns
WHERE
table_schema = 'public' AND
table_name = 'actor' AND
column_name = 'first_name';
Hasil:
+-------------+-------------------+------------+--------------+ | column_name | data_type | max_length | octet_length | +-------------+-------------------+------------+--------------+ | first_name | character varying | 45 | 180 | +-------------+-------------------+------------+--------------+
pg_typeof()
Fungsi
pg_typeof()
fungsi mengembalikan OID dari tipe data nilai yang diteruskan ke sana.
Oleh karena itu kita dapat menggunakannya untuk mendapatkan tipe data kolom dengan meneruskan kolom ke pg_typeof()
fungsi saat menanyakan tabel:
SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;
Hasil:
+-------------------+ | pg_typeof | +-------------------+ | character varying | +-------------------+
Di PostgreSQL, character varying
adalah nama untuk varchar
(sebenarnya, varchar
adalah alias untuk character varying
).