Skip to content

Commit

Permalink
MDEV-8212 alter table - failing to ADD PRIMARY KEY IF NOT EXISTS when…
Browse files Browse the repository at this point in the history
… existing index of same as column name.

        The default name for the primary key is rather 'PRIMARY' instead of the indexed column name.
  • Loading branch information
Alexey Botchkov committed Jun 14, 2015
1 parent fc31e31 commit 196528e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 11 additions & 0 deletions mysql-test/r/alter_table.result
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,17 @@ t2 CREATE TABLE `t2` (
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t2;
DROP TABLE t1;
CREATE TABLE t1 (
`transaction_id` int(11) NOT NULL DEFAULT '0',
KEY `transaction_id` (`transaction_id`));
ALTER TABLE t1 DROP KEY IF EXISTS transaction_id, ADD PRIMARY KEY IF NOT EXISTS (transaction_id);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`transaction_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`transaction_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
# Bug#11748057 (formerly known as 34972): ALTER TABLE statement doesn't
# identify correct column name.
#
Expand Down
8 changes: 8 additions & 0 deletions mysql-test/t/alter_table.test
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,14 @@ SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
`transaction_id` int(11) NOT NULL DEFAULT '0',
KEY `transaction_id` (`transaction_id`));
ALTER TABLE t1 DROP KEY IF EXISTS transaction_id, ADD PRIMARY KEY IF NOT EXISTS (transaction_id);
SHOW CREATE TABLE t1;

DROP TABLE t1;

--echo # Bug#11748057 (formerly known as 34972): ALTER TABLE statement doesn't
--echo # identify correct column name.
--echo #
Expand Down
17 changes: 11 additions & 6 deletions sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5859,12 +5859,17 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info)
/* let us check the name of the first key part. */
if ((keyname= key->name.str) == NULL)
{
List_iterator<Key_part_spec> part_it(key->columns);
Key_part_spec *kp;
if ((kp= part_it++))
keyname= kp->field_name.str;
if (keyname == NULL)
continue;
if (key->type == Key::PRIMARY)
keyname= primary_key_name;
else
{
List_iterator<Key_part_spec> part_it(key->columns);
Key_part_spec *kp;
if ((kp= part_it++))
keyname= kp->field_name.str;
if (keyname == NULL)
continue;
}
}
if (key->type != Key::FOREIGN_KEY)
{
Expand Down

0 comments on commit 196528e

Please sign in to comment.