Hier sind drei Möglichkeiten, den Datentyp einer bestimmten Spalte in MariaDB abzurufen.
Der \d
Befehl
In psql der \d
Der Befehl zeigt Informationen über Tabellen, Ansichten, materialisierte Ansichten, Indizes, Sequenzen oder Fremdtabellen an.
Wir können diesen Befehl verwenden, um den Datentyp der Spalten in einer bestimmten Tabelle zu überprüfen:
\d public.actor
Ergebnis:
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()
Wir können ein Pluszeichen anhängen (+
), um erweiterte Informationen anzuzeigen:
\d+ public.actor
Ergebnis:
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
Die information_schema.columns
Anzeigen
Die information_schema.columns
Ansicht enthält Informationen über Spalten:
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';
Ergebnis:
+-------------+-------------------+------------+--------------+ | column_name | data_type | max_length | octet_length | +-------------+-------------------+------------+--------------+ | first_name | character varying | 45 | 180 | +-------------+-------------------+------------+--------------+
Der pg_typeof()
Funktion
Der pg_typeof()
Die Funktion gibt die OID des Datentyps des Werts zurück, der ihr übergeben wird.
Wir können es daher verwenden, um den Datentyp einer Spalte zu erhalten, indem wir die Spalte an pg_typeof()
übergeben Funktion beim Abfragen der Tabelle:
SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;
Ergebnis:
+-------------------+ | pg_typeof | +-------------------+ | character varying | +-------------------+
In PostgreSQL character varying
ist der Name für varchar
(eigentlich varchar
ist der Alias für character varying
).