Skip to content

Commit 02f382e

Browse files
author
hf@deer.(none)
committed
Fix for #1189 (Mess with names about CONSTRAINT)
Second edition: error message was deleted as Segey suggested Now name of the constraint will be used as the name of the key if the last not specified
1 parent 05e4d90 commit 02f382e

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

mysql-test/r/constraints.result

+13
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ drop table t1;
1414
create table t1 (a int null);
1515
insert into t1 values (1),(NULL);
1616
drop table t1;
17+
create table t1 (a int null);
18+
alter table t1 add constraint constraint_1 unique (a);
19+
alter table t1 add constraint unique key_1(a);
20+
alter table t1 add constraint constraint_2 unique key_2(a);
21+
show create table t1;
22+
Table Create Table
23+
t1 CREATE TABLE `t1` (
24+
`a` int(11) default NULL,
25+
UNIQUE KEY `constraint_1` (`a`),
26+
UNIQUE KEY `key_1` (`a`),
27+
UNIQUE KEY `key_2` (`a`)
28+
) TYPE=MyISAM DEFAULT CHARSET=latin1
29+
drop table t1;

mysql-test/t/constraints.test

+6
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ drop table t1;
2121
create table t1 (a int null);
2222
insert into t1 values (1),(NULL);
2323
drop table t1;
24+
create table t1 (a int null);
25+
alter table t1 add constraint constraint_1 unique (a);
26+
alter table t1 add constraint unique key_1(a);
27+
alter table t1 add constraint constraint_2 unique key_2(a);
28+
show create table t1;
29+
drop table t1;

sql/sql_yacc.yy

+18-9
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize);
598598

599599
%type <simple_string>
600600
remember_name remember_end opt_ident opt_db text_or_password
601-
opt_escape
601+
opt_escape opt_constraint
602602

603603
%type <string>
604604
text_string opt_gconcat_separator
@@ -631,7 +631,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize);
631631
expr_list udf_expr_list when_list ident_list ident_list_arg
632632

633633
%type <key_type>
634-
key_type opt_unique_or_fulltext
634+
key_type opt_unique_or_fulltext constraint_key_type
635635

636636
%type <key_alg>
637637
key_alg opt_btree_or_rtree
@@ -1189,6 +1189,13 @@ key_def:
11891189
lex->key_list.push_back(new Key($1,$2, $3, lex->col_list));
11901190
lex->col_list.empty(); /* Alloced by sql_alloc */
11911191
}
1192+
| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
1193+
{
1194+
LEX *lex=Lex;
1195+
const char *key_name= $3 ? $3:$1;
1196+
lex->key_list.push_back(new Key($2, key_name, $4, lex->col_list));
1197+
lex->col_list.empty(); /* Alloced by sql_alloc */
1198+
}
11921199
| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
11931200
{
11941201
LEX *lex=Lex;
@@ -1212,8 +1219,8 @@ check_constraint:
12121219
;
12131220

12141221
opt_constraint:
1215-
/* empty */
1216-
| CONSTRAINT opt_ident;
1222+
/* empty */ { $$=(char*) 0; }
1223+
| CONSTRAINT opt_ident { $$=$2; };
12171224

12181225
field_spec:
12191226
field_ident
@@ -1575,14 +1582,16 @@ delete_option:
15751582
| SET DEFAULT { $$= (int) foreign_key::FK_OPTION_DEFAULT; };
15761583

15771584
key_type:
1578-
opt_constraint PRIMARY_SYM KEY_SYM { $$= Key::PRIMARY; }
1579-
| key_or_index { $$= Key::MULTIPLE; }
1585+
key_or_index { $$= Key::MULTIPLE; }
15801586
| FULLTEXT_SYM { $$= Key::FULLTEXT; }
15811587
| FULLTEXT_SYM key_or_index { $$= Key::FULLTEXT; }
15821588
| SPATIAL_SYM { $$= Key::SPATIAL; }
1583-
| SPATIAL_SYM key_or_index { $$= Key::SPATIAL; }
1584-
| opt_constraint UNIQUE_SYM { $$= Key::UNIQUE; }
1585-
| opt_constraint UNIQUE_SYM key_or_index { $$= Key::UNIQUE; };
1589+
| SPATIAL_SYM key_or_index { $$= Key::SPATIAL; };
1590+
1591+
constraint_key_type:
1592+
PRIMARY_SYM KEY_SYM { $$= Key::PRIMARY; }
1593+
| UNIQUE_SYM { $$= Key::UNIQUE; }
1594+
| UNIQUE_SYM key_or_index { $$= Key::UNIQUE; };
15861595

15871596
key_or_index:
15881597
KEY_SYM {}

0 commit comments

Comments
 (0)