---
title: "The silent syntax difference in foreign keys between Postgres and MySQL"
description: "MySQL parses but silently ignores inline column-level `REFERENCES` clauses, so a foreign key written Postgres-style (`customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE`) creates no constraint, no index and no cascade behavior in MySQL."
url: "https://swe.quest/articles/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql-36df1bac8b9567ad"
canonical: "https://neon.com/blog/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql"
source: "Neon"
published: "2024-06-05"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["mysql","postgres","sql","schema-migrations"]
---

# The silent syntax difference in foreign keys between Postgres and MySQL

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-06-05, 6 min read, How-to. Original: [https://neon.com/blog/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql](https://neon.com/blog/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql)

## Summary

MySQL parses but silently ignores inline column-level \`REFERENCES\` clauses, so a foreign key written Postgres-style (\`customer\_id INT REFERENCES customers(customer\_id) ON DELETE CASCADE\`) creates no constraint, no index and no cascade behavior in MySQL. Orphaned rows insert without error. Only a separate table-level \`FOREIGN KEY (...) REFERENCES ...\` clause is enforced. Use the explicit \`FOREIGN KEY\` form in both databases, review schemas when porting between them, and test that constraints actually reject invalid data.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [MySQL](https://swe.quest/tags/mysql.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)

## Prerequisites

- SQL DDL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- relational foreign keys

## Keywords

MySQL, PostgreSQL, FOREIGN KEY, Neon

## Related posts

- [Fix a top cause of slow queries in PostgreSQL (no slow query log needed)](https://swe.quest/articles/fix-a-top-cause-of-slow-queries-in-postgresql-no-slow-query-log-needed-4d1a4c0df1aa9a0d.md): Render · 2024-12-12 · Medium · 7 min read · PostgreSQL, Performance engineering, SQL
  PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans.
- [LLM Rules for Postgres](https://swe.quest/articles/llm-rules-for-postgres-ad82e8ad652f6e5a.md): Neon · 2025-08-29 · Medium · 4 min read · PostgreSQL, Code review, Schema migrations, AI for coding, LLMs
  Six explicit Postgres rules, drawn from building the wispbit review bot, let an LLM code reviewer or coding agent catch common migration and query mistakes before production.
- [Postgres to ClickHouse: Data Modeling Tips](https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-aec1f81e14af15d4.md): ClickHouse · 2024-08-28 · Medium · 9 min read · Analytics & OLAP, PostgreSQL, Event-driven architecture, SQL, Replication
  Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (\_peerdb\_version, \_peerdb\_is\_deleted), so duplicates persist until background merges run.
- [Remember to use primary keys vs unique constraints in Postgres](https://swe.quest/articles/remember-to-use-primary-keys-vs-unique-constraints-in-postgres-429326ad5b407be8.md): Neon · 2024-05-15 · Medium · 5 min read · PostgreSQL, Schema migrations, Replication
  Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication.
- [Safely Adding NOT NULL Columns to Your Database Tables](https://swe.quest/articles/safely-adding-not-null-columns-to-your-database-tables-0c4b04a1141f0507.md): Shopify Engineering · 2022-10-04 · Medium · 7 min read · Schema migrations, MySQL, Databases
  Adding a NOT NULL column through LHM's shadow-table migration (new table, replication triggers, batched copy, rename) can break writes or silently lose data depending on the DEFAULT clause, an added UNIQUE INDEX and the MySQL SQL mode.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
