【数据库MySQL】练习---索引

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

猜你喜欢

转载自blog.csdn.net/trichloromethane/article/details/112826649