diff --git a/mysql-test/r/alter_table_trans.result b/mysql-test/r/alter_table_trans.result index 67ae4075d57..435f5abd9d0 100644 --- a/mysql-test/r/alter_table_trans.result +++ b/mysql-test/r/alter_table_trans.result @@ -16,3 +16,11 @@ insert t1 values (repeat('3', 8193),3,1,1); ALTER TABLE t1 ADD PRIMARY KEY (col4(10)) , ADD UNIQUE KEY uidx (col3); ERROR 23000: Duplicate entry '1' for key 'uidx' DROP TABLE t1; +CREATE TABLE t1 (a INT) ENGINE = InnoDB; +INSERT INTO t1 VALUES (2); +ALTER TABLE t1 ADD PRIMARY KEY (a); +ALTER TABLE t1 DROP PRIMARY KEY; +INSERT INTO t1 VALUES (2); +ALTER TABLE t1 ADD PRIMARY KEY (a); +ERROR 23000: Duplicate entry '2' for key 'PRIMARY' +DROP TABLE t1; diff --git a/mysql-test/t/alter_table_trans.test b/mysql-test/t/alter_table_trans.test index 29b9b4c212f..19b40d84b22 100644 --- a/mysql-test/t/alter_table_trans.test +++ b/mysql-test/t/alter_table_trans.test @@ -32,4 +32,16 @@ insert t1 values (repeat('3', 8193),3,1,1); ALTER TABLE t1 ADD PRIMARY KEY (col4(10)) , ADD UNIQUE KEY uidx (col3); DROP TABLE t1; +# +# MDEV-5743 Server crashes in mysql_alter_table on an attempt to add a primary key to InnoDB table +# + +CREATE TABLE t1 (a INT) ENGINE = InnoDB; +INSERT INTO t1 VALUES (2); +ALTER TABLE t1 ADD PRIMARY KEY (a); +ALTER TABLE t1 DROP PRIMARY KEY; +INSERT INTO t1 VALUES (2); +--error ER_DUP_ENTRY +ALTER TABLE t1 ADD PRIMARY KEY (a); +DROP TABLE t1; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index c9f754a204f..6d8c1677166 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -6908,7 +6908,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, if (is_PK(key_info)) { merged[0]= key_info[0]; - if (is_PK(table->key_info)) + if (table->key_info && is_PK(table->key_info)) { old_cnt--; table->key_info++;