Primary key 主鍵

  1. 登入 mariadb。
    [root@kvm3 ~]# mycli -uroot -p123qwe
    Connecting to socket /var/lib/mysql/mysql.sock, owned by user mysql
    MariaDB 10.5.16
    mycli 1.26.1
    Home: http://mycli.net
    Bug tracker: https://github.com/dbcli/mycli/issues
    Thanks to the contributor - Tyler Kuipers
    
  2. 使用資料庫 dyw。
    MariaDB root@(none):(none)> use dyw;
    You are now connected to database "dyw" as user "root"
    Time: 0.001s
    
  3. 查看資料表。
    MariaDB root@(none):dyw> show tables;
    +---------------+
    | Tables_in_dyw |
    +---------------+
    | dcount        |
    | tbl           |
    +---------------+
    
    2 rows in set
    Time: 0.036s
    
  4. 查詢資料表 dcount 沒有索引。
    MariaDB root@(none):dyw> show index from dcount\G
    0 rows in set
    Time: 0.032s
    
  5. 於資料表 dcount 欄位 author 加入主鍵
    MariaDB root@(none):dyw> alter table dcount add primary key(author);
    You're about to run a destructive command.
    Do you want to proceed? (y/n): y
    Your call!
    Query OK, 0 rows affected
    Time: 0.127s
    
  6. 查詢資料表 dcount 的索引,其中 Key_name: PRIMARY 表示主鍵,Non_unique: 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.
    MariaDB root@(none):dyw> show index from dcount\G
    ***************************[ 1. row ]***************************
    Table         | dcount
    Non_unique    | 0
    Key_name      | PRIMARY
    Seq_in_index  | 1
    Column_name   | author
    Collation     | A
    Cardinality   | 6
    Sub_part      | <null>
    Packed        | <null>
    Null          |
    Index_type    | BTREE
    Comment       |
    Index_comment |
    
    1 row in set
    Time: 0.006s
    
  7. 刪除資料表 dcount 的主鍵,因每個資料表只能有一個主鍵,所以不用指定欄位。
    MariaDB root@(none):dyw> alter table dcount drop primary key;
    You're about to run a destructive command.
    Do you want to proceed? (y/n): y
    Your call!
    Query OK, 6 rows affected
    Time: 0.105s
    
  8. 查詢資料表 dcount 沒有索引。
    MariaDB root@(none):dyw> show index from dcount\G
    0 rows in set
    Time: 0.003s