Sub topic 16
Differences Between Database Systems
Assume you already know SELECT, INSERT, UPDATE, and DELETE, and understand tables, rows, columns, and common data types. This chapter focuses on PostgreSQL, MySQL, SQL Server, and SQLite.
SQL defines common concepts, but each database system, or dialect, implements some features differently. Check the target system before copying a CREATE TABLE statement between projects.
Identity columns and generated keys
An identity column generates a value when a row is inserted without an explicit key. The concept is portable, but the syntax is not.
| Database system | Typical syntax |
|---|---|
| PostgreSQL | `GENERATED ALWAYS AS IDENTITY` |
| MySQL | `AUTO_INCREMENT` |
| SQL Server | `IDENTITY(1,1)` |
| SQLite | `INTEGER PRIMARY KEY` |
For example:
-- PostgreSQL
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE
);
-- MySQL
CREATE TABLE customers (
customer_id INTEGER AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE
);
-- SQL Server
CREATE TABLE customers (
customer_id INTEGER IDENTITY(1,1) PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE
);
-- SQLite
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE
);SQLite's special behavior applies when the column is exactly INTEGER PRIMARY KEY. It automatically uses row identifiers. AUTOINCREMENT is a separate SQLite keyword with additional behavior and is usually unnecessary.
`CHECK` constraints
A CHECK constraint rejects a row when its condition evaluates to false. Support differs by version.
- PostgreSQL enforces
CHECKconstraints. - SQL Server enforces them.
- SQLite enforces them.
- MySQL versions before 8.0.16 accepted
CHECKsyntax but generally did not enforce it. MySQL 8.0.16 and later enforce it.
For example:
CREATE TABLE payments (
payment_id INTEGER PRIMARY KEY,
amount DECIMAL(10, 2) NOT NULL CHECK (amount >= 0)
);A CHECK condition should describe the current row. Do not assume that it can query another table. Database systems generally restrict subqueries inside CHECK constraints. Use triggers or application logic for rules that depend on other rows.
Foreign-key enforcement
Foreign keys require a referenced row to exist and prevent invalid relationships. PostgreSQL, MySQL with InnoDB tables, and SQL Server enforce foreign keys by default in ordinary configurations.
SQLite supports foreign keys, but enforcement must be enabled for each connection:
PRAGMA foreign_keys = ON;
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
FOREIGN KEY (customer_id)
REFERENCES customers (customer_id)
ON DELETE RESTRICT
);If the application opens another SQLite connection without executing the PRAGMA, that connection may accept invalid foreign-key data. Put the setting in connection setup and test it.
A practical portability check
Before deployment, record the database product, version, storage engine where relevant, and connection settings. Then run migration tests against that same combination.
Exercise
A schema uses GENERATED ALWAYS AS IDENTITY and a foreign key. It runs on PostgreSQL but fails to protect relationships in one SQLite test. What should you check?
Show answer
Replace the identity syntax with SQLite-compatible syntax, execute PRAGMA foreign_keys = ON for every test connection, and insert an invalid foreign-key value to verify enforcement.