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?
- A foreign key is just a column that stores the parent's primary key. If the parent key is a UUID, the FK column must be UUID (or BINARY(16)) too.
- Mismatches — e.g. UUID in one table and CHAR(36) in another — turn a fast integer-like comparison into a slow string comparison and block index usage.
How to generate
- Declare both columns with the same UUID/BINARY(16) type.
- Index the foreign key column (most engines do this automatically).
- Add ON DELETE rules that match your domain (CASCADE, RESTRICT).
Use Cases
- Order → customer references in an e-commerce schema.
- Comment → post relationships in a CMS.
- Event → aggregate links in event sourcing.
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
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterUUID as a Primary KeyUUID Database IDUUID Foreign KeyUUID Shard KeyUUID Partition KeyUUID in a Composite KeyUUID Surrogate KeyUUID API KeyUUID Session TokenUUID Access TokenUUID Refresh TokenUUID Idempotency KeyUUID Request IDUUID Correlation IDUUID Trace IDUUID CSRF TokenUUID NonceUUID FilenameUUID Object KeyUUID S3 KeyUUID Upload IDUUID Cache KeyUUID Blob NameUUID Temp FileUUID User IDUUID Account IDUUID Customer IDUUID Device IDUUID Tenant IDUUID Client IDUUID Anonymous IDUUID Member IDUUID Message IDUUID Event IDUUID Job IDUUID Task IDUUID Queue Message IDUUID Transaction IDUUID Order IDUUID Invoice IDUUID Payment IDUUID Reset TokenUUID Verification TokenUUID Invite CodeUUID API SecretUUID Share LinkUUID Magic LinkUUID Node IDUUID Worker IDUUID Lock IDUUID Cluster IDUUID Test DataUUID Seed DataUUID FixtureUUID Mock IDUUID Placeholder IDUUID Sample ID