现需要将一个字段的非唯一索引调整为唯一索引。
alter table table_name add unique key(xx);
问已经解决。防止生产出现无索引情况,准备新建唯一索引后再删除原来非唯一索引。但会出现报错【ErrorCode: 1408,ORA-01408: such column list already indexed】此时继续增加非唯一索引,列为当前列后面增加一个无关紧要列。此索引为临时增加的复合索引。增加后删除原来非唯一索引,创建唯一索引,再删除临时增加的复合索引。
1 个赞
不知道这样的处理是不是标准的解决办法?有经验的朋友请指教。
如果是生成在使用,这个办法是比较保险的办法
直接加没有问题。
MySQL [test]> create table t2(id bigint not null, c1 varchar(50), c2 timestamp );Query OK, 0 rows affected (0.41 sec)
MySQL [test]> create index t2_ind1 on t2(id);
iQuery OK, 0 rows affected (0.78 sec)
MySQL [test]> insert into t2 values(1,'a',now());
Query OK, 1 row affected (0.00 sec)
MySQL [test]> insert into t2 values(2,'b',now());
Query OK, 1 row affected (0.00 sec)
MySQL [test]> alter table t2 add unique key t2_Uk(id);
Query OK, 0 rows affected (0.80 sec)
MySQL [test]> show indexes from t2;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+------------+
| t2 | 1 | t2_ind1 | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES | NULL |
| t2 | 0 | t2_Uk | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES | NULL |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+------------+
2 rows in set (0.02 sec)
MySQL [test]> alter table t2 drop key t2_ind1;
Query OK, 0 rows affected (0.72 sec)
1 个赞
您这是MYSQL,ORACLE 的字段上有索引时不能再创建唯一索引的。