求MySQL与PostgreSQL中information_schema.columns.data_type取值列表及生成方法
Hey there! Great question—let’s break this down for you, covering both MySQL and PostgreSQL, including how to pull the full list of data_type values yourself and the key differences between the two databases.
data_type Values Generate the Full List via SQL
The easiest way to get all possible data_type values in MySQL is to query the information_schema.columns table directly. Run this command:
SELECT DISTINCT data_type FROM information_schema.columns;
If you want to exclude system databases (like information_schema or mysql) to focus on user-defined types, you can filter them out:
SELECT DISTINCT data_type FROM information_schema.columns WHERE table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys');
Common data_type Values in MySQL
Here’s a curated list of typical values you’ll see:
CHARVARCHARTEXT/TINYTEXT/MEDIUMTEXT/LONGTEXTINT/TINYINT/SMALLINT/MEDIUMINT/BIGINTFLOAT/DOUBLE/DECIMALDATE/DATETIME/TIMESTAMP/TIME/YEARENUM/SETBINARY/VARBINARYBLOB/TINYBLOB/MEDIUMBLOB/LONGBLOBJSON
data_type Values Generate the Full List via SQL
Just like MySQL, you can query information_schema.columns to get all unique data_type values:
SELECT DISTINCT data_type FROM information_schema.columns;
Note: PostgreSQL uses SQL-standard type names, so even if you use shorthand (like varchar instead of character varying) when creating columns, the data_type field will return the standard name.
Common data_type Values in PostgreSQL
Here’s what you’ll typically encounter:
character(equivalent to MySQL’sCHAR)character varying(equivalent to MySQL’sVARCHAR)textinteger/smallint/bigintnumeric/real/double precisiondate/timestamp without time zone/timestamp with time zonetime without time zone/time with time zone/intervalbooleanjson/jsonbbytea(equivalent to MySQL’sBLOBtypes)uuid/inet/cidr/macaddrarrayenumrange
data_type Values - Naming Style: MySQL uses concise, shorthand type names (e.g.,
VARCHAR,INT), while PostgreSQL leans into SQL-standard verbose names (e.g.,character varying,integer). PostgreSQL does accept shorthand aliases in DDL, butdata_typewill always show the standard name. - String/Text Types: MySQL splits large text into multiple tiered types (
TINYTEXT,MEDIUMTEXT, etc.) based on storage limits. PostgreSQL uses a singletexttype for all unbounded text (you can add length constraints, butdata_typestill remainstext). - Numeric Types: MySQL includes
MEDIUMINTandYEAR(a specialized 1-byte numeric type), which PostgreSQL doesn’t have. PostgreSQL usesnumericfor arbitrary-precision decimals (similar to MySQL’sDECIMAL) andrealfor single-precision floats (equivalent to MySQL’sFLOAT). - Date/Time Features: PostgreSQL natively supports timezone-aware timestamp/time types (
timestamp with time zone,time with time zone) andintervaltypes for duration values. MySQL’sTIMESTAMPconverts to UTC but isn’t a true timezone-aware type, and it lacks a dedicatedintervaltype. - Specialized Types: PostgreSQL has built-in support for
uuid,inet(IP addresses),jsonb(binary-optimized JSON), andrangetypes—none of these are native in MySQL (you’d need to use workarounds like string storage or extensions). MySQL’sSETtype is unique to it; PostgreSQL’senumrequires explicit creation viaCREATE TYPE(vs. MySQL’s inlineENUMdefinition). - Arrays: PostgreSQL lists
ARRAYas a distinctdata_typefor array columns. MySQL doesn’t have a native array type, so you’d useJSONor comma-separated strings (resulting injsonorvarcharindata_type).
内容的提问来源于stack exchange,提问作者Damien Golding

