Här är tre sätt att få datatypen för en given kolumn i MariaDB.
\d
Kommando
I psql, \d
kommandot visar information om tabeller, vyer, materialiserade vyer, index, sekvenser eller främmande tabeller.
Vi kan använda detta kommando för att kontrollera datatypen för kolumnerna i en given tabell:
\d public.actor
Resultat:
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()
Vi kan lägga till ett plustecken (+
) för att avslöja utökad information:
\d+ public.actor
Resultat:
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
Visa
information_schema.columns
vyn innehåller information om kolumner:
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';
Resultat:
+-------------+-------------------+------------+--------------+ | column_name | data_type | max_length | octet_length | +-------------+-------------------+------------+--------------+ | first_name | character varying | 45 | 180 | +-------------+-------------------+------------+--------------+
pg_typeof()
Funktion
pg_typeof()
funktion returnerar OID för datatypen för värdet som skickas till den.
Vi kan därför använda det för att få datatypen för en kolumn genom att skicka kolumnen till pg_typeof()
funktion när du frågar tabellen:
SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;
Resultat:
+-------------------+ | pg_typeof | +-------------------+ | character varying | +-------------------+
I PostgreSQL, character varying
är namnet för varchar
(faktiskt varchar
är aliaset för character varying
).