From c9185225cba8bb6a2a6745d98a168efe6dfaddcd Mon Sep 17 00:00:00 2001 From: Alexey Botchkov Date: Sun, 25 Oct 2015 22:45:48 +0400 Subject: MDEV-8358 ALTER TABLE .. ADD PRIMARY KEY IF NOT EXISTS -> ERROR 1068 (42000): Multiple primary key defined Checks for multiple primary keys added. --- sql/sql_table.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'sql/sql_table.cc') diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 5ad4ceb9c35..e0f7184b0e8 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -5846,6 +5846,16 @@ drop_create_field: { if (!key->create_if_not_exists) continue; + + /* Check if the table already has a PRIMARY KEY */ + if (key->type == Key::PRIMARY && + table->s->primary_key != MAX_KEY) + { + push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE, + ER_DUP_KEYNAME, ER(ER_MULTIPLE_PRI_KEY)); + goto remove_key_no_warn; + } + /* If the name of the key is not specified, */ /* let us check the name of the first key part. */ if ((keyname= key->name.str) == NULL) @@ -5912,6 +5922,7 @@ drop_create_field: remove_key: push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE, ER_DUP_KEYNAME, ER(ER_DUP_KEYNAME), keyname); +remove_key_no_warn: key_it.remove(); if (key->type == Key::FOREIGN_KEY) { -- cgit v1.2.1