SQL data types form the backbone of a database schema. They dictate storage format, valid operations, and comparison rules for every value stored in a database. Standard SQL groups types into categories such as numeric (INTEGER, DECIMAL, FLOAT), character (CHAR, VARCHAR), boolean (BOOLEAN), temporal (DATE, TIME, TIMESTAMP), binary (BLOB, BYTEA), and special-purpose types like JSON, XML, spatial, and enumerated (ENUM). Each database adds proprietary extensions, but all systems rely on the same core principle: a value can only occupy a column if its type is compatible. Choosing the correct type impacts performance, disk footprint, index efficiency, accuracy, and data integrity. Numeric precision, string length, time zone handling, and locale collation rules vary by dialect and must be considered during design. Types can be cast or converted, but implicit conversion rules differ, which may lead to runtime errors or silent truncation. SQL also allows user-defined types (domains, composite, or custom classes) that encapsulate additional constraints. Nullability is orthogonal to type: NULL represents an unknown value of any declared type.
CAST, CONVERT, CREATE TABLE, ALTER TABLE, DOMAIN, ENUM, COLLATION
ANSI SQL-86
CHAR stores a fixed number of characters, padding with spaces as needed. VARCHAR stores variable-length strings up to the defined limit, saving space for uneven text.
Choose NUMERIC (DECIMAL) for exact precision like currency. FLOAT and REAL are approximate and can introduce rounding errors at scale.
Use ALTER TABLE with ALTER COLUMN ... TYPE. Provide a USING clause or CAST to convert existing data safely, e.g., `ALTER TABLE t ALTER COLUMN c TYPE BIGINT USING c::BIGINT;`.
Your string format likely does not match the database's default date format. Cast explicitly or set the correct `DATEFORMAT` or `lc_time` configuration.