1、查看默认的存储引擎:
show engines \G;
2、设置表的存储引擎:
创建表的时候设置存储引擎。
3、修改存储引擎:
mysql> alter table vc engine = myisam;
各种存储引擎的特性
MyISAM
不支持事务和外键,优势是访问速度快,对事务完整性没有要求或者以select、insert为主的应用基本上都可以使用该存储引擎来创建表。
InnoDB
提供了具有提交、回滚和奔溃恢复能力的事务安全。
自动增长列
对于InnoDB表,自动增长列必须是索引。如果是组合索引,也必须使组合索引的第一列,但是对于myisam表,自动增长列可以是组合索引的其他列,这样插入记录后,自动增长列是按照组合索引的前面几列进行排序后递增的。
mysql> create table autoincre_demo(
-> d1 smallint not null auto_increment,
-> d2 smallint not null,
-> name varchar(10),
-> index(d2,d1)
-> )engine=myisam;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into autoincre_demo(d2,name) values(2,'2'),(3,'3'),(4,'4'),(2,'2'),(3,'3'),(4,'4');
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from autoincre_demo;
+----+----+------+
| d1 | d2 | name |
+----+----+------+
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 1 | 4 | 4 |
| 2 | 2 | 2 |
| 2 | 3 | 3 |
| 2 | 4 | 4 |
+----+----+------+
6 rows in set (0.00 sec)
mysql> insert into autoincre_demo(d2,name) values(5,'5');
Query OK, 1 row affected (0.01 sec)
mysql> select * from autoincre_demo;
+----+----+------+
| d1 | d2 | name |
+----+----+------+
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 1 | 4 | 4 |
| 2 | 2 | 2 |
| 2 | 3 | 3 |
| 2 | 4 | 4 |
| 1 | 5 | 5 |
+----+----+------+
7 rows in set (0.00 sec)
外键约束
mysql> create table country(
-> country_id smallint unsigned not null auto_increment,
-> country varchar(50) not null,
-> last_update timestamp,
-> primary key(country_id)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> create table city(
-> city_id smallint unsigned not null auto_increment,
-> city varchar(50),
-> country_id smallint unsigned not null,
-> last_update 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
-> );
子表在创建外键约束时,可以指定在删除、更新父表时,对子表进行的相应操作,包括restrict、cascade、set null和no action。其中restrict和no action相同,是指限制在子表有关联记录的情况下父表不能更新;cascade表示父表在更新或删除时,更新或删除子表对应记录;set null表示父表在更新或删除的时候,子表的对应字段被set null。