Commit 73e6c5e8 authored by unknown's avatar unknown

Introduce keys in child tables corresponding to FOREIGN KEYs

Remove redundant keys in CREATE TABLE and ALTER TABLE


mysql-test/r/constraints.result:
  Remove redundant keys
mysql-test/r/create.result:
  Remove redundant keys
mysql-test/r/innodb.result:
  Remove redundant keys
mysql-test/r/range.result:
  Remove redundant keys
mysql-test/t/range.test:
  Remove redundant keys
sql/sql_class.cc:
  Equality comparison of keys (ignoring name)
sql/sql_class.h:
  Equality comparison of keys (ignoring name)
sql/sql_table.cc:
  Remove redundant keys
sql/sql_yacc.yy:
  Introduce keys in child tables corresponding to FOREIGN KEYs
parent 7f3ac5b8
...@@ -22,8 +22,6 @@ show create table t1; ...@@ -22,8 +22,6 @@ show create table t1;
Table Create Table Table Create Table
t1 CREATE TABLE `t1` ( t1 CREATE TABLE `t1` (
`a` int(11) default NULL, `a` int(11) default NULL,
UNIQUE KEY `constraint_1` (`a`), UNIQUE KEY `constraint_1` (`a`)
UNIQUE KEY `key_1` (`a`),
UNIQUE KEY `key_2` (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1; drop table t1;
...@@ -155,37 +155,7 @@ t1 CREATE TABLE `t1` ( ...@@ -155,37 +155,7 @@ t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL default '0', `a` int(11) NOT NULL default '0',
`b` int(11) default NULL, `b` int(11) default NULL,
PRIMARY KEY (`a`), PRIMARY KEY (`a`),
KEY `b` (`b`), KEY `b` (`b`)
KEY `b_2` (`b`),
KEY `b_3` (`b`),
KEY `b_4` (`b`),
KEY `b_5` (`b`),
KEY `b_6` (`b`),
KEY `b_7` (`b`),
KEY `b_8` (`b`),
KEY `b_9` (`b`),
KEY `b_10` (`b`),
KEY `b_11` (`b`),
KEY `b_12` (`b`),
KEY `b_13` (`b`),
KEY `b_14` (`b`),
KEY `b_15` (`b`),
KEY `b_16` (`b`),
KEY `b_17` (`b`),
KEY `b_18` (`b`),
KEY `b_19` (`b`),
KEY `b_20` (`b`),
KEY `b_21` (`b`),
KEY `b_22` (`b`),
KEY `b_23` (`b`),
KEY `b_24` (`b`),
KEY `b_25` (`b`),
KEY `b_26` (`b`),
KEY `b_27` (`b`),
KEY `b_28` (`b`),
KEY `b_29` (`b`),
KEY `b_30` (`b`),
KEY `b_31` (`b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1; drop table t1;
create table t1 select if(1,'1','0'), month("2002-08-02"); create table t1 select if(1,'1','0'), month("2002-08-02");
......
...@@ -371,7 +371,6 @@ t1 0 PRIMARY 2 b A # NULL NULL BTREE ...@@ -371,7 +371,6 @@ t1 0 PRIMARY 2 b A # NULL NULL BTREE
t1 0 c 1 c A # NULL NULL BTREE t1 0 c 1 c A # NULL NULL BTREE
t1 0 b 1 b A # NULL NULL BTREE t1 0 b 1 b A # NULL NULL BTREE
t1 1 a 1 a A # NULL NULL BTREE t1 1 a 1 a A # NULL NULL BTREE
t1 1 a_2 1 a A # NULL NULL BTREE
drop table t1; drop table t1;
create table t1 (col1 int not null, col2 char(4) not null, primary key(col1)); create table t1 (col1 int not null, col2 char(4) not null, primary key(col1));
alter table t1 engine=innodb; alter table t1 engine=innodb;
......
...@@ -249,18 +249,14 @@ explain select count(*) from t1 where x in (1,2); ...@@ -249,18 +249,14 @@ explain select count(*) from t1 where x in (1,2);
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range x x 5 NULL 2 Using where; Using index 1 SIMPLE t1 range x x 5 NULL 2 Using where; Using index
drop table t1; drop table t1;
CREATE TABLE t1 (key1 int(11) NOT NULL default '0', KEY i1 (key1), KEY i2 (key1)); CREATE TABLE t1 (key1 int(11) NOT NULL default '0', KEY i1 (key1));
INSERT INTO t1 VALUES (0),(0),(1),(1); INSERT INTO t1 VALUES (0),(0),(1),(1);
CREATE TABLE t2 (keya int(11) NOT NULL default '0', KEY j1 (keya)); CREATE TABLE t2 (keya int(11) NOT NULL default '0', KEY j1 (keya));
INSERT INTO t2 VALUES (0),(0),(1),(1),(2),(2); INSERT INTO t2 VALUES (0),(0),(1),(1),(2),(2);
explain select * from t1, t2 where (t1.key1 <t2.keya + 1) and t2.keya=3; explain select * from t1, t2 where (t1.key1 <t2.keya + 1) and t2.keya=3;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ref j1 j1 4 const 1 Using where; Using index 1 SIMPLE t2 ref j1 j1 4 const 1 Using where; Using index
1 SIMPLE t1 ALL i1,i2 NULL NULL NULL 4 Range checked for each record (index map: 0x3) 1 SIMPLE t1 ALL i1 NULL NULL NULL 4 Range checked for each record (index map: 0x1)
explain select * from t1 force index(i2), t2 where (t1.key1 <t2.keya + 1) and t2.keya=3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ref j1 j1 4 const 1 Using where; Using index
1 SIMPLE t1 ALL i2 NULL NULL NULL 4 Range checked for each record (index map: 0x2)
DROP TABLE t1,t2; DROP TABLE t1,t2;
CREATE TABLE t1 ( CREATE TABLE t1 (
a int(11) default NULL, a int(11) default NULL,
......
...@@ -197,12 +197,11 @@ drop table t1; ...@@ -197,12 +197,11 @@ drop table t1;
# #
# bug #1172 # bug #1172
# #
CREATE TABLE t1 (key1 int(11) NOT NULL default '0', KEY i1 (key1), KEY i2 (key1)); CREATE TABLE t1 (key1 int(11) NOT NULL default '0', KEY i1 (key1));
INSERT INTO t1 VALUES (0),(0),(1),(1); INSERT INTO t1 VALUES (0),(0),(1),(1);
CREATE TABLE t2 (keya int(11) NOT NULL default '0', KEY j1 (keya)); CREATE TABLE t2 (keya int(11) NOT NULL default '0', KEY j1 (keya));
INSERT INTO t2 VALUES (0),(0),(1),(1),(2),(2); INSERT INTO t2 VALUES (0),(0),(1),(1),(2),(2);
explain select * from t1, t2 where (t1.key1 <t2.keya + 1) and t2.keya=3; explain select * from t1, t2 where (t1.key1 <t2.keya + 1) and t2.keya=3;
explain select * from t1 force index(i2), t2 where (t1.key1 <t2.keya + 1) and t2.keya=3;
DROP TABLE t1,t2; DROP TABLE t1,t2;
# #
......
...@@ -79,6 +79,34 @@ extern "C" void free_user_var(user_var_entry *entry) ...@@ -79,6 +79,34 @@ extern "C" void free_user_var(user_var_entry *entry)
} }
bool key_part_spec::operator==(const key_part_spec& other) const
{
return length == other.length && !strcmp(field_name, other.field_name);
}
/* Equality comparison of keys (ignoring name) */
bool Key::operator==(Key& other)
{
if (type == other.type &&
algorithm == other.algorithm &&
columns.elements == other.columns.elements)
{
List_iterator<key_part_spec> col_it1(columns);
List_iterator<key_part_spec> col_it2(other.columns);
const key_part_spec *col1, *col2;
while ((col1 = col_it1++))
{
col2 = col_it2++;
DBUG_ASSERT(col2 != NULL);
if (!(*col1 == *col2))
return false;
}
return true;
}
return false;
}
/**************************************************************************** /****************************************************************************
** Thread specific functions ** Thread specific functions
****************************************************************************/ ****************************************************************************/
......
...@@ -210,6 +210,7 @@ class key_part_spec :public Sql_alloc { ...@@ -210,6 +210,7 @@ class key_part_spec :public Sql_alloc {
const char *field_name; const char *field_name;
uint length; uint length;
key_part_spec(const char *name,uint len=0) :field_name(name), length(len) {} key_part_spec(const char *name,uint len=0) :field_name(name), length(len) {}
bool operator==(const key_part_spec& other) const;
}; };
...@@ -245,6 +246,8 @@ class Key :public Sql_alloc { ...@@ -245,6 +246,8 @@ class Key :public Sql_alloc {
:type(type_par), algorithm(alg_par), columns(cols), name(name_arg) :type(type_par), algorithm(alg_par), columns(cols), name(name_arg)
{} {}
~Key() {} ~Key() {}
/* Equality comparison of keys (ignoring name) */
bool operator==(Key& other);
}; };
class Table_ident; class Table_ident;
......
...@@ -639,12 +639,13 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, ...@@ -639,12 +639,13 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info,
/* Create keys */ /* Create keys */
List_iterator<Key> key_iterator(keys); List_iterator<Key> key_iterator(keys), key_iterator2(keys);
uint key_parts=0, fk_key_count=0; uint key_parts=0, fk_key_count=0;
List<Key> keys_in_order; // Add new keys here
bool primary_key=0,unique_key=0; bool primary_key=0,unique_key=0;
Key *key; Key *key, *key2;
uint tmp, key_number; uint tmp, key_number;
/* special marker for keys to be ignored */
static char ignore_key[1];
/* Calculate number of key segements */ /* Calculate number of key segements */
*key_count= 0; *key_count= 0;
...@@ -677,7 +678,21 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, ...@@ -677,7 +678,21 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info,
my_error(ER_TOO_LONG_IDENT, MYF(0), key->name); my_error(ER_TOO_LONG_IDENT, MYF(0), key->name);
DBUG_RETURN(-1); DBUG_RETURN(-1);
} }
key_iterator2.rewind ();
while ((key2 = key_iterator2++) != key)
{
if (*key == *key2)
{
/* TO DO: issue warning message */
/* mark that the key should be ignored */
key->name=ignore_key;
break;
}
}
if (key->name != ignore_key)
key_parts+=key->columns.elements; key_parts+=key->columns.elements;
else
(*key_count)--;
if (key->name && !tmp_table && if (key->name && !tmp_table &&
!my_strcasecmp(system_charset_info,key->name,primary_key_name)) !my_strcasecmp(system_charset_info,key->name,primary_key_name))
{ {
...@@ -704,6 +719,16 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, ...@@ -704,6 +719,16 @@ int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info,
uint key_length=0; uint key_length=0;
key_part_spec *column; key_part_spec *column;
if (key->name == ignore_key)
{
/* ignore redundant keys */
do
key=key_iterator++;
while (key && key->name == ignore_key);
if (!key)
break;
}
switch(key->type){ switch(key->type){
case Key::MULTIPLE: case Key::MULTIPLE:
key_info->flags = 0; key_info->flags = 0;
......
...@@ -1200,12 +1200,14 @@ key_def: ...@@ -1200,12 +1200,14 @@ key_def:
| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references | opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
{ {
LEX *lex=Lex; LEX *lex=Lex;
lex->key_list.push_back(new foreign_key($4, lex->col_list, lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
$8, $8,
lex->ref_list, lex->ref_list,
lex->fk_delete_opt, lex->fk_delete_opt,
lex->fk_update_opt, lex->fk_update_opt,
lex->fk_match_option)); lex->fk_match_option));
lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4:$1,
HA_KEY_ALG_UNDEF, lex->col_list));
lex->col_list.empty(); /* Alloced by sql_alloc */ lex->col_list.empty(); /* Alloced by sql_alloc */
} }
| opt_constraint check_constraint | opt_constraint check_constraint
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment