diff --git a/mysql-test/r/constraints.result b/mysql-test/r/constraints.result index 3b41e291e0f..7cd8053c21b 100644 --- a/mysql-test/r/constraints.result +++ b/mysql-test/r/constraints.result @@ -14,3 +14,16 @@ drop table t1; create table t1 (a int null); insert into t1 values (1),(NULL); drop table t1; +create table t1 (a int null); +alter table t1 add constraint constraint_1 unique (a); +alter table t1 add constraint unique key_1(a); +alter table t1 add constraint constraint_2 unique key_2(a); +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) default NULL, + UNIQUE KEY `constraint_1` (`a`), + UNIQUE KEY `key_1` (`a`), + UNIQUE KEY `key_2` (`a`) +) TYPE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; diff --git a/mysql-test/t/constraints.test b/mysql-test/t/constraints.test index cc796e0abd2..dbc34a0dff1 100644 --- a/mysql-test/t/constraints.test +++ b/mysql-test/t/constraints.test @@ -21,3 +21,9 @@ drop table t1; create table t1 (a int null); insert into t1 values (1),(NULL); drop table t1; +create table t1 (a int null); +alter table t1 add constraint constraint_1 unique (a); +alter table t1 add constraint unique key_1(a); +alter table t1 add constraint constraint_2 unique key_2(a); +show create table t1; +drop table t1; diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 805dc9f1932..8fe69ad6ae3 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -598,7 +598,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); %type remember_name remember_end opt_ident opt_db text_or_password - opt_escape + opt_escape opt_constraint %type text_string opt_gconcat_separator @@ -631,7 +631,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); expr_list udf_expr_list when_list ident_list ident_list_arg %type - key_type opt_unique_or_fulltext + key_type opt_unique_or_fulltext constraint_key_type %type key_alg opt_btree_or_rtree @@ -1189,6 +1189,13 @@ key_def: lex->key_list.push_back(new Key($1,$2, $3, lex->col_list)); lex->col_list.empty(); /* Alloced by sql_alloc */ } + | opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')' + { + LEX *lex=Lex; + const char *key_name= $3 ? $3:$1; + lex->key_list.push_back(new Key($2, key_name, $4, lex->col_list)); + lex->col_list.empty(); /* Alloced by sql_alloc */ + } | opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references { LEX *lex=Lex; @@ -1212,8 +1219,8 @@ check_constraint: ; opt_constraint: - /* empty */ - | CONSTRAINT opt_ident; + /* empty */ { $$=(char*) 0; } + | CONSTRAINT opt_ident { $$=$2; }; field_spec: field_ident @@ -1575,14 +1582,16 @@ delete_option: | SET DEFAULT { $$= (int) foreign_key::FK_OPTION_DEFAULT; }; key_type: - opt_constraint PRIMARY_SYM KEY_SYM { $$= Key::PRIMARY; } - | key_or_index { $$= Key::MULTIPLE; } + key_or_index { $$= Key::MULTIPLE; } | FULLTEXT_SYM { $$= Key::FULLTEXT; } | FULLTEXT_SYM key_or_index { $$= Key::FULLTEXT; } | SPATIAL_SYM { $$= Key::SPATIAL; } - | SPATIAL_SYM key_or_index { $$= Key::SPATIAL; } - | opt_constraint UNIQUE_SYM { $$= Key::UNIQUE; } - | opt_constraint UNIQUE_SYM key_or_index { $$= Key::UNIQUE; }; + | SPATIAL_SYM key_or_index { $$= Key::SPATIAL; }; + +constraint_key_type: + PRIMARY_SYM KEY_SYM { $$= Key::PRIMARY; } + | UNIQUE_SYM { $$= Key::UNIQUE; } + | UNIQUE_SYM key_or_index { $$= Key::UNIQUE; }; key_or_index: KEY_SYM {}