Sub topic 18
How MySQL, SQL Server, Oracle, and PostgreSQL Differ
Assume you know SELECT, INSERT, UPDATE, and DELETE, and can create tables with basic data types. The key concepts are portable SQL, but generated values, referential actions, and constraint timing vary by database system.
Dialect differences
A primary key always identifies each row uniquely and cannot contain NULL. A foreign key still points to a parent key in every system, but the syntax and available options differ.
| Feature | PostgreSQL | MySQL | SQL Server | Oracle |
|---|---|---|---|---|
| Generated integer key | `GENERATED ... AS IDENTITY` | `AUTO_INCREMENT` | `IDENTITY` | Identity column or sequence |
| Default storage engine | Built into the database | Use InnoDB for foreign keys | Built into the database | Built into the database |
| `ON UPDATE CASCADE` | Supported | Supported by InnoDB | Supported | Not supported |
| Deferrable constraints | Supported | Not supported | Not supported | Supported |
| `RESTRICT` action | Supported | Usually treated like `NO ACTION` | Use `NO ACTION` | Use `NO ACTION` |
For example, this is PostgreSQL-style syntax:
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE orders (
order_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id INTEGER NOT NULL
REFERENCES customers (customer_id)
ON DELETE RESTRICT
);The equivalent generated-key declarations look different:
- MySQL uses
INT AUTO_INCREMENT. - SQL Server uses
INT IDENTITY(1,1). - Oracle can use
NUMBER GENERATED BY DEFAULT AS IDENTITY, or a sequence with a trigger or explicitNEXTVAL. - PostgreSQL commonly uses an identity column. Older PostgreSQL code may use
SERIAL, but identity columns make the generation rule clearer.
Oracle supports ON DELETE CASCADE and ON DELETE SET NULL, but it has no ON UPDATE referential action. PostgreSQL and Oracle can also defer selected constraint checks until transaction commit. This helps with circular references, but it does not make invalid data acceptable after the transaction commits.
Common mistakes and how to avoid them
Do not assume a foreign key automatically has an index. Add an index on a child column that is used for joins, parent deletes, or frequent filtering. MySQL may create supporting indexes in some cases, but relying on that hides an important performance decision.
Do not use a natural value as the primary key merely because it looks unique today. Email addresses, usernames, and business codes can change. A surrogate key gives rows a stable identity; keep the natural value protected with UNIQUE.
Do not confuse NO ACTION, RESTRICT, and CASCADE. Test deletes and updates with realistic related rows. CASCADE can remove many child rows, while SET NULL requires a nullable foreign-key column.
Finally, check NULL behavior for unique constraints in your database. Most systems allow several NULL values because NULL means unknown, but exact behavior and available indexes differ.
Exercise
Which design is safer when an email address may change: using the email as the primary key, or using a generated customer ID with a unique email constraint?
Show answer
Use the generated customer ID as the primary key and enforce email uniqueness separately.