Sub topic 15
Differences Between Database Systems
Assume you know SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, primary keys, and foreign keys. This chapter uses PostgreSQL, MySQL, SQL Server, and SQLite examples.
SQL has common ideas, but each database system, or dialect, uses its own syntax and rules. Always identify the target database before running a schema-change script.
The same change can use different syntax
Adding a nullable column is similar in most systems. SQL Server usually omits the word COLUMN.
-- PostgreSQL, MySQL, and SQLite
ALTER TABLE customers ADD COLUMN email VARCHAR(255);
-- SQL Server
ALTER TABLE customers ADD email VARCHAR(255);Changing a column type differs more substantially:
-- PostgreSQL
ALTER TABLE customers
ALTER COLUMN name TYPE VARCHAR(200);
-- MySQL
ALTER TABLE customers
MODIFY COLUMN name VARCHAR(200) NOT NULL;
-- SQL Server
ALTER TABLE customers
ALTER COLUMN name VARCHAR(200) NOT NULL;These statements are not interchangeable. MySQL’s MODIFY COLUMN describes the complete new definition. PostgreSQL separates the column name from the TYPE clause. SQL Server uses ALTER COLUMN.
Renaming a column is broadly consistent in modern systems:
ALTER TABLE customers RENAME COLUMN name TO full_name;PostgreSQL, MySQL 8, Oracle, and SQLite 3.25 and later support this form. SQL Server commonly uses its procedure instead:
EXEC sp_rename 'customers.name', 'full_name', 'COLUMN';Constraints and table removal
Constraint syntax also varies. PostgreSQL, SQL Server, and newer MySQL versions support named constraints with DROP CONSTRAINT:
ALTER TABLE orders
ADD CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers (id);
ALTER TABLE orders
DROP CONSTRAINT fk_orders_customer;Some MySQL versions use DROP FOREIGN KEY fk_orders_customer for a foreign key. The constraint name is essential because it gives you a stable name for later changes.
DROP TABLE removes the table definition and its stored rows. IF EXISTS prevents an error when the table is already absent:
DROP TABLE IF EXISTS orders;Support for IF EXISTS, CASCADE, column alterations, and transactional DDL is not identical across systems. Check the documentation for the exact version you use.
| Task | PostgreSQL | MySQL | SQL Server | SQLite |
|---|---|---|---|---|
| Rename a column | `RENAME COLUMN` | `RENAME COLUMN` in modern versions | `sp_rename` | Supported in newer versions |
| Change a type | `ALTER COLUMN ... TYPE` | `MODIFY COLUMN` | `ALTER COLUMN` | More limited |
| Roll back many DDL statements | Usually supported | Many DDL statements commit implicitly | Depends on statement | Depends on operation |
Check the dialect before running a script
Use a short compatibility checklist:
- Identify the database product and version.
- Check the syntax for
ADD,ALTER,RENAME, and constraint removal. - Check whether the operation supports a transaction.
- Test the script against a copy of the schema and representative data.
- Confirm how locks and existing values affect the change.
The safest migration is specific to one dialect, tested on that dialect, and recorded with the database version it supports.
Exercise
You need to change a column type in PostgreSQL, MySQL, and SQL Server. Why should you not run one shared ALTER TABLE statement on all three systems?
Show answer
The systems use different clauses: PostgreSQL uses ALTER COLUMN ... TYPE, MySQL uses MODIFY COLUMN, and SQL Server uses ALTER COLUMN. Their conversion, locking, and transaction behavior can also differ.