数据库分为关系型数据库和非关系型数据库
| 关系型数据库 | 非关系型数据库 | |
|---|---|---|
| 使用SQL | 是 | 不强制要求,一般不基于SQL实现 |
| 事务支持 | 支持 | 不支持 |
| 复杂操作 | 支持 | 不支持 |
| 海量读写操作 | 效率低 | 效率高 |
| 基本结构 | 基于表和列 结构固定 | 灵活性比较强 |
| 使用场景 | 业务方面的OLTP系统 | 用于数据的缓存,或基于统计分析的OLAP的系统 |
字符类型
char
varchar(M),M是最大的字符数,可变长度的字符,节省空间,效率低
text
blob
tinyint(1字节),smallint(2字节),mediumint(3字节),int/integer(4字节)
bigint(8字节)
浮点型小数:float(M,D),double(M,D),
定点型小数:dec(M,D), decimal(M,D)
日期类型
date: yyyy:MM:dd HH:mm:ss
time: HH:mm:ss
datetime: yyyy:MM:dd
timestamp(时间戳):长整数
INSERT INTO table (col1,col2,col3) VALUES (a,b,c),(a1,b1,c1);
DELETE FROM table;
DELETE FROM table where xx = xx;
SELECT * FROM table;
SELECT col1,col2,col3 FROM table;
UPDATE table set col1 = 值 WHERE id=xx;
默认升序
SELECT * FROM table WHERE 条件 ORDER BY 列名 ASC|DESC;
查询不重复的记录
SELECT DISTINCT 列名 FROM table;
一般配合sum(),avg(),count(),max(),min()五个聚合函数使用的
根据部门编号groupId分组,统计每个部分的平均工资
SELECT groupId,avg(sal) avgs FROM table group by groupId;
select * from table where id=1;
select * from table where name > 18;
select * from table where name > 18 and gender = 0;
select * from table where name > 18 or gender = 0;
select * from table where not (name > 18 and gender = 0);
select * from table where name like "a%";
select * from table where name like "_a%";
SELECT * FROM table LIMIT 10;(查询前10行)
SELECT * FROM table LIMIT 10 OFFSET 20; 从查询到的第20行开始返回10行
SELECT * FROM table LIMIT pageSize OFFSET offset;
SELECT * FROM table LIMIT offset,count; 从offset位置开始,返回count行
select s.id,t.t_id from student s,teacher t where s.teacher_id = t.t_id;
select * from tb_emp e inner join tb_dept d on e.dept_id = d.id;
inner可省略
相当于查询表1的所有数据,包含表1和表2交集部分的数据
select 字段列表 from 表1 left outer join 表2 on 条件;
outer可省略
查询表2所有数据包含表1和表2交集部分的数据
select 字段列表 from 表1 right outer join 表2 on 条件;
select * from tb_emp where dept_id = (select id from tb_dept where name='xx');//()返回的是一条记录,可以用=
select * from tb_emp where dept_id in (select id from tb_dept where name in ('xx','yy'));//查询返回多条记录,用in
select name from tb_emp where salary>8000
UNION
select name from tb_emp where age > 40;