sql >> Base de Datos >  >> RDS >> PostgreSQL

3 formas de verificar el tipo de datos de una columna en PostgreSQL

Aquí hay tres formas de obtener el tipo de datos de una columna dada en MariaDB.

El \d Comando

En psql, el \d El comando muestra información sobre tablas, vistas, vistas materializadas, índice, secuencias o tablas foráneas.

Podemos usar este comando para verificar el tipo de datos de las columnas en una tabla dada:

\d public.actor

Resultado:

                                             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()

Podemos añadir un signo más (+ ) para revelar información ampliada:

\d+ public.actor

Resultado:

                                                                 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

El information_schema.columns Ver

El information_schema.columns la vista contiene información sobre las columnas:

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';

Resultado:

+-------------+-------------------+------------+--------------+
| column_name |     data_type     | max_length | octet_length |
+-------------+-------------------+------------+--------------+
| first_name  | character varying |         45 |          180 |
+-------------+-------------------+------------+--------------+

El pg_typeof() Función

El pg_typeof() La función devuelve el OID del tipo de datos del valor que se le pasa.

Por lo tanto, podemos utilizarlo para obtener el tipo de datos de una columna pasando la columna a pg_typeof() función al consultar la tabla:

SELECT pg_typeof(first_name)
FROM public.actor
LIMIT 1;

Resultado:

+-------------------+
|     pg_typeof     |
+-------------------+
| character varying |
+-------------------+

En PostgreSQL, character varying es el nombre de varchar (en realidad, varchar es el alias para character varying ).