Skip to content

MySQL: Allow optional constraint name after CONSTRAINT keyword#2183

Merged
iffyio merged 1 commit intoapache:mainfrom
mvzink:push-spzzrnynpouu
Feb 6, 2026
Merged

MySQL: Allow optional constraint name after CONSTRAINT keyword#2183
iffyio merged 1 commit intoapache:mainfrom
mvzink:push-spzzrnynpouu

Conversation

@mvzink
Copy link
Contributor

@mvzink mvzink commented Jan 23, 2026

MySQL allows CONSTRAINT CHECK (expr) without a name - the database will auto-generate a constraint name. This is a MySQL extension; the SQL standard requires a name after the CONSTRAINT keyword. See docs.

Previously, we required an identifier after CONSTRAINT, causing us to interpret CONSTRAINT as a column name in cases like:

CREATE TABLE t (x INT, CONSTRAINT CHECK (x > 1))

Now we check if the token after CONSTRAINT is a constraint type keyword (CHECK, PRIMARY, UNIQUE, FOREIGN) and treat the name as optional for dialects that support this. This is just MySQL as far as I know (and Generic), but we introduce a new Dialect flag even though it's a minor feature. We could remove the flag and allow the more permissive optional name syntax across dialects if desired.

@mvzink mvzink force-pushed the push-spzzrnynpouu branch from 902c307 to 76574e4 Compare January 26, 2026 17:29
MySQL allows `CONSTRAINT CHECK (expr)` without a name - the database will
auto-generate a constraint name. This is a MySQL extension; the SQL standard
requires a name after the CONSTRAINT keyword. See [docs].

Previously, we required an identifier after CONSTRAINT, causing us to
interpret CONSTRAINT as a column name in cases like:

```sql
CREATE TABLE t (x INT, CONSTRAINT CHECK (x > 1))
```

Now we check if the token after CONSTRAINT is a constraint type keyword
(CHECK, PRIMARY, UNIQUE, FOREIGN) and treat the name as optional for
dialects that support this. This is just MySQL as far as I know (and
Generic), but we introduce a new `Dialect` flag even though it's a minor
feature. We could remove the flag and allow the more permissive optional
name syntax across dialects if desired.

[docs]: https://dev.mysql.com/doc/refman/8.4/en/create-table.html
@mvzink mvzink force-pushed the push-spzzrnynpouu branch from 76574e4 to ef55f1f Compare February 4, 2026 18:16
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @mvzink!

@iffyio iffyio added this pull request to the merge queue Feb 6, 2026
Merged via the queue into apache:main with commit cb21477 Feb 6, 2026
10 checks passed
@mvzink mvzink deleted the push-spzzrnynpouu branch February 6, 2026 18:00
fmguerreiro pushed a commit to fmguerreiro/datafusion-sqlparser-rs that referenced this pull request Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants