Skip to content

Commit 27c3ec8

Browse files
authored
Support ALTER TABLE DROP PRIMARY KEY (#682)
* parse alter table drop primary key * cargo nightly fmt * add Dialect validation
1 parent 1b3778e commit 27c3ec8

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/ast/ddl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub enum AlterTableOperation {
4444
if_exists: bool,
4545
cascade: bool,
4646
},
47+
/// `DROP PRIMARY KEY`
48+
///
49+
/// Note: this is a MySQL-specific operation.
50+
DropPrimaryKey,
4751
/// `RENAME TO PARTITION (partition=val)`
4852
RenamePartitions {
4953
old_partitions: Vec<Expr>,
@@ -124,6 +128,7 @@ impl fmt::Display for AlterTableOperation {
124128
if *cascade { " CASCADE" } else { "" },
125129
)
126130
}
131+
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
127132
AlterTableOperation::DropColumn {
128133
column_name,
129134
if_exists,

src/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,6 +3137,10 @@ impl<'a> Parser<'a> {
31373137
name,
31383138
cascade,
31393139
}
3140+
} else if self.parse_keywords(&[Keyword::PRIMARY, Keyword::KEY])
3141+
&& dialect_of!(self is MySqlDialect | GenericDialect)
3142+
{
3143+
AlterTableOperation::DropPrimaryKey
31403144
} else {
31413145
let _ = self.parse_keyword(Keyword::COLUMN);
31423146
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);

tests/sqlparser_mysql.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,19 @@ fn parse_update_with_joins() {
875875
}
876876
}
877877

878+
#[test]
879+
fn parse_alter_table_drop_primary_key() {
880+
match mysql_and_generic().verified_stmt("ALTER TABLE tab DROP PRIMARY KEY") {
881+
Statement::AlterTable {
882+
name,
883+
operation: AlterTableOperation::DropPrimaryKey,
884+
} => {
885+
assert_eq!("tab", name.to_string());
886+
}
887+
_ => unreachable!(),
888+
}
889+
}
890+
878891
#[test]
879892
fn parse_alter_table_change_column() {
880893
let expected_name = ObjectName(vec![Ident::new("orders")]);

0 commit comments

Comments
 (0)