Voici trois façons d'obtenir le type de données d'une colonne donnée dans MariaDB.
Le \d
Commande
En psql, le \d
La commande affiche des informations sur les tables, les vues, les vues matérialisées, l'index, les séquences ou les tables étrangères.
Nous pouvons utiliser cette commande pour vérifier le type de données des colonnes dans une table donnée :
\d public.actor
Résultat :
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()
Nous pouvons ajouter un signe plus (+
) pour révéler des informations étendues :
\d+ public.actor
Résultat :
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
Les information_schema.columns
Afficher
Le information_schema.columns
la vue contient des informations sur les colonnes :
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';
Résultat :
+-------------+-------------------+------------+--------------+ | column_name | data_type | max_length | octet_length | +-------------+-------------------+------------+--------------+ | first_name | character varying | 45 | 180 | +-------------+-------------------+------------+--------------+
Le pg_typeof()
Fonction
Le pg_typeof()
La fonction renvoie l'OID du type de données de la valeur qui lui est transmise.
On peut donc l'utiliser pour obtenir le type de données d'une colonne en passant la colonne au pg_typeof()
fonction lors de l'interrogation de la table :
SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;
Résultat :
+-------------------+ | pg_typeof | +-------------------+ | character varying | +-------------------+
Dans PostgreSQL, character varying
est le nom de varchar
(en fait, varchar
est l'alias pour character varying
).