UUID Use Case

UUID Foreign Key

When your parent table uses a UUID key, the child table's foreign key must use the same 128-bit type — not a string of a different length.

Matches parent typeIndex the FKReferential integrity

Generated locally with the Web Crypto API — nothing leaves your browser.

What Is UUID Foreign Key?

How to generate

  1. Declare both columns with the same UUID/BINARY(16) type.
  2. Index the foreign key column (most engines do this automatically).
  3. Add ON DELETE rules that match your domain (CASCADE, RESTRICT).

Use Cases

Code Examples

PostgreSQL
CREATE TABLE orders (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  customer_id UUID REFERENCES customers(id)
);

Frequently Asked Questions

Can a UUID FK reference a BIGINT PK?
No — types must match. If the parent is BIGINT, the child FK must be BIGINT; if the parent is UUID, the FK must be UUID.
Do I need to index a UUID foreign key?
Yes. Without an index, joins and cascading deletes scan the child table on every operation.

Related tools