数据库基础知识汇总

1、事务

事务用来管理insert、update、delete语句,必须满足4个条件(ACID):Atomicity(原子性)、Consistency(稳定性)、Isolation(隔离性)、Durability(可靠性)。

2、索引

作用:大大提高MySQL检索速度。

索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可有多个单列索引,但这不是组合索引。组合索引,即一个索引包含多个列。

实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。

缺点:虽然提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。此外,建立索引会占用磁盘空间的索引文件。

/*
索引
*/
alter table sys_role add index index_createtime(createtime);#添加普通索引
ALTER TABLE sys_role add UNIQUE index_createtime(createtime);-- 添加唯一索引
ALTER TABLE sys_role ADD INDEX index_name('column1','column2','column3');/*添加多列索引*/

3、mysql流程函数

①if(value,t,f) 若value为真,返回t;否则,返回f。

②ifnull(value1,value2)若value1非空,则返回value1;否则返回value2。

③case when [value1] then [result] ...else [default] end 勿忘记end。

case when salary<=2000 then 'low' else 'high' end

④case [expr] when [value1] then [result] ... else [default] end

select case salary when 1000 then 'low' when 2000 then 'mid' else 'high' end from salary; 

其他常用函数:

inet_aton(ip)和inet_ntoa(num)

例如我们想要知道在‘192.168.1.3.和''192.168.1.20'间共有多少IP地址?

表中地址包括:192.168.1.1;192.168.1.3;192.168.1.6;192.168.1.10;192.168.1.20;192.168.1.30;

按照正常思维,应用字符串比较,如下:

select * from t where ip>='192.168.1.3' and ip<='192.168.1.20'
但返回结果为空,因为字符串比较时,为字符挨个比较,而‘192.168.1.3’其实比‘192.168.1.20’要‘大’,因为3比2大,而不是我们正常思维的3<20,正确语句如下:

select * from t where inet_aton(ip)>=inet_aton('192.168.1.3') and inet_aton(ip)<=inet_aton('192.168.1.20');
4、存储引擎InnoDB

①自动增长列

②外键约束


你可能感兴趣的:(数据库)