【資料庫MySQL】練習---索引
阿新 • • 發佈:2021-02-04
writers表結構:
欄位名 | 資料型別 | 主鍵 | 外來鍵 | 非空 | 唯一 | 自增 |
w_id | SMALLINT(11) | 是 | 否 | 是 | 是 | 是 |
w_name | VARCHAR(255) | 否 | 否 | 是 | 否 | 否 |
w_address | VARCHAR(255) | 否 | 否 | 否 | 否 | 否 |
w_age | CHAR(2) | 否 | 否 | 是 | 否 | 否 |
w_note | VARCHAR(255) | 否 | 否 | 否 | 否 | 否 |
1、 在資料庫裡建立表writers,儲存引擎為MyISAM,建立表的同時在w_id欄位上新增名稱為UniqIdx的唯一索引
步驟1:建立資料庫
mysql> create database if not exists db2_idx character set utf8;
Query OK, 1 row affected (0.03 sec)
步驟2:建立表及索引
mysql> create table writers( -> w_id SMALLINT(11) primary key unique auto_increment, -> w_name VARCHAR(255) not null, -> w_address VARCHAR(255), -> w_age CHAR(2) not null, -> w_note VARCHAR(255), -> unique index index_id(w_id) -> )engine=MyISAM; Query OK, 0 rows affected, 1 warning (0.01 sec)
2、使用alter table語句在w_name欄位上建立nameIdx的普通索引
mysql> Alter table writers add index nameIdx (w_name);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
3、使用CREATE INDEX 語句在w_address和w_age欄位上面建立名稱為MultiIdx的組合索引
mysql> Create index MultiIdx on writers(w_address,w_age); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
4、使用create index語句在w_note欄位上建立名稱為FTIdex的全文索引
mysql> Create fulltext index FTIdex on writers(w_note);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
5、刪除名為FTIdex的全文索引
mysql> Drop index FTIdex on writers;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0