1.插入数据
insert into 表名 values(值1,值2,值3)--插入全部数据
insert into 表名(字段1,字段2,字段3) values(值1,值2,值3)--插入部分字段数据
insert into employee values('0001','Jason','1','1001','10000','1');
2.更新数据
update 表名 set 字段 = 新值(更新内容) where 字段 = 某值(条件)
3.删除数据
delete from 表名 where 字段 = 某值(条件)
关系运算符
>, >=, <, <= ,=,
!= ,<>不等于
逻辑运算符
并且and, 或者or,非not
between a and b 大于等于a且小于等于b,是一个带有跨度的范围
num in(a,b,c,d) num=a, num=b,num=c,num=d,是逐个匹配
操作符
/ + - * mod(m,n)m对n求余
连接符||
select (‘我叫’ || ename || ‘,年龄是’ || eage || ‘,月薪’ || salary) as 自我介绍 from employee;
去重关键字distinct
select distinct 字段1,字段2 from 表名;–必须每个字段都相等才能去重
模糊查询
like % 匹配一个或多个字符,_匹配一个字符
select * from employee where ename like 'J%'; 查询以J为首字符的姓名
select * from employee where ename like '%n%'; 查询中间有字符n的姓名
select * from employee where ename like '%n_'; 查询中间有字符n,n后只有一位字符的姓名
select * from employee where ename like '%n__'; 两个下划线,查询字符n后有两位字符的姓名
根据查询信息建表
create table 新表 as select …
create table emp1 as select * from employee where ename like ‘%n%’;
4.连接查询
内连接,自然连接
select * from emp,dept where emp.dp_id=dept.dp_id;
select * from emp inner join dept on emp.dp_id=dept.dp_id;
select * from emp join dept on emp.dp_id=dept.dp_id;
select emp.*,dept.dp_name,dept.dp_address from emp,dept where emp.dp_id=dept.dp_id;
--外连接
--【左外连接】在内连接基础上,将连接操作符左侧表不符合连接条件的记录加入结果集中,右侧表则用null填充。
select * from emp e left outer join dept d on e.dp_id=d.dp_id;
select * from emp e left join dept d on e.dp_id=d.dp_id;
select * from emp e,dept d where e.dp_id=d.dp_id(+);
--【右外连接】在内连接基础上,将连接操作符右侧表不符合连接条件的记录加入结果集中,左侧表则用null填充。
select * from emp e right outer join dept d on e.dp_id=d.dp_id;
select * from emp e right join dept d on e.dp_id=d.dp_id;
select * from emp e,dept d where e.dp_id(+)=d.dp_id;
5.聚合函数
--求模
mod(m,n)m/n求余
--求和
select sum(score) from score;
--平均数
select avg(score) 平均分数 from score;
--最大最小
select max(score) 最高分数,min(score) 最低分数 from score;
--总条数
select count(*) 总条数 from student;
select count(sid) 总条数 from student;
--排序
asc 默认升序
desc 降序
select * from score order by score;
select * from score order by score asc;
select * from score order by score desc;
--分组查询(需和聚合函数一起使用)
select count(*),sex from student group by sex;
having过滤
select count(*),sex from student group by sex having count(*)>=2;
当查询中存在group by子句时,select列表(或是having子句)中只能存在聚合函数,或是出现在group by子句中的字段。
sql关键词执行优先顺序
from>where>group by>having>select>order by