数据库分为: 
 
 
 
 
          关系数据库和非关系数据库 
 
 
 
 
          关系数据库:支持sql语言 
 
 
 
 
          非关系数据库:不支持sql语言 
 
 
 
 
  关系就和二维的表格一样,一个表格一个关系 
 
 
 
 
  DBMC:数据库管理系统 
 
 
 
 
  DDL:数据定义语言  
 
 
 
 
  create:创建   drop:删除   alter:修改   rename:重命名   truncate:截断 
 
 
 
 
  DML:数据管理系统 
 
 
 
 
  insert:插入   delete:删除  update:更新   select:查询 
 
 
 
 
  DCL:数据库控制语言 
 
 
 
 
  grant:授权  revoke:回收权利    commit:提交事务    roliback:回收事务 
 
 
  
 
 
  SELECT : 
 
 
 
 
          select 数据 from 数据源 -->这里的数据可以是*  而这个*代表所有列   select  * from 表名;--查询这个表中的所有列,其中这个--是sql语句中的注释 
 
 
  
 
 
  其中的命令不区分大小写,但是字符串中的内容还是要区分大小写的 
 
 
  
 
 
  伪例:在表中不存在但是可以用,可以查,但是查出来那一列都是这个伪例,伪例可以为整数,表达式,字符串. 
 
 
  
 
 
  --例子,在下面这个emp就是人员表,用它类替代表名 
 
 
 
 
  select 123*456 from emp; 
 
 
 
 
  其中的'123*456'就是表名,而且这个值就是下面列的值,且sql语句中的字符串的表示就是用' '且是英文形式,连接符在Java中是'+',但是在sql中是' || ' 
 
 
  
 
 
  别名:为查询结果集中的列起名(名字默认字段名或伪例的值) 
 
 
  
 
 
  select 数据 from 数据源 where exists (结果集)存在即合理,从from后的数据源中拿出一条数据,执行where 后地判断,判断是否满足条件,看见exists,就观察后面() 中的结果集是否存在数据 
 
 
 
 
  ,存在数据就满足条件,被保留,不存在就不合理 
 
 
  
 
 
  select *from emp e where exists(select * from dapt d where dname in('SALES','ACCOUNTING')and d.deptno=e.deptno); 
 
 
  
 
 
  --排序  order by 排序字段 
 
 
 
 
  --select 数据 from 数据源 where 行过滤条件 order by 排序字段1,desc\asc(排序字段2) 
 
 
 
 
  --查询部门序号为10和30的部门所有的列 
 
 
 
 
  select *from emp where deptno in (10,30) order by sal,empno desc; 
 
 
  
 
 
  还有一点:查询所有字段的值,emp1,emp2,这样的,字段名是优于*的 
 
 
 
 
  select * from  emp; 
 
 
 
 
  select * ename,empno from emp; 
 
 
  
 
 
  数据库中的函数: 
 
 
 
 
          单行函数:一行记录返回一个结果 
 
 
 
 
          多行函数\组函数\聚合函数:多条记录返回一个结果  nvl(comm); 
 
 
  
 
 
  --用单行函数打出当前时间 
 
 
 
 
  --去重加伪列 
 
 
 
 
  select distinct sysdatefrom emp; 
 
 
 
 
  --虚表 
 
 
 
 
  select sysdate from dual; 
 
 
  
 
 
  --2天后的日期 
 
 
 
 
  select sysdate,sysdate+2 from dual; 
 
 
 
 
  --所有员工的入职日期 
 
 
 
 
  select ename,hiredate,hiredate-3 from emp; 
 
 
 
 
  --查询所有员工的试用期到期,三个月的试用期 
 
 
 
 
  select empno,ename,sal,hiredate ,hiredate+90  from emp; 
 
 
 
 
  --上面那个无法算出月份的日期,容易出错,例如2月的情况 
 
 
 
 
  select empno ,ename,sal,hiredate,add_months(hiredate,3) from emp; 
 
 
 
 
  --查询所有员工到目前为止一共工作了几个月 
 
 
 
 
  select empno ,ename ,sal,hiredate,add_months(hiredate) from emp; 
 
 
 
 
  --当月的最后一天 
 
 
 
 
  select last_day(sysdate) from emp; 
 
 
 
 
  --下周的周三是几号 
 
 
 
 
  select next_day(sysdate,'周三') from emp; 
 
 
 
 
  --日期与字符串的转换问题 
 
 
 
 
  --to_char(日期对象,'模板') 
 
 
 
 
  --to_date(日期字符串,'模板') 
 
 
 
 
  --下面的dual是虚表 
 
 
 
 
  select to_date('2021-7-29 5:55:55','yyyy-mm-dd hh24:mi:ss')from dual; 
 
 
 
 
  select to_char(sysdate,'yyyy"年"mm"月"dd'"日")from dual; 
 
 
  
 
 
  --组函数-count() sum() max() min()  avg() 
 
 
 
 
  --统计下公司有多少员工 
 
 
 
 
  --用列的数量来计算 
 
 
 
 
  select count(*) from emp; 
 
 
 
 
  --用员工个数来计算 
 
 
 
 
  select count(empno) from emp; 
 
 
 
 
  --用列数来计算 
 
 
 
 
  select count(1) from emp; 
 
 
  
 
 
  --统计有几个部门 
 
 
 
 
  --有几个部门编号就有几个部门 
 
 
 
 
  select count(deptno) from dept; 
 
 
 
注意:
 
 
        select后面出现的组函数的使用,只能和组函数还有分组字段一起使用
 
  
 
 
  --统计有员工存在的部门总数 
 
 
 
 
  --看看有人的有几个 
 
 
 
 
  select count (distinct deptno) from emp; 
 
 
 
 
  --统计20部门有几个人 
 
 
 
 
  select count(1) from emp where deptno=20; 
 
 
 
 
  --查询最高工资和最低工资 
 
 
 
 
  select max(sal),min(sal) from emp; 
 
 
 
 
  --查询出20部门的工资平均数 
 
 
 
 
  select avg(sal) from emp where deptno=20; 
 
 
 
 
  --查询出20部门的工资和部门编号 
 
 
 
 
  select avg(sal) ,deptno from where deptno =20  group by deptno; 
 
 
 
 
  --计算出奖金的总和(comm)其中null不参与组函数运算 
 
 
 
 
  select sum(comm) from emp; 
 
 
 
 
  --有奖金的人数 
 
 
 
 
  select count(1) from emp whyere comm is  not null; 
 
 
  
 
 
  分组:group by 分组字段1,分组字段2 
 
 
 
注意:
 
 
        一旦分组,分组之后的操作都是以组为单位进行运算的,只能看到有多少组,每个组的分组字段的值是什么,但是看不到组中的数据
 
 
 
  查询 : select 数据 from 数据源 where 行过滤条件 group by 分组字段 having 组过滤信息 order by 排序字段 
 
 
 
 
  流程:from--> where --> group by -->having --> select -->order by  
 
 
 
注意:
 
 
        一旦分组,select 后面只能查询分组字段或者组函数了
 
 
 
  --找出每个部门的最高工资 
 
 
 
 
  select deptno,max(sal),min(sal),sum(sal) ,count(1) from emp group by deptno; 
 
 
 
 
  --找出20,30部门的最高工资,先过滤后分组--这个order by是排序的 
 
 
 
 
  select  max(sal),deptno from emp where deptno in(20,30) group by deptno order by max(sal) desc; 
 
 
 
 
  --先分组 
 
 
 
 
  select max(sal),deptno from emp group by deptno having deptno in(20,30); 
 
 
 
 
  --求出平均工资高于2000的部门编号和平均工资 
 
 
 
 
  select deptno,avg(sal) from emp group by deptno having avg(sal)>2000; 
 
 
 
 
  --求最低平均工资的部门编号 
 
 
 
 
  select deptno from emp group y deptno having avg(sal)=(select min(avg(sal)) )from emp group by deptno; 
 
 
  
 
 
  连表查询: 
 
 
 
 
          当要查询的数据来自不同的数据源\表,就可以用连表查询 
 
 
 
 
      语法:92语法,99语法(92年出来的.....) 
 
 
 
 
  92-:select 数据 from 数据源1,数据源2.......笛卡尔积:这是个乘法 
 
 
 
 
  select *from emp,dept; 
 
 
  
 
注意:
 
 
        查询同名字段需指明出处
 
 
 
  表连接条件: 
 
 
 
 
          等值连接:select *from emp e,dept d where  e.deptno =d.deptno; 
 
 
 
 
  select  e.empno  ,d.dname,edeptno  from emp e,deptno d where e.deptno=d.deptno; 
 
 
 
 
          非等值连接:区间 
 
 
 
 
  --查询10,30部门的员工信息以及所在部门的信息 
 
 
 
 
  select e* ,d* from emp e,deptno d where e.deptno=d,deptno and e.deptno in(10,30) order by sal; 
 
 
 
 
  等值连接条件的字段一般为主外键关联关系,或者同名字段,但是主要类型相同就可以 
 
 
 
 
    
 
 
 
 
  员工表emp1,经理表emp2 
 
 
 
 
  自连接,一张表为两个身份,自己与自己连接 
 
 
 
 
  select *from emp e1,emp e2  where e1,mgr=e2.empno(+); 
 
 
 
 
  外连接分为左外连接与右外连接,表连接位置就是上面的(emp e1,emp e2)主表在左边就是左连接,在右边就是右连接,其中这个(+)在哪里,哪里就是经理表