Hier zijn drie manieren om het gegevenstype van een bepaalde kolom in MariaDB te krijgen.
De \d
Commando
In psql, de \d
commando toont informatie over tabellen, weergaven, gerealiseerde weergaven, index, reeksen of vreemde tabellen.
We kunnen dit commando gebruiken om het gegevenstype van de kolommen in een bepaalde tabel te controleren:
\d public.actor
Resultaat:
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()
We kunnen een plusteken toevoegen (+
) om uitgebreide informatie te onthullen:
\d+ public.actor
Resultaat:
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
De information_schema.columns
Bekijk
De information_schema.columns
weergave bevat informatie over kolommen:
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';
Resultaat:
+-------------+-------------------+------------+--------------+ | column_name | data_type | max_length | octet_length | +-------------+-------------------+------------+--------------+ | first_name | character varying | 45 | 180 | +-------------+-------------------+------------+--------------+
De pg_typeof()
Functie
De pg_typeof()
functie retourneert de OID van het gegevenstype van de waarde die eraan wordt doorgegeven.
We kunnen het daarom gebruiken om het gegevenstype van een kolom te krijgen door de kolom door te geven aan de pg_typeof()
functie tijdens het opvragen van de tabel:
SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;
Resultaat:
+-------------------+ | pg_typeof | +-------------------+ | character varying | +-------------------+
In PostgreSQL, character varying
is de naam voor varchar
(eigenlijk varchar
is de alias voor character varying
).