数据库的这些你都知道吗?

尴尬尴尬尴尬尴尬尴尬尴尬尴尬尴尬

好尴尬

这么久,都学到数据库了,可是之前的感觉还没巩固!!!

数据库基础来啦:

--建表

create table abc(stdname varchar2(20),stdnum number(10));

--查看表结构

desc abc;

--规定表字段的值不能为 null

alter table abc modify(stdnum number(10) not null);

--查看当前系统的所有表

select * from tab;

--插入数据

insert into abc(stdname,stdnum)) values ('张三',20125555);

   --如果没有指定字段,则必须按照字段的顺序对所有字段赋值

   insert into abc values ('张三',20134444);

   --如果只对部分字段赋值,则字段名和值必须对应

   insert into abc(stdnum) values (20143333);

   

--查询表

select * from abc;

--修改数据(把张三改成李四 如果后面不加 while 则表中所有的 stdname 都会改成李四)

update abc set stdname='李四' where stdname='张三';

--删除该表的全部数据

delete abc;

--删除指定的数据( 把名字为李四的人的全部数据删掉 )

delete abc where stdname='李四';

------------------------------------------------------------  简单查询  --------------------------------------------------------------------

--使用scott用户

--查看emp表结构

desc emp;

   --查询一张表的所有数据(如果不是查询所有字段  尽量不要使用 * 来查询)

   select * from emp;

   --查询指定表中的指定字段(只查询 empno 和 ename 这两行) 查询结果会按照指定的字段顺序来显示字段数据

   select empno,ename from emp;

   --非重复的查询某个字段(不显示重复的 可用来判断种类数) --- 去掉的是结果集当中的重复语句,不是把表中的删除了

   select distinct job from emp;

--条件查询

--查询 smith 所在的部门,工作,薪水

select deptno,job,sal from emp where ename='SMITH'; --单引号里面的数据区分大小写

   --查询部门编号等于 10 和 20 的所有员工

   select * from emp where deptno=10 or deptno=20;

   --查询薪水 >2000 并且 < 2500

   select * from emp where sal>2000 and sal<2500;

   --查询入职日期在 81 年 9 月 17 号之前的人

   -- varchar 类型是可以比较大小的

   select * from emp where hiredate<'17-11月-81';

--模糊匹配查询 ( % :表示通配任意个字符 , _ :表示通配一个字符 )

--查询名字中包含 K 的

select * from emp where ename like '%K%';

   --查询名字中首字母是 S 的

   select * from emp where ename like 'S%';

   --查询名字第三个字母是 O 的(前两个打两个下划线 表示是任意字符)

   select * from emp where ename like '__O%';

   --查询没有上司的员工

   select * from emp where mgr is null;

   --使用别名查询(自己可更改组别的名字  as 可以省略)

   select empno as 编号,ename as 名字 from emp where ename like 'S%';

   --怎么查询 smith 的年薪?

   select empno 编号,ename 姓名,sal*12 年薪 from emp where ename='SMITH';

   --查询工资高于 500 或者是岗位为 MANAGER 的雇员,同时还要满足她们的姓名首字母为大写字母(记得括号的重要性)

   select * from emp where (sal>500 or job='MANAGER') and ename like 'J%';

   --查询所有的数据,要按照薪水从高到低的顺序显示

   select * from emp order by sal desc ;

   select * from emp order by sal desc ; --从低到高

   --统计所有员工的编号,姓名,年薪(月薪 + 奖金)

   --nvl(comm,0) 取得 comm 的值,如果为 null 则改为 0 参与计算(因为 null 一参与计算值也为 null 了)

   select empno 编号,ename 姓名,(sal+nvl(comm,0))*12 年薪 from emp; 

   --根据别名排序

   select empno as 编号,ename as 名字 from emp order by sal desc ;

   

--分组函数  max,min,avg( 平均数 ),sum,count( 计数 ) 

--查询最高月薪是多少

select max(sal) 月薪 from emp;

   --查询出月薪最高或者最低的员工的编号 姓名 月薪

   select empno,ename,sal from emp where sal=(select max(sal) 月薪 from emp);

   select empno,ename,sal from emp where sal=(select min(sal) 月薪 from emp);

   --同时找到月薪最低和最高的员工的编号 姓名 月薪

   select empno,ename,sal from emp where sal=(select max(sal) 月薪 from emp) or sal=(select min(sal) 月薪 from emp);

   

--计算有多少员工

select count(empno) 员工数 from emp;

   --计算总工资数

   select sum(sal) 总工资 from emp;

   --计算员工的平均月薪

   select avg(sal) from emp;

   select sum(sal)/count(empno) 平均工资 from emp;

   

------------------------------------------------------------ 分组查询 -----------------------------------------------------------------

--显示平均工资和最高工资

select avg(sal) 平均工资,max(sal) 最高工资 from emp;

   --显示每个部门的平均工资和最高工资

   --分组的条件一定要查询出来

   select avg(sal) 平均工资 ,max (sal) 最高工资,deptno 部门 from emp group by deptno;

   --显示每个部门各种岗位的平均工资和最低工资

   select avg(sal) 平均工资 ,min (sal) 最低工资,deptno 部门,job 岗位 from emp group by deptno,job 

   --显示平均工资低于 2000 的部门和它的平均工资

   select avg(sal) 平均工资,deptno 部门 from emp group by deptno  having avg(sal)<2000;

   --显示平均工资高于 2000 的部门和它的平均工资(总裁办 ( 部门编号10)不参与计算)

   select avg(sal) 平均工资,deptno 部门 from emp where deptno!=10 group by deptno  having avg(sal)>2000;

------------------------------------------------------------ 多表查询 -----------------------------------------------------------------

--查询部门表

select * from dept;

--显示雇员名,雇员工资及所在的部门的名字

select ename,sal,deptno from emp;

--笛卡尔积 ( 两个表的关联条件 )

select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno; --联系到了两张表

--显示部门号为 10 的部门名,员工名和工资 emp-->e 别名

select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno and e.deptno=10;

--显示各个员工的姓名,工资及工资的级别

select e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;

--显示员工名,员工工资,所在部门的名字,并按部门排序

select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno order by d.dname;

----------------------------------------------------------------------------------------------------------------------------------------

--查询 smith 的上司

select mgr from emp where ename='SMITH';

select ename from emp where empno=7902;

    --嵌套查询

    select ename from emp where empno=(select mgr from emp where ename='SMITH');

    --自连接查询

    select e1.ename,e1.mgr,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno and e1.ename='SMITH';

--如何让查询部门 10 的工作相同上午雇员名字、岗位、工资、部门号

    --查询部门 10 中的所有工作类型

    select job from emp where deptno=10;

    --如果子查询返回多个结果 则用 in 代替 = 

    select ename,job,sal,deptno from emp where job in (select job from emp where deptno=10);

    

--如何显示工资比部门 30 的所有员工的工资高的员工的姓名、工资和部门号

    --查询部门 30 中工资最高的

    select max(sal) from emp where deptno=30;

    --使用 max 函数

    select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

    --使用 >all

    select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30); -- 跟所有人比  并且

    

--如何显示工资比部门 30 的任意一个员工的工资高的员工姓名,工资和部门号

    --用 any

    select ename,sal,deptno from emp where sal>any(select sal from emp where deptno=30);  -- 跟任意一个人比  或者

    --或者用 min 函数

    select ename,sal,deptno from emp where sal>(select min(sal) from emp where deptno=30);

    

--如何查询部分数据

--伪列:ROWNUM (查表的时候不会出现 但是是默认存在的一列 可以人为特地查到 ( 表里没有的 只存在在结果集里面))

    --查询结果只显示 10 条

    select ROWNUM,ename from emp where ROWNUM<=10;

    --查询第 5 到第 10 条数据(不能使用 where ROWNUM>5 and ROWNUM<11 )  (一定要用别名,要不然没结果  要从上一次的结果集中查询)

    select ROWNUM,ename from ( select ROWNUM r,ename from emp where ROWNUM<=10) where r>4 and r<11;

    

--集合操作符 : union,union all,intersect,minus

    --union 合并结果并且去掉重复行

    select * from emp where job='SALESMAN' union select * from emp where sal>=1500;

    --union all 合并结果 并且保留重复行

    select * from emp where job='SALESMAN' union all select * from emp where sal>=1500;

    --得到两个结果的交集

    select * from emp where job='SALESMAN' intersect select * from emp where sal>=1500;

    --得到在第一个结果中存在但是又不在第二个结果集中存在的

     select * from emp where job='SALESMAN' minus select * from emp where sal>=1500;

     

    

---------------------------建表时设置某一段的唯一性

 create table aabbcc(

    userID number(10) primary key not null, --不可重复

    ..........

    

 )    

-----如果表中已经有了数据,就不能设置外键,

它对于我来说还是个神奇的东西,

怎么用java用它还不熟练,......

国庆七天兼职完了之后得了可怕的厌学症。。好想出去工作啊啊啊啊天真天真

会不会遭骂,,, 吐舌头

猜你喜欢

转载自i-am-running.iteye.com/blog/2128159