next up previous contents
Next: Unique key 不重複鍵 Up: KEY 及 INDEX Previous: Index 簡介   Contents

Primary key 主鍵

  1. 資料表 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>
    
  2. 於資料表 dcount_tbl 欄位 dyw_author 加入主鍵,其中 Key_name: PRIMARY 表示主鍵,Non_qunique: 0 表示不重複。
    Collation: How the column is sorted in the index. In MySQL, this can have values “A” (Ascending) or NULL (Not sorted).
    Cardinality: An estimate of the number of unique values in the index.
    mysql> alter table dcount_tbl add primary key (dyw_author);
    mysql> show index from dcount_tbl\G
    *************************** 1. row ***************************
            Table: dcount_tbl
       Non_unique: 0
         Key_name: PRIMARY
     Seq_in_index: 1
      Column_name: dyw_author
        Collation: A
      Cardinality: 6
         Sub_part: NULL
           Packed: NULL
             Null: 
       Index_type: BTREE
          Comment: 
    Index_comment:
    
  3. 刪除資料表 dcount_tbl 的主鍵,因每個資料表只能有一個主鍵,所以不用指定欄位。
    mysql> alter table dcount_tbl drop primary key;
    mysql> show index from dcount_tbl\G
    mysql>
    



2017-06-01