mysql 库、表、引擎

innoDB和myisam的区别

InnoDB支持事物,而MyISAM不支持事物

InnoDB支持行级锁,而MyISAM支持表级锁

InnoDB支持MVCC, 而MyISAM不支持

InnoDB支持外键,而MyISAM不支持

InnoDB支持聚集索引,而MyISAM不支持

InnoDB不支持全文索引,而MyISAM支持。

MyISAM存储表的行数

innoDB: 支持自动崩溃恢复、高并发

InnoDB引擎的4大特性

  • 插入缓冲(insert buffer)
  • 二次写(double write)
  • 自适应哈希索引(ahi)
  • 预读(read ahead)

memory 功能上等同于MyISAM,存储在内存,速度快,适合临时表

Archive 只支持insert和select,压缩比高,适合存日志

一张表可有多个唯一约束,唯一约束可以为null,但只能有一个null

主键互不相同,自动为not null,尽量不要修改

多列作为主键,组合唯一,单列可以重复

只有主键能自增,主键也可以不自增,

自增默认从1开始,每个表最多只允许一列自增,且必须被索引

last_insert_id()获取最后一个自增的值

有外键的表称为子表,参照的表称为父表,不能使用临时表

父表和子表必须使用相同的存储引擎innoDB

外键列和参照列必须创建索引,

数据类型可以隐式转换,数字长度和符号必须相同,字符串长度可不同

pid smallint unsigned,

foreign key (pid) references db2 (id)

foreign key (pid) references db2 (id) cascade

cascade 表示父表记录被删除或修改,子表记录也自动删除或修改

set null表示父表删除或更新,子表外键列设为null,子表的外键列不能是not null的

restrict表示父表不能删除或更新

no action与restrict相同

show create database 创建数据库的sql语句

show create table 创建表的sql语句

show errors/warnings 显示错误/警告

use db_name 使用数据库

show databases 查看数据库

select database() 查看当前数据库 user() version()

show tables [from db] 查看表

show columns from t 查看列

describe t 查看列

创建表

create table [if not exists] t

(

id int not null auto_increment,

name char(50) not null, 非空约束

age tinyint unsigned null, null允许为空,可以不写null

type int not null default 1 默认约束

phone int not null unique key 唯一约束

primary key (id, name) 主键约束

)engine=InnoDB; 省略则使用默认引擎

更新表

alter table t add email char(50);

alter table t drop column email;

alter table t 外键约束

add constraint uid foreign key (id)

references user (id)

删除表

drop table t;

重命名

rename table a to aa, b to bb;

相关推荐