SQL · Module 2 · Lesson 21 min read

Text, Boolean & UUID Types

Text types

TypeMeaning
TEXTany length ✅ (recommended in PostgreSQL)
VARCHAR(n)up to n characters; error if longer
CHAR(n)fixed length, padded with spaces (rarely useful)
  • In PostgreSQL, TEXT and VARCHAR have the same performance
  • To limit length, prefer TEXT + CHECK (length(name) <= 100) — it's easier to change later
  • Text comparison is case-sensitive: 'Asha' = 'asha' → false

BOOLEAN

  • Values: true, false and NULL (unknown)
  • Accepts input like 'yes', 'no', 't', 'f', '1', '0'
  • Name booleans clearly: is_active, paid, has_children
  • Filter with WHERE paid or WHERE NOT paid

UUID

A 128-bit universally unique identifier: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11

  • Generate with gen_random_uuid() (built in since PostgreSQL 13)
  • ✅ Unique across tables, servers and devices — great for public IDs and offline-created records
  • ✅ Doesn't reveal how many users you have (/users/5 reveals a lot!)
  • ❌ Bigger (16 bytes vs 4 for INT) and random UUIDs make indexes slightly slower
  • Supabase uses UUIDs for auth.users.id

Other useful types

  • ENUM – a fixed list of values: CREATE TYPE mood AS ENUM ('happy', 'sad')
  • TEXT[] – arrays (our expenses.tags)
  • JSONB – JSON documents (Unit 13)
  • INET – IP addresses, BYTEA – binary data (store files in storage buckets instead)

Example

SQL
-- Text comparison is case-sensitive
SELECT 'Asha' = 'asha'               AS exact_match,      -- false
       lower('Asha') = lower('asha') AS case_insensitive; -- true

-- VARCHAR(n) rejects longer text
CREATE TABLE contacts (
  id        UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name      TEXT NOT NULL CHECK (length(name) BETWEEN 2 AND 60),
  country   CHAR(2) NOT NULL DEFAULT 'NP',          -- ISO code
  phone     VARCHAR(15),
  is_active BOOLEAN NOT NULL DEFAULT true
);

INSERT INTO contacts (name, phone) VALUES ('Asha Sharma', '9801234567');
INSERT INTO contacts (name, phone, is_active) VALUES ('Ravi Thapa', '9807654321', 'no');

SELECT id, name, country, is_active FROM contacts;
-- id is a random UUID like 3f1c9a4e-...; is_active: true / false

SELECT name FROM contacts WHERE is_active;      -- only active contacts

-- ❌ Too long for VARCHAR(15):
-- INSERT INTO contacts (name, phone) VALUES ('Test', '+977-9801234567890');
-- ERROR: value too long for type character varying(15)

-- ENUM type
CREATE TYPE payment_method AS ENUM ('cash', 'card', 'esewa', 'khalti');
SELECT 'esewa'::payment_method;

DROP TABLE contacts;
DROP TYPE payment_method;