Relational Database Systems Concepts, Design, SQL, PostgreSQL, MySQL, and Applications

Appendices

Appendix D. SQL Data Types and Constraints

The type and constraint menu of Chapter 9, consolidated for both platforms, with the canonical university column as the example wherever one exists.

Numeric types

TypeRange / precisionUseExample
TINYINT (MySQL)−128..127 (255 unsigned)Tiny flags, ratings—
SMALLINT±32 thousandYears, counts that stay smalladmission_year, credits
INTEGER / INT±2.1 billionIDs, countsstudent_id, section_id
BIGINT±9.2 quintillionLarge IDs, counterstxn_id in banking
NUMERIC(p,s) / DECIMAL(p,s)Exact, p total and s fractional digitsMoney, GPA — anything countablebudget NUMERIC(12,2), gpa NUMERIC(3,2)
REAL / FLOAT / DOUBLE PRECISIONBinary floating pointMeasurements, never money—
SERIAL (PG legacy)Integer + sequence shorthandAuto-increment keys—
GENERATED ... AS IDENTITYStandard auto-incrementNew surrogate keysChapter 9/15
AUTO_INCREMENT (MySQL)Per-table counterMySQL surrogate keysChapter 9/16
BOOLEAN (PG) / TINYINT(1) (MySQL)TRUE/FALSE/NULL / 0-1Flagsactive

Rules: exact numerics for anything aggregated as money or grades; NUMERIC(p,s) scale chosen as a domain rule (GPA NUMERIC(3,2) enforces 0.00–9.99); integers prefer the smallest fitting type; UNSIGNED (MySQL) doubles positive range.

Character and text types

TypeNotesExample
CHAR(n)Fixed-length, paddedgrade CHAR(2)
VARCHAR(n)Variable, length rulefull_name VARCHAR(60), course_id VARCHAR(8)
TEXT (PG)Unlimited, preferred defaultDescriptions
TINYTEXT/TEXT/MEDIUMTEXT/LONGTEXT (MySQL)Size tiersLarge bodies
BINARY/VARBINARY/BYTEA/BLOBBytesFiles, hashes
ENUM(...) (MySQL)Domain-as-type (Chapter 9's caution)—

Date and time types

TypeMeaningExample
DATECalendar dayhire_date
TIMEClock time—
TIMESTAMP / DATETIMEDate + timerecorded_at
TIMESTAMPTZ (PG)UTC-stored, zone-renderedEvent instants
TIMESTAMP (MySQL)UTC-converted, range 1970–2038MySQL event instants
DATETIME (MySQL)Literal, range 1000–9999Scheduled wall-clock facts
INTERVAL (PG) / interval syntax (MySQL)Durationshire_date + INTERVAL '1 year'

Other types

TypeUse
JSON / jsonbSemi-structured payloads (Chapter 15/16/27)
uuid (PG) / BINARY(16) (MySQL)Distributed identifiers
inet (PG)IP addresses
Arrays int[], ranges tstzrange, enums, domains (PG)Chapter 15's native toolbox
vector(n) (pgvector)Embeddings (Chapter 27)

Constraints

ConstraintEnforcesCanonical example
NOT NULLValue always knownfull_name NOT NULL
PRIMARY KEYIdentity: unique + not null (+ clustered in InnoDB)student_id, (student_id, section_id)
UNIQUENatural identity, alternate keysdept_name UNIQUE
FOREIGN KEY ... REFERENCESValues exist in the parentevery FK arrow on the ERD
ON DELETE CASCADE / RESTRICT / SET NULL / SET DEFAULTParent-removal policyChapter 6/9
CHECK (cond)Domain rules (NULL passes — add OR col IS NULL where absence is legal)CHECK (gpa BETWEEN 0.00 AND 4.00)
DEFAULT exprInsert-time fillcapacity DEFAULT 40
DEFERRABLE INITIALLY DEFERRED (PG)Check at COMMITChapter 15/22
NOT VALID + VALIDATE (PG)Online constraint addChapter 15
EXCLUDE USING gist (...) (PG)Non-overlap / complex conflictsChapter 29 (hotel bookings)
Generated columnsComputed stored valuesChapter 15

NULL rules (summary)

NULL means "value absent," not zero or empty string. Comparisons with NULL are UNKNOWN; WHERE keeps only TRUE. Aggregates skip NULLs except COUNT(*). IS NULL / IS NOT NULL are the tests; COALESCE, NULLIF, IS DISTINCT FROM (and MySQL's <=>) are the tools (Chapters 2, 10, 11).