diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index 4cb57a43cf3..d9eccbcf27e 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -468,3 +468,44 @@ NULL select database(); database() NULL +use test; +create table t1 (a int, index `primary` (a)); +ERROR 42000: Incorrect index name 'primary' +create table t1 (a int, index `PRIMARY` (a)); +ERROR 42000: Incorrect index name 'PRIMARY' +create table t1 (`primary` int, index(`primary`)); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `primary` int(11) default NULL, + KEY `primary_2` (`primary`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create table t2 (`PRIMARY` int, index(`PRIMARY`)); +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `PRIMARY` int(11) default NULL, + KEY `PRIMARY_2` (`PRIMARY`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create table t3 (a int); +alter table t3 add index `primary` (a); +ERROR 42000: Incorrect index name 'primary' +alter table t3 add index `PRIMARY` (a); +ERROR 42000: Incorrect index name 'PRIMARY' +create table t4 (`primary` int); +alter table t4 add index(`primary`); +show create table t4; +Table Create Table +t4 CREATE TABLE `t4` ( + `primary` int(11) default NULL, + KEY `primary_2` (`primary`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +create table t5 (`PRIMARY` int); +alter table t5 add index(`PRIMARY`); +show create table t5; +Table Create Table +t5 CREATE TABLE `t5` ( + `PRIMARY` int(11) default NULL, + KEY `PRIMARY_2` (`PRIMARY`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t2, t3, t4, t5; diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test index 70fa4173c76..fedd82356f9 100644 --- a/mysql-test/t/create.test +++ b/mysql-test/t/create.test @@ -359,3 +359,33 @@ select database(); # Connect without a database connect (user4,localhost,mysqltest_1,,*NO-ONE*); select database(); + +# +# Test for Bug 856 'Naming a key "Primary" causes trouble' +# + +use test; +--error 1280 +create table t1 (a int, index `primary` (a)); +--error 1280 +create table t1 (a int, index `PRIMARY` (a)); + +create table t1 (`primary` int, index(`primary`)); +show create table t1; +create table t2 (`PRIMARY` int, index(`PRIMARY`)); +show create table t2; + +create table t3 (a int); +--error 1280 +alter table t3 add index `primary` (a); +--error 1280 +alter table t3 add index `PRIMARY` (a); + +create table t4 (`primary` int); +alter table t4 add index(`primary`); +show create table t4; +create table t5 (`PRIMARY` int); +alter table t5 add index(`PRIMARY`); +show create table t5; + +drop table t1, t2, t3, t4, t5; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index ee11b7b9da0..200511481b1 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -639,6 +639,12 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, DBUG_RETURN(-1); } key_parts+=key->columns.elements; + if (key->name && !tmp_table && + !my_strcasecmp(system_charset_info,key->name,primary_key_name)) + { + my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name); + DBUG_RETURN(-1); + } } tmp=min(file->max_keys(), MAX_KEY); if (key_count > tmp) @@ -1079,7 +1085,8 @@ make_unique_key_name(const char *field_name,KEY *start,KEY *end) { char buff[MAX_FIELD_NAME],*buff_end; - if (!check_if_keyname_exists(field_name,start,end)) + if (!check_if_keyname_exists(field_name,start,end) && + my_strcasecmp(system_charset_info,field_name,primary_key_name)) return (char*) field_name; // Use fieldname buff_end=strmake(buff,field_name,MAX_FIELD_NAME-4); for (uint i=2 ; ; i++) @@ -2403,6 +2410,12 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, { if (key->type != Key::FOREIGN_KEY) key_list.push_back(key); + if (key->name && + !my_strcasecmp(system_charset_info,key->name,primary_key_name)) + { + my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name); + DBUG_RETURN(-1); + } } }