sql >> Database teknologi >  >> RDS >> PostgreSQL

3 måder at kontrollere en kolonnes datatype i PostgreSQL

Her er tre måder at få datatypen for en given kolonne i MariaDB på.

\d Kommando

I psql er \d kommando viser information om tabeller, visninger, materialiserede visninger, indeks, sekvenser eller fremmede tabeller.

Vi kan bruge denne kommando til at kontrollere datatypen for kolonnerne i en given tabel:

\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 tilføje et plustegn (+ ) for at afsløre udvidet 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 Vis

information_schema.columns visningen indeholder oplysninger om kolonner:

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 returnerer OID'en for datatypen for den værdi, der sendes til den.

Vi kan derfor bruge det til at få datatypen for en kolonne ved at sende kolonnen til pg_typeof() funktion, mens du forespørger i tabellen:

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

Resultat:

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

I PostgreSQL, character varying er navnet på varchar (faktisk varchar er aliaset for character varying ).


  1. Tjek, om der findes en tabel i Oracle

  2. Hvad er standard begrænsningsnavnet i SQL Server?

  3. Gotchas, når du bruger Microsoft Access Runtime

  4. Sådan får du sidste 1 times data i MySQL