不知道有多少人清楚的知道,在oracle中,如果一个复合索引,假定索引(a,b,c)三个字段,删除了(包括unused)其中一个字段,oracle会怎么处理这个索引。同样,如果是约束,oracle又怎么处理? 用oracle为例子,我又拿mysql做了一个对比,看看mysql是怎么处理这个
不知道有多少人清楚的知道,在oracle中,如果一个复合索引,假定索引(a,b,c)三个字段,删除了(包括unused)其中一个字段,oracle会怎么处理这个索引。同样,如果是约束,oracle又怎么处理?
用oracle为例子,我又拿mysql做了一个对比,看看mysql是怎么处理这个问题的。我这里不讨论谁好谁差,只是希望大家知道其中的差别与细节而已。
我们先看oracle的例子,我们创建一个表,然后在上面创建一个约束,创建一个索引:
sql 10g>create table test(a int,b int,c int);
table created.
sql 10g>alter table test add constraint pk_test primary key (a,b);
table altered.
sql 10g>create index ind_test on test(b,c);
index created.
然后,我们检查刚才创建的约束与索引
sql 10g>select t.constraint_name,c.constraint_type,t.column_name,t.position,c.status,
c.validated
2 from user_cons_columns t,user_constraints c
3 where c.constraint_name=t.constraint_name
4 and c.constraint_type != 'c'
5 and t.table_name = 'test'
6 order by constraint_name,position;
constraint_name c column_name position status validated
---------------- - ------------ ---------- -------- -------------
pk_test p a 1 enabled validated
pk_test p b 2 enabled validated
sql 10g>select t.index_name,t.column_name,t.column_position,i.status
2 from user_ind_columns t,user_indexes i
3 where t.index_name=i.index_name
4 and t.table_name = 'test'
5* order by index_name,column_position
index_name column_name column_position status
-------------- ------------ --------------- --------
ind_test b 1 valid
ind_test c 2 valid
现在,我们先删除索引上的字段,其实并没有物理删除,只是设置为unused:
sql 10g>alter table test set unused (c);
table altered.
sql 10g>select t.index_name,t.column_name,t.column_position,i.status
2 from user_ind_columns t,user_indexes i
3 where t.index_name=i.index_name
4 and t.table_name = 'test'
5 order by index_name,column_position;
no rows selected
发现了什么,索引也删除了。那我们再删除约束上的字段呢?
sql 10g>alter table test set unused (b);
alter table test set unused (b)
*
error at line 1:
ora-12991: column is referenced in a multi-column constraint
sql 10g>alter table test set unused (b) cascade constraints;
table altered.
sql 10g>select t.constraint_name,c.constraint_type,t.column_name,t.position,c.status,
c.validated
2 from user_cons_columns t,user_constraints c
3 where c.constraint_name=t.constraint_name
4 and c.constraint_type != 'c'
5 and t.table_name = 'test'
6 order by constraint_name,position;
no rows selected
我们可以看到,正常的删除会报一个错误,如果我们指定了cascade,将会把对应的约束也删除。
我们看完了oracle的处理过程,再看看mysql是这么处理删除索引上字段这个事情的
mysql> create table test(a int,b int,c int);
query ok, 0 rows affected (0.72 sec)
mysql> alter table test add primary key(a,b);
query ok, 0 rows affected (0.27 sec)
records: 0 duplicates: 0 warnings: 0
mysql> create index ind_test on test(b,c);
query ok, 0 rows affected (0.32 sec)
records: 0 duplicates: 0 warnings: 0
我们执行同样的操作,先删除复合索引中的一个字段,然后删除约束中的一个字段。
mysql> alter table test drop c;
query ok, 0 rows affected (0.58 sec)
records: 0 duplicates: 0 warnings: 0
mysql> show index from test;
+-------+------------+----------+--------------+-------------+-----------+
| table | non_unique | key_name | seq_in_index | column_name | collation |
+-------+------------+----------+--------------+-------------+-----------+
| test | 0 | primary | 1 | a | a |
| test | 0 | primary | 2 | b | a |
| test | 1 | ind_test | 1 | b | a |
+-------+------------+----------+--------------+-------------+-----------+
3 rows in set (0.06 sec)
mysql> alter table test drop b;
query ok, 0 rows affected (0.28 sec)
records: 0 duplicates: 0 warnings: 0
mysql> show index from test;
+-------+------------+----------+--------------+-------------+-----------+
| table | non_unique | key_name | seq_in_index | column_name | collation |
+-------+------------+----------+--------------+-------------+-----------+
| test | 0 | primary | 1 | a | a |
+-------+------------+----------+--------------+-------------+-----------+
1 row in set (0.03 sec)
可以看到,mysql的处理方式是有差别的,mysql仅仅是把字段从索引中拿掉,而不是删除该索引。本文的意思,就是想提醒大家,平常在做columns删除的时候,包括unused,一定要小心,是否有复合索引包含了该字段,否则,一不小心把索引删除了,可能将引发大的错误。
【相关文章】
mysql和oracle数据库的一些异同oracle数据库的完整性约束规则详解db2数据库的约束
