TIDB对表增加字段或者索引,如何判断已经存在就不增加

不用那麽麻煩,直接加 if not exists 不就好了

TiDB [test] 15:04:01> alter table t1 add column if not exists c2 int;
Query OK, 0 rows affected, 1 warning (0.001 sec)

Note (Code 1060): Duplicate column name 'c2'

TiDB [test] 15:05:10> create index if not exists idx_t1_c2 on t1(c2);
Query OK, 0 rows affected (2.890 sec)

TiDB [test] 15:05:25> create index if not exists idx_t1_c2 on t1(c2);
Query OK, 0 rows affected, 1 warning (0.002 sec)

Note (Code 1061): index already exist idx_t1_c2
1 个赞