1,创建表的时候,使用存储引擎
mysql> create table ai(id int) engine=Myisam;
Query OK, 0 rows affected (0.03 sec)
2,查看存储引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
3,存储引擎—MyISAM
mysql默认存储引擎,不支持事务,不支持外键,访问速度快,对事物完整性没有要求的比如select,insert为主的应用可以使用此引擎来创建表
每个MyISAM在磁盘上存储成3个文件,扩展名分别为
.frm(存储表定义)
.MYD(MYData, 存储数据)
.MYI(MYIndex, 存储索引)
表可能会被损坏
可以通过
mysql> check table ai;
+---------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+-------+----------+----------+
| user.ai | check | status | OK |
+---------+-------+----------+----------+
1 row in set (0.01 sec)
来检查表是否损坏
4,innodb mysql仅有的支持外建的存储引擎(创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引)
mysql> create table country(country_id smallint unsigned not null auto_increment, country varchar(50) not null, last_update timestamp not null default current_timestamp on update current_timestamp, primary key (country_id))engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql> desc country;
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| country_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| country | varchar(50) | NO | | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.01 sec)
mysql> create table city(city_id smallint unsigned not null auto_increment, city varchar(50) not null, country_id smallint unsigned not null, last_update timestamp not null default current_timestamp on update current_timestamp, primary key (city_id), key idx_fk_country_id(country_id), constraint fk_city_country foreign key (country_id) references country (country_id) on delete restrict on update cascade)engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.07 sec)
mysql> desc city;
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| city_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| city | varchar(50) | NO | | NULL | |
| country_id | smallint(5) unsigned | NO | MUL | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.01 sec)
子表的外键指定是on delete restrict on update cascade 主表删除记录时,对应子表有对应记录,不允许删除,主要是更新操作时,子表有记录,子表对应更新
5,memory
mysql> create table memoryDemo engine=memory select city_id, city, country_id from city group by city_id;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select count(*) from memoryDemo;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.01 sec)
hash索引,存储在内存中
6,merge
底层是myisam
存储引擎选择
myisam
默认的mysql插件式存储,如果应用以读操作和插入操作为主,只有很少的更新和删除操作,并且对事务的完整性,并发要求不高的情况下选择
innodb
用于事务处理应用程序,支持外键,如果应用对事务的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作处理查询,插入以外还有更新和删除操作,innodb存储引擎除了有效的降低由于删除和更新导致的锁定,还能保证事务的完整提交(commit)和回滚(RollBack)对于类似计费系统或者财务系统等对数据准确性要求比较高的系统
memory
将所有的数据都保存在ram中,在需要快速定位记录和其他类似数据的环境下,可提供极快的访问,memory的缺陷是对表的大小有限制,太大的表无法cache在内存中,其次是要确保表的数据可以恢复,数据库异常终止后表的数据是可以恢复的,memory表通常用于更新不太频繁的小表
merge
用于将一系列相同的myisam表以逻辑方式组合在一起,并作为一个对象引用他们,merge表的优点在于可以突破单个myiscam表大小的限制,并且通过将不同的表分布在多个磁盘上,可以有效的改善merge表的访问效率,这对于诸如多数据仓储等vldb环境