-- 终端A
mysql> create database myisam_test; -- 创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> use myisam_test;
Database changed
mysql> create table mtest(
-> id int primary key,
-> name varchar(11) not null
-> )engine=MyISAM; -- 使用engine=MyISAM
Query OK, 0 rows affected (0.01 sec)
-- 终端B
[root@VM-0-3-centos mysql]# ls myisam_test/ -al --mysql数据目录下
total 28
drwxr-x--- 2 mysql mysql 4096 Jun 13 13:33 .
drwxr-x--x 13 mysql mysql 4096 Jun 13 13:32 ..
-rw-r----- 1 mysql mysql 61 Jun 13 13:32 db.opt
-rw-r----- 1 mysql mysql 8586 Jun 13 13:33 mtest.frm -- 表结构数据
-rw-r----- 1 mysql mysql 0 Jun 13 13:33 mtest.MYD -- 该表对应的数据,当前没有数据,所以是0
-rw-r----- 1 mysql mysql 1024 Jun 13 13:33 mtest.MYI -- 该表对应的主键索引数据
注:MyISAM 这种用户数据与索引数据分离的索引方案,叫做非聚簇索引
代码语言:javascript
代码运行次数:0
运行
复制
-- 终端A
mysql> create database innodb_test; -- 创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> use innodb_test;
Database changed
mysql> create table itest(
-> id int primary key,
-> name varchar(11) not null
-> )engine=InnoDB; -- 使用engine=InnoDB
Query OK, 0 rows affected (0.02 sec)
-- 终端B
[root@VM-0-3-centos mysql]# ls innodb_test/ -al
total 120
drwxr-x--- 2 mysql mysql 4096 Jun 13 13:39 .
drwxr-x--x 14 mysql mysql 4096 Jun 13 13:38 ..
-rw-r----- 1 mysql mysql 61 Jun 13 13:38 db.opt
-rw-r----- 1 mysql mysql 8586 Jun 13 13:39 itest.frm -- 表结构数据
-rw-r----- 1 mysql mysql 98304 Jun 13 13:39 itest.ibd -- 该表对应的主键索引和用户数据,虽然现在一行数据没有,但是该表并不为0,因为有主键索引数据
注:InnoDB 这种用户数据与索引数据在一起索引方案,叫做聚簇索引
4、普通索引
MySQL 除了默认会建立主键索引外,我们用户也有可能建立按照其他列信息建立的索引,一般这种索引可以叫做辅助(普通)索引
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
body TEXT,
FULLTEXT (title,body)
)engine=MyISAM;
代码语言:javascript
代码运行次数:0
运行
复制
INSERT INTO articles (title,body) VALUES
('MySQL Tutorial','DBMS stands for DataBase ...'),
('How To Use MySQL Well','After you went through a ...'),
('Optimizing MySQL','In this tutorial we will show ...'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL vs. YourSQL','In the following database comparison ...'),
('MySQL Security','When configured properly, MySQL ...');
查询有没有database数据:
使用基本查询方式:
使用全文索引:
索引创建原则:
较频繁作为查询条件的字段应该创建索引
唯一性太差的字段不适合单独创建索引,即使频繁作为查询条件
更新非常频繁的字段不适合作创建索引
不会出现在where子句中的字段不该创建索引
2、查询索引
第一种方法: show keys from 表名
第二种方法: show index from 表名
第三种方法(信息比较简略): desc 表名
3、删除索引
第一种方法-删除主键索引: alter table 表名 drop primary key
第二种方法-其他索引的删除: alter table 表名 drop index 索引名
代码语言:javascript
代码运行次数:0
运行
复制
-- 索引名就是 show keys from 表名中的 Key_name 字段
mysql> alter table user10 drop index idx_name;