Oracle数据库

1.oracle数据库如何更改用户?
grant connect,resource to scott;(给用户授权)
alter user scott account unlock;
connect scott/tiger
2.host cls (清屏)

distinct(去重复)作用于后面所有的列

order by 后面+列 表达式 别名 序号(作用于后面所有的列,先按照第一列排序,再后面的列,desc作用于最近的列)

3.查询平均工资大于2000

select deptno avg(sal) from emp

group by deptno

having avg(sal)>2000;

where 后面不可以使用多行函数(where和having的区别)

左外连接

写法:where e.deptno=d.deptno(+) where e.deptno(+)=d.deptno(右外连接)

自连接:通过表的别名,将同一张表视为多张表(自连接不适合操作大表)
子查询注意的问题:
1.括号
2.合理的书写风格
3.可以在主查询的where select having from 后面使用子查询
(1)查询工资比scott高的员工信息

     select  * from emp where
     sal >(select sal from emp where ename='scott')

(2)查询部门名称是SALES和Account 的员工

  select * from emp where deptno in 
  (select deptno from dept where dname='SALES' 
   or dname='Account ' )

(3)查询工资比30 号部门任意一个员工高的员工信息

 select * from emp where sal>any
 (select sal from emp where deptno=30)

(4)查询不是老板的员工信息

 select * from emp where emptno not in 
 (select mgr from emp where mgr is not null)

4.不可以在order by 后面使用 子查询
5.主查询和子查询可以不是一张表,只有子查询返回的结果,主查询可以使用即可。
6.一般先执行子查询,再 执行主查询。但相关的子查询例外
7.单行子查询只能使用单行操作符,多行子查询只能使用多行操作符。

rownum 行号 永远按照默认的顺序生成,只能使用 < ,<=
(1)找到员工表中最高 的前三名

select rownum,sal from (select * from emp order by  desc )
where rownum >3; 

(2)找到员工表中薪水大于本部门平均薪水的员工

  select e.empno,e.ename,e.sal,d.avgsal
  from emp e,
 (select deptno avg(sal) avgsal from emp group by deptno) d
  where e.deptno=d.deptno and e.sal >d.avgsal     

修改表
alter table user add photo blob,modify tname varchar2(33),drop column photo,rename column tname to username,rename user to user2(新增列,修改列,删除列,重名列,重名表)

on delete set null 将子表的相关依赖记录的外键值为null

视图 ##(grant授权)

是虚表,封装了一条复杂的查询语句,简化复杂的查询。

create view   viewqq
  as 
  select * from where deptno =10;

序列(数组—》内存中) 可以提高访问的效率
索引 是由于加速数据存取的数据对象(经常被访问的而且数据量很大)
)(rowid行地址(目录))
单列索引
复合索引

create index myindex on emp(deptno);

PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)。PL/SQL是Oracle数据库对SQL语句的扩展。在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL把数据操作和查询语句组织在PL/SQL代码的过程性单元中,通过逻辑判断、循环等操作实现复杂的功能或者计算。PL/SQL 只有 Oracle 数据库有。

存储过程和存储函数
指存储在数据库中提供所有用户程序调用的子程序叫存储过程,存储函数。
过程和函数的区别是函数有一个返回值,过程没有返回值
但函数和过程都可以通过out指定一个或多个输出参数。我们可以利用out 参数,在过程和函数中实现多个返回值。
(1) 无参数

 create or replace procedure hello
 as 
 --说明部分
 begin
   dbms_output.put_line('ass');
 end;

(2) 输入参数(指定员工涨100 ,并且打印涨前和涨后的工资)

create or replace procedure hello(eno in number)
is 
 psal emp.sal%type;
begin
   --涨前的工资
 select sal into psal from emp where empno=eno;
  --涨后的工资
  update emp set sal=sal+100 where empno=eno;
  dbms_output.put_line('涨前:'|| psal || '涨后:'    ||(psal+100);
  end;

调用存储过程的方法


 1. exec  hello(7839);
 2. begin 
        hello(7839);
    end;

如果只有一个返回值,就用存储函数,否则就用存储过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值