数据库1
1.存储引擎
数据的存储方式 -- 存储引擎engines.使用不同的存储引擎,数据是以不同的方式存储的.
show engines;(查看存储引擎)
innodb 2个文件
mysql5.6以上 默认的存储方式
transaction 事务 保证数据安全 数据的完整性而设置的概念
row-level locking 行级锁
table-level locking 表级锁
foreign keys 外键约束
树tree - 加速查询 (树形结构(数据+树) + 表结构)
myisam 3个文件
mysql5.5以下 默认的存储方式
table-level locking 表级锁
树tree - 加速查询 (树形结构 + 数据 + 表结构)
memory 1个文件
基于hash
show create table staff;
create table myisam_t (id int,name char(18)) engine=myisam;(设置数据的储存方式)
create table memory_t (id int,name char(18)) engine=memory;
2.数据类型
数值
tinyint int
create table int_t (
ti tinyint,
i int,
tiun tinyint unsigned,
iun int unsigned
);
create table fd_t(
f float, # 精度问题 小数点后5位
d double, # 精度更高但也不准确
f2 float(5,2),# 在精确位四舍五入
d2 double(5,2)# 在精确位四舍五入
)
create table dec_t(
dec1 decimal,
dec2 decimal(30,20)
)
时间
内置函数 now()
datetime 打卡时间/日志/论坛博客类的评论\文章/
date 员工生日/入职日期/离职日期/开班时间
time 上课时间/下课时间/规定上班时间 竞赛数据
year
timestamp 由于表示范围的问题,导致用的少了
create table time_t2(
dt datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, # 表示的范围更大,还能拥有timestamp的特点
d date,
t time,
y year,
ts timestamp # 不能为空,默认值是当前时间,在修改的时候同时更新时间
)
字符串
char 0-255 定长存储 存储速度更快 占用更多的空间
char(12)
alex --> 'alex ' --> 12的长度
varchar 0-65535 变长存储 存储速度慢 占用的空间小
varchar(12)
'alex' --> 'alex4' --> 5的长度
手机号码/身份证号码 : char
用户名/密码 : 有一定范围弹性 char
评论 : varchar
create table ch_t(
c char,
c2 char(5),
vc2 varchar(5)
)
enum和set
单选题,只能从有限的条件中选择
create table enum_t(
id int,
name char(12),
gender enum('男','女','不详')
)
多选题,从有限的条件中选
create table set_t(
id int,
name char(12),
hobby set('抽烟','喝酒','烫头','搓脚')
)
3.约束
not null 非空约束
(后缀的选项不能为空)
create table t1(id int not null,
name char(12) not null,
age tinyint unsigned
);
create table t2(id int not null,
name char(12) not null,
age tinyint unsigned not null default 18
);
create table t2(id int not null,
name char(12) not null,
age tinyint unsigned default 18
);
unique 唯一约束
后缀的选项具有唯一性,不能重复.;例如: id ,username, ident ,phone mail_number.
create table t3(
id int unique,
username char(18) unique,
age tinyint unsigned
)
非空+唯一
如果一张表中没有设置primary key 主键,那么第一个设置非空+唯一的字段会被设置成主键.
一张表中只能有一个主键.primary key 主键的功能和非空+唯一一样.
create table t4(
id int not null unique,
username char(18) not null unique,
age tinyint unsigned
);
主键的约束作用 = 非空+唯一 特点:一张表只能有一个主键
create table t5(
id int primary key,
username char(18) not null unique,
age tinyint unsigned
);