Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,8 @@ impl Dialect for GenericDialect {
fn supports_comment_optimizer_hint(&self) -> bool {
true
}

fn supports_constraint_keyword_without_name(&self) -> bool {
true
}
}
17 changes: 17 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,23 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `CONSTRAINT` keyword without a name
/// in table constraint definitions.
///
/// Example:
/// ```sql
/// CREATE TABLE t (a INT, CONSTRAINT CHECK (a > 0))
/// ```
///
/// This is a MySQL extension; the SQL standard requires a name after `CONSTRAINT`.
/// When the name is omitted, the output normalizes to just the constraint type
/// without the `CONSTRAINT` keyword (e.g., `CHECK (a > 0)`).
///
/// <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
fn supports_constraint_keyword_without_name(&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 {
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ impl Dialect for MySqlDialect {
fn supports_comment_optimizer_hint(&self) -> bool {
true
}

/// See: <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
fn supports_constraint_keyword_without_name(&self) -> bool {
true
}
}

/// `LOCK TABLES`
Expand Down
15 changes: 14 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9243,7 +9243,20 @@ impl<'a> Parser<'a> {
&mut self,
) -> Result<Option<TableConstraint>, ParserError> {
let name = if self.parse_keyword(Keyword::CONSTRAINT) {
Some(self.parse_identifier()?)
if self.dialect.supports_constraint_keyword_without_name()
&& self
.peek_one_of_keywords(&[
Keyword::CHECK,
Keyword::PRIMARY,
Keyword::UNIQUE,
Keyword::FOREIGN,
])
.is_some()
{
None
} else {
Some(self.parse_identifier()?)
}
} else {
None
};
Expand Down
21 changes: 21 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,27 @@ fn parse_create_table_unallow_constraint_then_index() {
assert!(mysql_and_generic().parse_sql_statements(sql).is_ok());
}

#[test]
fn parse_create_table_constraint_check_without_name() {
let dialects = all_dialects_where(|d| d.supports_constraint_keyword_without_name());
dialects.one_statement_parses_to(
"CREATE TABLE t (x INT, CONSTRAINT PRIMARY KEY (x))",
"CREATE TABLE t (x INT, PRIMARY KEY (x))",
);
dialects.one_statement_parses_to(
"CREATE TABLE t (x INT, CONSTRAINT UNIQUE (x))",
"CREATE TABLE t (x INT, UNIQUE (x))",
);
dialects.one_statement_parses_to(
"CREATE TABLE t (x INT, CONSTRAINT FOREIGN KEY (x) REFERENCES t2(id))",
"CREATE TABLE t (x INT, FOREIGN KEY (x) REFERENCES t2(id))",
);
dialects.one_statement_parses_to(
"CREATE TABLE t (x INT, CONSTRAINT CHECK (x > 1))",
"CREATE TABLE t (x INT, CHECK (x > 1))",
);
}

#[test]
fn parse_create_table_with_fulltext_definition() {
mysql_and_generic().verified_stmt("CREATE TABLE tb (id INT, FULLTEXT (id))");
Expand Down
Loading