# SQL and type compatibility

> Compare write results, type ranges, collation, transaction visibility and constraints.

---

LLMS index: [llms.txt](/llms.txt)

---

The examples on this page use an ordinary MySQL connection to a scratch schema. The native PostgreSQL connection sees the same stored rows. Parser rules, transaction boundaries and driver result types depend on the entry point.

## SQL at the two endpoints {#dialect}

| Task | MySQL behavior in 19.0.1 | Migration consequence |
|---|---|---|
| Create and alter tables | Backticks, inline `KEY`, `AUTO_INCREMENT`, `MODIFY` and `CHANGE` are handled by the dialect. Auto-increment uses a PostgreSQL identity sequence. | Compare the next generated value after loading explicit keys. A change to a default or generation expression deserves a write test. |
| Query and join | Backticks, `LIMIT offset,count`, single-target `UPDATE … JOIN` and selected `DELETE FROM target USING …` forms are implemented. | Rewrite multi-target writes and `DELETE target FROM … JOIN` as explicit single-target operations. Compare affected rows inside the intended transaction. |
| Update a duplicate key | `ON DUPLICATE KEY UPDATE` checks unique keys. `REPLACE` deletes conflicting rows and inserts a new row using column defaults. FK actions and triggers can run. | Use an explicit update when omitted columns or referencing rows must survive. Plain `UPDATE` counts matched rows, including unchanged rows. |
| Use a boolean or integer predicate | `SELECT 1 WHERE 1` returns `1`; inserting `1=1` into an `INT` column stores `1`. Native `psql` rejects the integer predicate (SQLSTATE 42804). | Write `WHERE flag <> 0` in portable SQL. Native integer/boolean conversions require explicit casts. Some extension-provided casts and types are visible database-wide. |
| Inspect or replay definitions | `SHOW CREATE TABLE`, `SHOW COLUMNS` and `SHOW INDEX` expose MySQL-facing definitions. `information_schema`, `SHOW STATUS` and system variables cover a subset. | Replay the DDL in a scratch schema. Compare constraints and physical storage via native catalogs. |

This example returned affected-row counts `1, 1, 2, 2` for the four writes. The final row was `(1,9)`: the omitted column took its default on replacement.

```sql
CREATE TABLE counters (id INT PRIMARY KEY, v INT DEFAULT 9);
INSERT INTO counters VALUES (1,7);
UPDATE counters SET v=7 WHERE id=1;
INSERT INTO counters VALUES (1,8) ON DUPLICATE KEY UPDATE v=8;
REPLACE INTO counters (id) VALUES (1);
SELECT * FROM counters;
```

## Types, bytes and collation {#semantics}

| Source value or DDL | Observed result in the example database | Representation or check |
|---|---|---|
| `CREATE TABLE ids(k BIGINT UNSIGNED)`, then `INSERT INTO ids VALUES(9223372036854775808)` | Error 1264, row count remains zero. The target domain ranges from 0 through 9223372036854775807. | For the full uint64 range, design a `DECIMAL(20,0)` column with bounds and review driver decoding, key generation and FKs. |
| `CREATE TABLE cents(n DECIMAL)`, then insert `1.6` | Reading `n` returns `2`. A bare declaration means `DECIMAL(10,0)`. | Specify precision and scale. A native unbounded numeric can be reported as `decimal(65,30)` with a warning when exported. |
| Insert `0x610062` into `TEXT`; repeat with `VARBINARY(4)` | Text rejects NUL with 1366. `HEX(binary_column)` returns `610062`. `BINARY(n)` additionally pads with zero bytes. | Use a binary column for payloads that require arbitrary bytes. Compare `HEX()` and length, including trailing zeros. |
| Insert `'27:30:00'` into `TIME`; evaluate `CAST('0000-00-00' AS DATE)` | Both return 1292. The time column represents a time of day. | Store signed durations as integer microseconds or convert explicitly to PostgreSQL `interval`. Map zero dates to a business-approved value or nullable field. |
| `CREATE TABLE e(v ENUM('a','b'))` or `CREATE TABLE s(v SET('a','b'))` | Both fail with 1091 in a fresh database. | Use checked text, a lookup table or an explicit bitmask according to membership and ordering requirements. |
| `CREATE TABLE calc(i INT, twice INT AS(i*2) VIRTUAL)`, insert `i=4` | Reading the row returns `(4,8)`. Both `VIRTUAL` and `STORED` values are materialized on write. | Use deterministic expressions, compare stored values after loading, and budget for storage and write cost. |

### Compare strings before rebuilding unique keys {#collation}

Strings are stored and returned as UTF-8. `SET NAMES` updates session character settings; other encodings can be approximated with warning 1105. Convert legacy-encoded source bytes explicitly and check the decoded strings.

The dialect recognizes `utf8_general_ci` and `utf8mb4_general_ci` as the general-ci implementation, and `utf8_bin` and `utf8mb4_bin` as the binary implementation. Both use PAD SPACE. Other MySQL names ending in `_ci` map to general-ci weights; other suffixes map to binary order. Locale-specific equivalence, ordering, uniqueness and duplicate-key arbitration can change. String literals can behave differently from columns: in the C-locale example database `SELECT 'a'='A'` returned `0`.

```sql
CREATE TABLE labels(v VARCHAR(20) COLLATE utf8mb4_unicode_ci UNIQUE);
SHOW WARNINGS;
SHOW CREATE TABLE labels;
INSERT INTO labels VALUES ('A');
INSERT INTO labels VALUES ('a');
```

The warning code was 1105, the effective definition contained `utf8mb4_general_ci`, and the last insert returned 1062. Load candidate keys into a staging table without a unique index and group by the proposed target key, including any prefix length. Resolve collisions before creating the unique index.

## Transaction boundaries and snapshots {#transactions}

With MySQL autocommit disabled, `COMMIT` and `ROLLBACK` delimit application work. DDL, a new `BEGIN`, and enabling autocommit can commit preceding writes. In a scratch schema, `BEGIN; INSERT INTO counters VALUES (2,7); CREATE TABLE boundary(i INT); ROLLBACK;` leaves row 2 and the new table. Separate schema changes from work the application expects to roll back. Native PostgreSQL transactions retain PostgreSQL transactional DDL and error semantics.

At the MySQL endpoint, read-uncommitted, read-committed and **read-write repeatable-read** use PostgreSQL statement snapshots. Read-only repeatable-read and a consistent snapshot retain a transaction snapshot. For a two-connection check, create `meter(id INT PRIMARY KEY,v INT)` with row `(1,10)`. Set A to `SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ`. Read `v` in A, let B commit `UPDATE meter SET v=20 WHERE id=1`, then read again in A. Reset to 10 between trials.

| A starts with | First read | Second read |
|---|---:|---:|
| `START TRANSACTION READ WRITE` | 10 | 20 |
| `START TRANSACTION READ ONLY` | 10 | 10 |
| `START TRANSACTION WITH CONSISTENT SNAPSHOT` | 10 | 10 |

For reports, request a stable snapshot explicitly. For concurrent writes, retry the whole transaction after 1213 / SQLSTATE 40001. Serializable uses PostgreSQL isolation. InnoDB gap locking is unavailable. `LOCK TABLES` and `UNLOCK TABLES` are accepted without taking table locks or committing, so quiesce writers when a migration needs exclusivity.

## Indexes and foreign keys {#constraints}

A normal bounded full key can use a native B-tree. A true prefix key such as `PRIMARY KEY(k(3))` enforces uniqueness on the prefix. In the round-trip test, inserting `abcZ` next to `abc1` returned 1062 after restore. True prefix and oversized indexes recheck the heap, with no index-only scan or index-provided ordering. MySQL index hints are accepted and ignored. Examine the native query plan and benchmark your actual key distribution.

A table-level `FOREIGN KEY(i) REFERENCES parent(i)` requires a type-compatible primary or unique parent key. With only `KEY(i)` on the parent, creation failed (SQLSTATE 42830). Check for duplicates before adding a unique constraint. New orphan inserts under enforcement report 1452. Re-enabling checks leaves historical orphans in place; run an anti-join after a bulk load.

`SET FOREIGN_KEY_CHECKS=0` is session-local. In this release, skipping a specific FK requires ownership rights on the **referencing (child) table** or superuser status. Otherwise the constraint and parent-side actions still apply, with a warning. SECURITY DEFINER execution keeps checks active. A pending FK created before its parent still requires proper parent access when bound. Preserve the extension's metadata triggers in native backups.

## Prepared statements {#prepared-statements}

The SQL-level example below returned `42`. Wire-level server preparation is a separate path. The Go and JDBC runs used binary parameters and checked result types, errors and subsequent connection use. [Driver results and settings](/docs/mysql/drivers/#prepared-statements)

```sql
PREPARE n FROM 'SELECT ? + 1';
SET @x = 41;
EXECUTE n USING @x;
DEALLOCATE PREPARE n;
```

## Check the application connection {#clients}

A successful CLI login establishes reachability. For supported HBA methods and TLS see [authentication](/docs/mysql/reference/authentication/). For JDBC and Go parameter modes, result types, generated IDs and reuse after a database error, see [tested clients](/docs/mysql/drivers/).
{#connections}

GTID replication and stored procedures also affect the cutover plan. [Known gaps and troubleshooting](/docs/mysql/status/)
{#gaps}

---

Backlinks:

- [MySQL compatibility as a PostgreSQL 18 extension](/blog/mysql-as-a-postgresql-18-extension/)
- [MySQL](/docs/mysql/)
- [Protocol, dialect and origins](/docs/mysql/architecture/)
- [Migration and recovery](/docs/mysql/operations/)
- [Limits and troubleshooting](/docs/mysql/status/)
