Oto trzy sposoby uzyskania typu danych danej kolumny w MariaDB.
\d
Polecenie
W psql \d
Polecenie wyświetla informacje o tabelach, widokach, widokach zmaterializowanych, indeksie, sekwencjach lub tabelach obcych.
Możemy użyć tego polecenia, aby sprawdzić typ danych kolumn w danej tabeli:
\d public.actor
Wynik:
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()
Możemy dodać znak plus (+
) aby ujawnić rozszerzone informacje:
\d+ public.actor
Wynik:
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
Kolumny information_schema.columns
Zobacz
Kolumny information_schema.columns
widok zawiera informacje o kolumnach:
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';
Wynik:
+-------------+-------------------+------------+--------------+ | column_name | data_type | max_length | octet_length | +-------------+-------------------+------------+--------------+ | first_name | character varying | 45 | 180 | +-------------+-------------------+------------+--------------+
pg_typeof()
Funkcja
pg_typeof()
funkcja zwraca OID typu danych wartości, która jest do niej przekazywana.
Możemy zatem użyć go, aby uzyskać typ danych kolumny, przekazując kolumnę do pg_typeof()
funkcja podczas odpytywania tabeli:
SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;
Wynik:
+-------------------+ | pg_typeof | +-------------------+ | character varying | +-------------------+
W PostgreSQL, character varying
to nazwa dla varchar
(właściwie varchar
jest aliasem character varying
).