Sub topic 15
Differences between database systems
This chapter assumes you can write CREATE TABLE, know primary and foreign keys, and have run ALTER TABLE ... ADD COLUMN on at least one database. Here you will learn where the major dialects (PostgreSQL, MySQL, SQL Server, Oracle and SQLite) disagree about ALTER TABLE and DROP TABLE. You will also learn how to check a script before you run it on an unfamiliar system.
Why dialects differ
The SQL standard defines the core of ALTER TABLE: add a column, drop a column, set or drop a default, add or drop a constraint. Each vendor then implemented it at a different time, with different internals, and added its own extensions. The result is three kinds of difference:
- Syntax differences. The same operation is spelled differently.
- Missing features. One system cannot do an operation at all, or can do only part of it.
- Behavior differences. The same statement runs, but locking, transactions or rewrites of data differ.
Behavior differences are the most dangerous, because the script does not fail. It runs, and then does something you did not expect.
Adding a column
The portable form is the one you have already seen:
ALTER TABLE customers ADD COLUMN email VARCHAR(255);This works in PostgreSQL, MySQL and SQLite. Oracle and SQL Server omit the word COLUMN:
-- SQL Server
ALTER TABLE customers ADD email VARCHAR(255);
-- Oracle
ALTER TABLE customers ADD (email VARCHAR2(255));Oracle also uses VARCHAR2 as its preferred string type, and SQL Server uses ADD for several columns separated by commas, while PostgreSQL and MySQL repeat ADD COLUMN for each one.
Changing a column's type
This is the operation with the widest spread of syntax:
| Database | Statement | Notes |
|---|---|---|
| PostgreSQL | `ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(200);` | Nullability and default are unchanged. Use `USING expr` if no implicit cast exists. |
| MySQL | `ALTER TABLE t MODIFY COLUMN c VARCHAR(200) NOT NULL;` | You must restate the whole column definition. Anything you omit, such as `NOT NULL` or a default, is reset. |
| SQL Server | `ALTER TABLE t ALTER COLUMN c VARCHAR(200) NOT NULL;` | You must also restate nullability. If you omit it, the column becomes nullable. |
| Oracle | `ALTER TABLE t MODIFY (c VARCHAR2(200));` | Changes are restricted if the column holds data of an incompatible type. |
| SQLite | Not supported directly | Recreate the table. |
The MySQL and SQL Server rows contain a classic trap. Run MODIFY COLUMN name VARCHAR(200) on a NOT NULL column in MySQL, and the column silently becomes nullable. Always copy the full existing definition and change only the part you mean to change.
Renaming
Renaming a column with RENAME COLUMN ... TO ... works in PostgreSQL, MySQL 8, Oracle and SQLite 3.25 and later:
ALTER TABLE customers RENAME COLUMN name TO full_name;Older MySQL (5.7 and earlier) required CHANGE, which, like MODIFY, needs the full column definition. SQL Server has no ALTER TABLE rename at all. It uses a stored procedure:
-- SQL Server: column
EXEC sp_rename 'customers.name', 'full_name', 'COLUMN';
-- SQL Server: table
EXEC sp_rename 'customers', 'clients';For renaming a table, the other systems use ALTER TABLE customers RENAME TO clients;. MySQL also accepts RENAME TABLE customers TO clients;.
Dropping constraints
Dropping a named constraint also varies. The standard form works in PostgreSQL, SQL Server, Oracle and recent MySQL:
ALTER TABLE orders DROP CONSTRAINT fk_orders_customer;MySQL before 8.0.19 needs a type-specific form for foreign keys:
ALTER TABLE orders DROP FOREIGN KEY fk_orders_customer;MySQL also uses DROP INDEX for unique constraints and DROP PRIMARY KEY for primary keys in older syntax. SQLite does not support ADD CONSTRAINT or DROP CONSTRAINT on an existing table. You declare constraints in CREATE TABLE and change them by rebuilding the table.
Dropping a column
ALTER TABLE customers DROP COLUMN email; works in PostgreSQL, MySQL, SQL Server and Oracle. Oracle also allows a parenthesized list, and SQL Server refuses to drop a column that still has a default constraint or an index on it until you remove those first. SQLite added DROP COLUMN in version 3.35. It refuses to drop a column that is part of a primary key, is indexed, or is used in a constraint or view.
DROP TABLE and IF EXISTS
DROP TABLE IF EXISTS orders; works in PostgreSQL, MySQL, SQLite and SQL Server 2016 and later. Oracle supports IF EXISTS only from version 23ai. On earlier Oracle versions, scripts catch the "table does not exist" error in a PL/SQL block.
CASCADE also differs:
| Database | Behavior of `DROP TABLE` with dependents |
|---|---|
| PostgreSQL | Refuses if a foreign key or view depends on the table. `CASCADE` drops the dependent constraints and views, not the referencing tables. |
| MySQL | Accepts `CASCADE` and `RESTRICT` but ignores them. A foreign key reference blocks the drop unless you set `foreign_key_checks = 0`. |
| SQL Server | No `CASCADE`. Refuses if a foreign key references the table. Drop the constraint first. |
| Oracle | Use `DROP TABLE t CASCADE CONSTRAINTS;` to remove referencing foreign keys. |
| SQLite | Performs an implicit `DELETE` first. With foreign keys enabled, this can fail if child rows exist. |
Because CASCADE is silently ignored in MySQL, a script written for PostgreSQL can behave differently there without raising an error.
Transactions
PostgreSQL, SQL Server and SQLite can roll back most DDL statements:
-- PostgreSQL
BEGIN;
ALTER TABLE customers ADD COLUMN phone VARCHAR(30);
ALTER TABLE customers DROP COLUMN phone;
ROLLBACK;MySQL and Oracle commit the current transaction implicitly before and after a DDL statement. A ROLLBACK after that has nothing to undo. In these systems, a migration that fails halfway leaves the schema in a partly changed state, so you must write the reverse steps yourself.
Locking and rewrites
The same statement can take milliseconds in one system and lock a large table for minutes in another. Examples:
- PostgreSQL 11 and later adds a column with a constant default without rewriting the table. Changing a column's type often rewrites it.
- MySQL 8 can add a column with
ALGORITHM=INSTANTin many cases. You can requestALGORITHMandLOCKexplicitly, and MySQL then fails instead of silently choosing a slower method. - SQL Server adding a nullable column is a metadata change, but altering a type is usually a size-of-data operation.
- SQLite rewrites the table for most changes other than a simple add.
A portable pre-flight checklist
- Write down the exact version of the target database. Version matters as much as vendor:
RENAME COLUMNandDROP COLUMNin SQLite and MySQL depend on it. - Check the syntax of every statement against that version's documentation.
- Run the script on a copy of the data with the same engine and version.
- Decide whether the engine can roll back DDL. If not, prepare reverse statements.
- Read each type-change statement for lost
NOT NULLor default settings.
Exercise
You run ALTER TABLE customers MODIFY COLUMN name VARCHAR(200); on a MySQL table where name was VARCHAR(100) NOT NULL. What is the risk, and how do you fix the statement?
Show answer
MySQL replaces the whole column definition, so omitting NOT NULL makes the column nullable. Restate the full definition: ALTER TABLE customers MODIFY COLUMN name VARCHAR(200) NOT NULL;.
Exercise
A migration for PostgreSQL contains DROP TABLE customers CASCADE;. Name two ways it behaves differently on MySQL and on SQL Server.
Show answer
MySQL accepts CASCADE but ignores it, so the drop is still blocked by a foreign key from orders. SQL Server does not accept CASCADE at all, so the statement is a syntax error, and you must drop the foreign key constraint first.
Exercise
A migration with three ALTER TABLE statements fails on the third. On which of PostgreSQL and MySQL is the schema left unchanged if you wrapped the script in BEGIN and ROLLBACK, and why?
Show answer
PostgreSQL. It supports transactional DDL, so the rollback undoes the first two statements. MySQL commits implicitly at each DDL statement, so the first two changes stay.