dcount_tbl
沒有索引。
[root@kvm8 ~]# mysql -s -uroot -p123qwe mysql> use dyw; mysql> show tables; Tables_in_dyw dcount_tbl dyw_tbl mysql> show index from dcount_tbl\G mysql>
dcount_tbl
欄位 dyw_author
沒有重複。
mysql> select * from dcount_tbl; +------------+-----------+ | dyw_author | dyw_count | +------------+-----------+ | abc123 | 23 | | abc321 | 43 | | abcxyz | NULL | | xyz123 | 83 | | xyz222 | 12 | | xyz456 | NULL | +------------+-----------+ 6 rows in set (0.00 sec)
dcount_tbl
加入一筆資料,欄位 dyw_author
abc123 重複。
mysql> insert into dcount_tbl (dyw_author,dyw_count) values ("abc123","34"); Query OK, 1 row affected (0.07 sec)
dcount_tbl
欄位 dyw_author
abc123 有重複。
mysql> select * from dcount_tbl; +------------+-----------+ | dyw_author | dyw_count | +------------+-----------+ | abc123 | 23 | | abc321 | 43 | | abcxyz | NULL | | xyz123 | 83 | | xyz222 | 12 | | xyz456 | NULL | | abc123 | 34 | +------------+-----------+ 7 rows in set (0.00 sec)
dcount_tbl
欄位 dyw_author
加入不重複鍵不成功,因為有資料重複。
mysql> alter table dcount_tbl add unique key(dyw_author); ERROR 1062 (23000): Duplicate entry 'abc123' for key 'author'
dcount_tbl
欄位 dyw_author
中重複的一筆資料。
mysql> delete from dcount_tbl where dyw_count='34'; Query OK, 1 row affected (0.13 sec)
dcount_tbl
欄位 dyw_author
加入不重複鍵成功。
mysql> alter table dcount_tbl add unique key(dyw_author); Query OK, 0 rows affected (0.32 sec) Records: 0 Duplicates: 0 Warnings: 0
dcount_tbl
index,其中 Non_qunique: 0
表示不重複。add unique (field) 與 add unique key (field) 結果一樣。
mysql> alter table dcount_tbl add unique (dyw_author); mysql> show index from dcount_tbl\G *************************** 1. row *************************** Table: dcount_tbl Non_unique: 0 Key_name: dyw_author Seq_in_index: 1 Column_name: dyw_author Collation: A Cardinality: 6 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: mysql>
dcount_tbl
加入欄位 dyw_author
重複的資料。
mysql> insert into dcount_tbl (dyw_author,dyw_count) values ("abc123","34"); ERROR 1062 (23000): Duplicate entry 'abc123' for key 'author'
dcount_tbl
的不重複鍵,因不重複鍵是索引,所以使用 drop index dyw_author
。
mysql> alter table dcount_tbl drop index dyw_author; mysql> show index from dcount_tbl\G mysql>