表类型(存储引擎)的选择

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。


存储方式


MEMORY


MERGE


TokuDB


如何选择合适的存储引擎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值