From df75250ac7657f38857043ddab3edf4d56abcaca Mon Sep 17 00:00:00 2001 From: Michael Victor Zink Date: Wed, 25 Feb 2026 13:00:48 -0800 Subject: [PATCH] Support MySQL KEY keyword in column definitions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `supports_key_column_option()` dialect trait method to enable MySQL-specific KEY syntax in column-level constraints: - `UNIQUE [KEY]` — optional KEY after UNIQUE - `[PRIMARY] KEY` — standalone KEY as shorthand for PRIMARY KEY --- src/ast/ddl.rs | 2 +- src/dialect/generic.rs | 4 ++++ src/dialect/mod.rs | 12 ++++++++++++ src/dialect/mysql.rs | 5 +++++ src/parser/mod.rs | 23 ++++++++++++++++++++++- tests/sqlparser_mysql.rs | 9 +++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 0c4f93e647..3a951f66b5 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -2040,7 +2040,7 @@ impl fmt::Display for ColumnOption { Ok(()) } Unique(constraint) => { - write!(f, "UNIQUE")?; + write!(f, "UNIQUE{:>}", constraint.index_type_display)?; if let Some(characteristics) = &constraint.characteristics { write!(f, " {characteristics}")?; } diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index 1cf195e637..a7a3c2715a 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -280,4 +280,8 @@ impl Dialect for GenericDialect { fn supports_constraint_keyword_without_name(&self) -> bool { true } + + fn supports_key_column_option(&self) -> bool { + true + } } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index bcca455eca..08b41a7409 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1195,6 +1195,18 @@ pub trait Dialect: Debug + Any { false } + /// Returns true if the dialect supports the `KEY` keyword as part of + /// column-level constraints in a `CREATE TABLE` statement. + /// + /// When enabled, the parser accepts these MySQL-specific column options: + /// - `UNIQUE [KEY]` — optional `KEY` after `UNIQUE` + /// - `[PRIMARY] KEY` — standalone `KEY` as shorthand for `PRIMARY KEY` + /// + /// + fn supports_key_column_option(&self) -> bool { + false + } + /// Returns true if the specified keyword is reserved and cannot be /// used as an identifier without special handling like quoting. fn is_reserved_for_identifier(&self, kw: Keyword) -> bool { diff --git a/src/dialect/mysql.rs b/src/dialect/mysql.rs index bdced4826b..6b057539e5 100644 --- a/src/dialect/mysql.rs +++ b/src/dialect/mysql.rs @@ -206,6 +206,11 @@ impl Dialect for MySqlDialect { fn supports_constraint_keyword_without_name(&self) -> bool { true } + + /// See: + fn supports_key_column_option(&self) -> bool { + true + } } /// `LOCK TABLES` diff --git a/src/parser/mod.rs b/src/parser/mod.rs index bb11d79c2d..b0f6b4213f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9048,12 +9048,18 @@ impl<'a> Parser<'a> { .into(), )) } else if self.parse_keyword(Keyword::UNIQUE) { + let index_type_display = + if self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY) { + KeyOrIndexDisplay::Key + } else { + KeyOrIndexDisplay::None + }; let characteristics = self.parse_constraint_characteristics()?; Ok(Some( UniqueConstraint { name: None, index_name: None, - index_type_display: KeyOrIndexDisplay::None, + index_type_display, index_type: None, columns: vec![], index_options: vec![], @@ -9062,6 +9068,21 @@ impl<'a> Parser<'a> { } .into(), )) + } else if self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY) { + // In MySQL, `KEY` in a column definition is shorthand for `PRIMARY KEY`. + // See: https://dev.mysql.com/doc/refman/8.4/en/create-table.html + let characteristics = self.parse_constraint_characteristics()?; + Ok(Some( + PrimaryKeyConstraint { + name: None, + index_name: None, + index_type: None, + columns: vec![], + index_options: vec![], + characteristics, + } + .into(), + )) } else if self.parse_keyword(Keyword::REFERENCES) { let foreign_table = self.parse_object_name(false)?; // PostgreSQL allows omitting the column list and diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 30405623dc..b4ae764c2e 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -944,6 +944,15 @@ fn parse_create_table_primary_and_unique_key_characteristic_test() { } } +#[test] +fn parse_create_table_column_key_options() { + mysql_and_generic().verified_stmt("CREATE TABLE foo (x INT UNIQUE KEY)"); + mysql_and_generic().one_statement_parses_to( + "CREATE TABLE foo (x INT KEY)", + "CREATE TABLE foo (x INT PRIMARY KEY)", + ); +} + #[test] fn parse_create_table_comment() { let without_equal = "CREATE TABLE foo (bar INT) COMMENT 'baz'";