Oracle 知识详解
来源:互联网 发布:javascript接受数据 编辑:程序博客网 时间:2024/09/21 09:19
1.cmd->sqlplus 用户名/密码 sqlplus scott/tiger
sqlplus / as sysdba; 使用sys用户登录
show user; 显示当前是以哪个用户登录的。
客户端访问服务器: sqlplus scott/tiger@test
2.isqlplus 通过浏览器去访问
http://ip地址:5560/isqlplus
http://ip地址:1158/em
超级用户的比较 :
1 SYS 用户:Oracle中的一个超级用户,主要用来维护系统信息和管理实例
2 SYSTEM用户:Oracle中默认的系统管理员,他拥有DBA权限,通过SYSTEM用户来管理Oracle数据库的用户、权限、存储
3 SCOTT用户:Oracle数据库的一个示范账户,在数据库安装时创建
数据库安全三要素:账户、权限、角色
用户权限——>用户模式 system——>system模式
模式 是用户所拥有的的所有数据库对象的集合
scott用户忘记解锁?1.先以sys用户登录:sqlplus / as sysdba;
2:解锁scott用户:alter user scott account unlock identified by tiger;
3:锁定scott用户:alter user scott account lock; 锁住scott用户
切换用户:
conn scott/tiger;
select * from emp;
创建用户:
登录的用户要求必须要有创建用户的权限
conn / as sysdba; conn system/manager;
创建用户语法:
create user 用户名 identified by 密码;
给新创建的用户授权: (授予创建会话的权利 create session)
grant create session to 用户名;
CREATE user 自定义用户名IDENTIFIED BY 密码DEFAULT TABLESPACE 表空间名(TESTSPACE)TEMPORARY TABLESPACE 临时表空间名(TEMPSPACE)QUOTA 空间大小 ON 表空间名授予create session权限:
create table to 用户 授权建表
Grant create session to 用户 可以登录访问
Alter user 用户 identified bu **** 修改密码
其他权限: create tablespace ; create user;create session;create table;create role;create view;create procedure;create sequence;
create any table 超越任何模式可以在任何模式中进行create table
收回权限:
revoke create session,create table from test; test为用户名
select * from user_sys_privs 查看用户权限
删除用户 :
Drop USER *** 删除用户
drop user *** cascade 连同创建的表一同删掉
角色管理:为方便管理 创建角色(权限的集合)
DBA 角色所有权限 ——>system
1. 定义角色 create role +角色名
2.为角色赋权 grant create session ,create table to +角色名
3.角色赋给新用户 grant 角色名(可多个)to 用户名
4.删除角色 drop role +角色名
安全性 :
Test模式 ——>sa模式 Test访问sa中的表
www表 demo表
1.授权 select insert update delete execute(存储过程/函数)
grant 权限列表 on 对象 to 账户
sa登录 grant select ,insert on demo to Test;
Test登录 select * from sa.demo;
添加delete 权限 delete from sa.demo where value =‘ ’;
2.收权
rovoke 权限列表 on 对象 from 账户
scott自带三张表:emp dept salgradeOracle和MySQL的区别:
Oracle: sysdate
Mysql: now()
字符串区分大小写
SQL语句
java JDBC(SQL) ORACLE
DDL:定义 create drop alter
DML操作 insert update delete
DCL控制 grant revoke
三级模式结构
概念模式 ---------------逻辑模式 -----------逻辑层 -------------基表
外模式 ---------------用户模式-------------视图层--------------视图
内模式 ---------------存储模式-------------物理层--------------存储文件
SQL不区分大小写 但值区分字符和日期类型的大小写查询所有员工的姓名,工作,直接领导的编号及所属部门编号
select ename,job,mgr,deptno from emp;
空值的特殊之处:
1、参与数学运算的操作数只要有一个为空,则结果为空 nvl函数可以解决
2.not in 的范围之内不允许出现null ,否则没有任何结果
3. null不等于null is null
双引号使用的地方:
1.用在给列其别名的时候。 如果别名中有空格或别名要区分大小写,则使用双引号
distinct必须紧跟在select关键字之后使用,并且对seleect中所有要查询的列都起作用
Oracle特有:
系统函数
dual 虚表
rownum 伪列 分页的时候使用
“||”将SQL任意两个基本数据类型的数据连接起来转换为字符串
select ename l| '的工资是:'|| sal from emp;
set linesize 200;设置行宽 set pagesize 150;一页显示的行数要求查询结果显示的格式为:”姓名:xxx,员工编号:xxx,直接领导编号:xxx,工作:xxx, 工资:xxx”
表的字段可以是中文,但记录名、域名要写为中文 oracle 字符串和字符都是' '
where限定条件查询
查询所有20部门的员工信息
select * from emp where deptno = 20;
查询所有薪水超过1000的员工信息
查询所有薪水在1000-3000之间的员工信息
条件链接需要用的关键字: and or and关键字优先级高于all
select * from emp where sal >= 1000 and sal <= 3000;
查询所有薪水小于1000或超过4000的员工信息
select * from emp where sal < 1000 or sal > 4000;
查询在20部门的所有工作为CLERK的员工信息
查询20部门或30部门的所有员工信息
查询不在20部门的员工信息
查询薪水为1000时对应的薪水等级
select grade from salgrade where losal <= 1000 and hisal >= 1000;
查询薪水在1000-3000之间的员工信息
select * from emp where sal between 1000 and 3000; 闭区间[1000,3000]
查询薪水不在1000-3000之间的员工信息 not:取反
select * from emp where sal < 1000 or sal > 3000;
select * from emp where sal not between 1000 and 3000;
查询在20部门或30部门的员工信息:in
select * from emp where deptno = 20 or deptno = 30;
select * from emp where deptno in(20,30);
查询不在20部门也不在30部门的员工信息:not in
select * from emp where deptno not in(20,30);
查询员工姓名为WARD、ALLEN、FORD、JAMES的员工信息
模糊查询:like WARD
%:代表0-多个字符
_ :代表一个字符
查询所有员工姓名中第二个字符为A的员工信息
select * from emp where ename like '_A%';
查询员工姓名中包含E的员工信息
select * from emp where ename like '%E%';
查询20部门员工姓名中包含E的员工信息
查询20部门员工,职位为'MANAGER',姓名中包含E的员工信息
查询20部门员工,职位为'MANAGER',姓名的第二个字符为A,最后一个字符为E的员工信息
查询20部门员工,职位为'MANAGER',姓名的包含S并且包含E的员工信息
select * from emp where deptno = 20 and job = 'MANAGER' and ename like '%E%S%'; X
select * from emp where deptno = 20 and job = 'MANAGER' and ename like '%S%E%'; X
select * from emp where deptno = 20 and job = 'MANAGER' and ename like '%S%E%' or ename like '%E%S%'; X
select * from emp where deptno = 20 and job = 'MANAGER' and ename like '%E%S%' or ename like '%S%E%'; X
select * from emp where deptno = 20 and job = 'MANAGER' and (ename like '%E%S%' or ename like '%S%E%');
select * from emp where deptno = 20 and job = 'MANAGER' and ename like '%E%' and ename like '%S%';
is null 判断是否为null
查询所有没有奖金的员工信息
select * from emp where comm is null ;
select * from emp where comm = null ; X
select *|table_name from user_tables;
select 列1,列2... from 表名;
distinct :1.只能出现在select之后, select distinct 列1,列2.... 2.
运算符: + .... between ... and ... in(值1,值2,....) is null like %_ not and or
where
3 SELECT 列1 别名,...
1 FROM 表
2 WHERE 条件
select empno no from emp where no = 7788; X
dual sysdate select sysdate from dual;
null
""
转换:
Java: 12 + "34" = 1234
Oracle 12 + '34' = 46
select * from emp where hiredate > '2-2月-12';
select * from emp where deptno = '20';
select to_number('$1,123.45','$9,999.99') from dual;
select ename,sal*12+nvl(comm,0) from emp;
select ename,sal*12+nvl2(comm,comm,0) from emp;
select ename,nvl2(sal*12+comm,sal*12+comm,sal*12) from emp;
select ename,coalesce(sal*12+comm,sal*12,comm) from emp;
decode
decode(字段名,可能出现的值1,替代的值1,可能出现的值2,替代的值2,可能出现的值3,替代的值3,可能出现的值4,替代的值4,...替代的值)
select decode(grade,6,'混的挺牛的',5,'混的还不错',4,'混的还凑合','加把劲') from salgrade;
查询所有员工的姓名、年实际收入,并且添加备注信息表明是年薪还是年薪加奖金
select ename,sal*12+nvl(comm,0),nvl2(comm,'年薪加奖金','年薪') from emp;
count:统计的是非空的个数
avg(comm) = sum(comm)/count(coumm)
注意:count 、avg 需要特别注意空值 和nvl函数联用
统计emp表中记录的个数:
select count(nvl(comm,0)) from emp;
统计emp表中平均奖金?
select avg(nvl(comm,0)) from emp;
18636931656
823403671
日期函数:
1.Sysdate获取当前系统时间
select Sysdate from dual ;
2.Months_between()计算两个时间相差的月
select ename,months_between(Sysdate,hirdate) from emp
3.round()对时间的截断,并进行四舍五入
select sysdate,round(sysdate,'year')from dual;
select sysdate,round(sysdate,'month')from dual;
select sysdate,round(sysdate,'day')from dual;
4.对日期截断,但不进行四舍五入
select sysdate,trunc(sysdate,'year')from dual;
时间格式:
查询公司中最晚入职的员工的入职时间,入职时间的格式为"yyyy年mm月dd日"
select to_char(max(HIREDATE),'yyyy"年"mm"月"dd"日"') from emp
查找81年入职的员工 trunc(日期,'year')对日期截断
select * from emp where trunc(hiredate,'year')='1-1月-81';5 select
1 from
2 where
3 group by
4 having
6 order by
通用函数
1.nvl函数
nvl(a,b):如果a为空,则用b替换
nvl2函数
nvl2(a,b,c):如果a为空,则返回c,否则返回b
双引号的适用范围:
1.起别名的时候
2.'yyyy"年"mm"月"dd“日”‘
分组函数:出现在select后面的列并且没有出现在分组函数中,则必须出现在group by后面select deptno,ename,max(sal) from emp group by deptno; X
select deptno,ename,max(sal) from emp group by deptno,ename; 不是我们想要的结果
查询部门编号及对应的平均薪水,要求只有平均薪水超过2000的才显示出来
select deptno,avg(sal) from emp group by deptno having avg(sal) >= 2000;
where和having的区别:
where后面跟非分组函数,having后面跟分组函数
把员工按部门分组,求最高薪水,部门号,并且过滤掉员工姓名中第二个字母是A的,要求分组后的平均薪水大于1500,
按照部门编号倒序排列
按照部门分组统计,求最高薪水,平均薪水,要求:只有薪水超过1200的员工才能参与统计,
并且分组结果中只包括平均薪水在1500以上的部门,并且按照平均薪水倒序排列
查询所有员工的姓名,要求所有员工的姓名显示成小写,雇用日期显示为"yyyy-mm-dd"这种格式,
薪水转换成'$99,999.999'这种格式
排序:order by asc desc
系统函数:
单行函数:nvl to_char to_number to_date
分组函数:max() min() count() sum() avg()分组查询: group by having
查询公司内薪水最高的员工信息?
1. 先查询薪水最高是多少? 5000
select max(sal) from emp;
2.查询薪水是5000的员工信息
select * from emp where sal = 5000;
select * from emp where sal = (select max(sal) from emp);
查询与SMITH工作相同的员工的姓名。
1. SMITH工作 CLERK select job from emp where ename = 'SMITH';
2.查询工作是CLERK并且姓名不是SIMTH的员工信息
select * from emp where job = 'CLERk' and ename != 'SMITH';
select * from emp where job = (select job from emp where ename = 'SMITH') and ename != 'SMITH';
查询和FORD在一个部门的员工姓名(不包含FORD):
1.FORD的部门编号 select deptno from emp where ename = 'FORD';
2.查询20部门的员工姓名 select ename from emp where deptno = 20 and ename != 'FORD';
select ename from emp where deptno = (select deptno from emp where ename = 'FORD') and ename != 'FORD';
查询每个部门中工资最高的员工信息:
1.查询每个部门中的最高工资 select max(sal) from emp group by deptno;
2.查询工资是2850,3000,500的员工信息:select * from emp where sal in(2850,3000,5000); X
select * from emp where (deptno,sal) in(select deptno,max(sal) from emp group by deptno);
查询平均工资低于20部门的部门号:
1.查询20部门的平均工资
select avg(sal) from emp where deptno = 20;
2. 查询平均工资低于2175的部门号
select deptno,avg(sal) from emp group by deptno having avg(sal) < 2175;
查询平均工资低于20部门的部门名称:
1.查询20部门的平均工资
select avg(sal) from emp where deptno = 20;
2. 查询平均工资低于2175的部门号 30
select deptno from emp group by deptno having avg(sal) < 2175;
3. 查询部门编号是30的部门名称
select dname from dept where deptno = (select deptno from emp group by deptno having avg(sal) < (select avg(sal) from emp where deptno = 20));
查询领导的姓名
1.查询领导的编号 select distinct mgr from emp;
2.查询编号是xxxx的姓名 select ename from emp where empno in(select distinct mgr from emp);
查询不是领导的员工信息:
1.查询领导的编号 select distinct mgr from emp where mgr is not null;
2.查询员工编号不再xxx的员工信息 select * from emp where empno not in (select distinct mgr from emp where mgr is not null);
查询平均工资低于ANALYST(工作岗位)的工作岗位名称
1.查询ANALYST的平均工资
select avg(sal) from emp where job = 'ANALYST';
2.查询平均工资低于3000的工作岗位名称 select job from emp group by job having avg(sal) < 3000;
select job from emp group by job having avg(sal) < (select avg(sal) from emp where job = 'ANALYST');
查询和KING或JONES在一个部门的员工姓名:
1.查询KING或JONES的部门编号: select deptno from emp where ename in ('KING','JONES');
2.查询在10部门或20部门的员工姓名 select ename from emp where deptno in (10,20);
select ename from emp where deptno in (select deptno from emp where ename in ('KING','JONES'));
查询工资比ALLEN的工资不少,但是比SCOTT要不多的员工的姓名:
1.查询ALLEN的工资 select sal from emp where ename = 'ALLEN'; 1600
2.查询SCOTT的工资 select sal from emp where ename = 'SCOTT'; 3000
3.查询工资在1600-3000范围之内的员工姓名 select ename from emp where sal between 1600 and 3000;
select ename from emp where sal between (select sal from emp where ename = 'ALLEN') and (select sal from emp where ename = 'SCOTT');
查询20部门员工姓名中包含E的员工信息
select * from (select * from emp where deptno = 20) where ename like '%E%';
select * from emp where deptno = 20 and ename like '%E%';
查询10号部门员工在整个公司中所占的比例(23.33%)
1.查找10号部门员工的个数 select count(*) from emp where deptno = 10; 3
2.查询工资中员工的个数 select count(*) from emp; 14
3.查询3/14的值 select 3/14*100||'%' from dual;
select (select count(*) from emp where deptno = 10)/(select count(*) from emp)*100||'%' from dual;
把员工按部门分组,求最高薪水,部门号,并且过滤掉员工姓名第二个字母是A的,要求分组后的平均薪水大于2000;
select deptno,max(sal) from emp where ename not like '_A%' group by deptno having avg(sal)>2000;
按照部门分组统计,求最高薪水,平均薪水,要求:只有薪水超过1200的员工才能参与统计,并且分组结果select detno,max(sal),avg(sal) from emp where sal>1200 group by deptno having avg(sal)>
子查询:
分类:按返回结果的个数 :单行子查询 多行子查询 子查询返回结果的行数
按执行机制区分:关联子查询 和非关联子查询
非关联查询 :内外层查询语句没联系,先执行内层后执行外层
相关子查询 :内外层有关联,先得到外层查询的一条记录,根据该记录去查询内层查询
相关子查询:查询工资大于该部门平均工资的员工信息:
select * from emp e where sal > (select avg(sal) from emp e1 where e.deptno = e1.deptno);
查询各岗位中工资等于该岗位平均工资的员工信息
查询各部门中收入大于该部门平均收入的员工信息
查询所有员工的姓名、所属部门名称及薪水等级
select ename,dname,grade from emp e
join dept d on e.deptno = d.deptno
join salgrade s on e.sal between s.losal and s.hisal;
查询薪水是5000的员工的编号,姓名,所属部门名称
查询所有员工的员工编号,员工姓名,薪水,薪水等级
查询20部门所有员工的员工编号,员工姓名,薪水,薪水等级
查询所有员工的编号,姓名,薪水,薪水等级及所属部门名称
查询工资比ALLEN的工资不少,但是比SCOTT要不多的员工的姓名、部门编号、部门名称及薪水等级:
select
from
join on
where
group by
having
order by
select a.ename employee,a.mgr,b.empno,b.ename leader from emp a
join emp b on a.mgr = b.empno;
外连接:
左外连接:select * from emp e left join dept d on e.deptno = d.deptno;
右外连接:select * from emp e right join dept d on e.deptno = d.deptno;
全外连接:select * from emp e all join dept d on e.deptno = d.deptno;
分页:
Oracle特有: dual rownum 伪列
查询前5条记录: select * from emp where rownum < 6;
查询第二页记录(每页显示5条):select * from emp where rownum > 5 and rownum < 11; X
rownum:只能使用 < <= 不能使用 > >=
select rownum,e.* from (
select rownum rn,emp.* from emp where rownum < 11
) e where rn >5;
先按照薪水升序排列,然后取第二页:
1.排序 select * from emp order by sal;
2.去前10条记录:
select rownum rn,e.* from (select * from emp order by sal) e where rownum < 11;
3.去后5条记录:
select rownum,a.* from (
select rownum rn,e.* from (select * from emp order by sal) e where rownum < 11) a
where rn > 5;
统计平均薪水最高的部门名称
查询平均薪水等级最低的部门名称
1 查询部门平均薪水等级
select deptno,avg(grade) avg_grade from (select * from emp e join salgrade s on e.sal between losal and hisol);
2查询平均薪水等级最低的值
select min(avg_grade) from (select deptno,avg(grade) avg_grade from (select * from emp e join salgrade s on e.sal betweenlosal and hisol)));
3查询平均薪水最低的部门编号
select deptno from (select deptno,avg(grade) avg_grade from(select * from emp e join salgrade s on e.sal between losal and hisol)))
4.查询军薪水等级最低I的部门名称
select dname from dept where deptno = (select deptno from -----------------)
查询比普通员工的最高薪水还要高的领导者姓名及所属部门名称及薪水等级
查询平均薪水最低的部门名称,要求:只有领导才参加统计表1 表2
1,2 3,4
1,3 2,3 1,4 2,4
6SELECT
1FROM
2JOIN ON JOIN ON
3WHERE
4GROUP BY
5HAVING
7ORDER BY
分组查询 子查询 连接查询
把雇员按部门分组,求最高薪水,部门号 要求过滤掉名字中第二个字母是'A'的员工,
并且部门的平均薪水 > 3000,按照部门编号倒序排列
求工作职位是'manager'的员工姓名,部门名称和薪水等级
按照部门分组统计,求最高薪水,平均薪水,最低薪水,只有薪水是1200以上的员工才参与统计,
并且分组结果中只包含平均薪水在1500以上的部门,并且按照平均薪水倒序排列
求薪水最高的员工姓名
查询各部门平均薪水等级,并且按平均薪水等级的降序排列.
查询所有员工姓名以S或s开头的所有员工信息
查询所有工作时间超过一年的员工编号,姓名及入职时间,要求雇用时间的格式为'yyyy年mm月dd日'
查询20部门的所有员工的员工姓名,实际收入
查询10部门工资大于3000的员工信息,要求按员工的入职时间由前到后排序
查询10部门或20部门的所有员工的姓名,并截取前三位,按员工姓名升序排列
查询前5条记录:
查询第六条到第十条记录:
先按照sal降序排列,然后取前五条记录:
先按照sal降序排列,然后得到第六条到第十条记录:
shu
创建表的语法:
create table 学生(
字段名1 类型,
字段名2 类型,
.....
);
设置默认值: default 值
create table 学生(
字段名1 类型 default 值1,
字段名2 类型,
.....
);
类型:char varchar2 number date
约束:列级约束 表级约束
5种约束:非空约束、唯一约束、主键约束、外键约束、受检查约束
非空 not null
create table student(
stu_no char(5),
stu_name varchar2(20) not null, --非空约束 只能使用列级约束
age number(2)
);
唯一约束:
列级约束:
create table student(
stu_no char(5),
stu_name varchar2(20) unique, --约束 只能使用列级约束
age number(2)
);
表级约束:
create table student(
stu_no char(5),
stu_name varchar2(20),
age number(2),
constraint student_uk unique(stu_name,age)
);
约束名字: 表名_uk_stu_name
列级约束vs表级约束:
列级约束只能约束一列,表级约束可以约束多列
主键约束: 非空约束+唯一约束 primary key 实体完整性
create table student(
stu_no char(5) primary key,
stu_name varchar2(20),
age number(2)
);
主属性:stu_no
外键约束: 参照完整性
例:在学生管理系统中,需要一张学生成绩表,该表中记录了学生的学号、学科以及成绩。
Create table student(
s_id number(3) primary key,
s_name varchar2(20)
); 主键表
Create table stu_grade(
s_id number(3) references student(s_id),
stu_course_name varchar2(30),
c_grade number(3,2)
); 外键表
Create table stu_grade(
s_id number(3) ,
stu_course_name varchar2(30),
c_grade number(3,2),
constraint s_g_fk foreign key(s_id) references student(s_id)
);
check 用户自定义完整性
create table temp as select * from emp;
如果temp表只想要emp表的结构,怎么办?
create table temp as select * from emp where 1 != 1;
insert into temp select * from emp where deptno = 20;
实体完整性、参照完整性和用户自定义完整性
数据库设计:
一般都要有id primary key
1NF:
学号、姓名、课程名、课程号、成绩
不满足1NF:
create table student(info varchar2(100));
insert into student values('1,张三,Oracle,1,93');
满足1NF 不满足2NF:
create table student(stu_no char(5) primary key ,stu_name varchar2(20),cou_no char(5),cou_name varchr2(50),grade number(5,2));
2NF: 消除函数依赖 每个字段都和主属性之间有依赖关系
满足2NF 不满足3NF:
create table student(
stu_no char(5) primary key ,
stu_name varchar2(20),
cou_no char(5),
grade number(5,2)
);
create table course(cou_no char(5) primary key,cou_name varchr2(50));
3NF: 表和表之间的关系
表和表之间的关系是多对多 需要创建第三张表来确定关系
create table student(
stu_no char(5) primary key ,
stu_name varchar2(20)
);
create table course(cou_no char(5) primary key,cou_name varchr2(50));
create table grade(stu_no char(5),cou_no char(5),grade number(5,2));
create table user(
id
name
gender
age
nickname...
);
create table friend(
id
user_id wo
friend_id zhangsan
);
insert into friend(1,'1','2');
insert into friend(2,'2','1');
表和表之间是1对1,1对多,多对1 不需要第三张表
emp dept dept emp
create table emp( deptno);
create table dept( );
1.创建视图:
create view emp_view as select empno,ename,job,mgr,hiredate,deptno from emp;
2.创建用户:
create user zhangsan1 identified by zhangsan1;
3. 授权
grant create session to zhangsan1;
grant select on scott.emp_view to zhangsan1;
创建序列:
create sequence student_sequece start with 1 increment by 1;
select student_sequece.nextval from dual; 得到序列的下一个值
select student_sequece.currval from dual; 得到序列的当前值
create table student(id number primary key,name varchar2(20));
insert into student values(student_sequece.nextval,'zhangsan');
drop table card;
create table card(id number(5),name varchar2(20),money number(10,2));
insert into card values(111,'公司',1000000);
insert into card values(1,'li',0);
发工资
1. 公司的卡减少
update card set money = money - 1000 where id = 111;
2.我的卡里增加钱
update card set money = money + 1000 where id = 1;
PL/SQL 查询某个员工的工资,如果低于2000,则提高至2000 if else end if
set serveroutput on; -打开输出语句
begin --执行语句
dbms_output.put_line('hello world');--输出
end;
/
块 不能重复使用 没有参数 没有返回值
declare --定义变量
begin --执行部分
exception --异常
end;
查询员工编号是7788的用户,并输出姓名和薪水
declare
v_name varchar2(20);
v_sal emp.sal%type;
begin
select ename,sal into v_name,v_sal from emp where empno = 7788;
dbms_output.put_line('姓名:'||v_name||'薪水:'||v_sal);
end;
改进:
declare
v_name varchar2(20);
v_sal emp.sal%type;
begin
select ename,sal into v_name,v_sal from emp where empno = &no;
dbms_output.put_line('姓名:'||v_name||'薪水:'||v_sal);
exception
when no_data_found then
dbms_output.put_line('没有找到对应记录');
end;
根据输入的员工编号输出薪水的范围 1000-2000 2000-3500 范围不确定
分支语句:
if 条件 then
xxx
elsif 条件 then
xxx
else
xxx
end if;
declare
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno = &no;
if v_sal >= 1000 and v_sal < 2000 then
dbms_output.put_line('薪水在1000-2000之间');
elsif v_sal >= 2000 and v_sal < 3000 then
dbms_output.put_line('薪水在2000-3000之间');
else
dbms_output.put_line('薪水范围不确定');
end if;
exception
when no_data_found then
dbms_output.put_line('员工不存在');
end;
定义变量: 变量名 类型 [:= 值1];
过程 有名字 有参数 没有返回值 可以重复使用
create procedure 过程名(参数名 参数类型,xxx) is
定义变量
begin
end;
create procedure he (no emp.empno%type) is
begin
dbms_output.put_line(no);
end;
查询用户输入的员工编号,如果该员工的薪水小于1000,则将薪水更新至1000;
create procedure hee(no emp.empno%type) is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno = no;
if(v_sal < 1000) then
update emp set sal = 1000 where empno = no;
dbms_output.put_line('更新成功');
end if;
exception
when no_data_found then
dbms_output.put_line('没有对应记录');
end;
调用: call 过程名(参数);
函数 可以重复使用 有参数 有返回值
create function 函数名(参数) return 返回值类型 is
定义变量
begin
end;
create function sal1(no emp.empno%type) return emp.sal%type is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno = no;
return v_sal;
exception
when no_data_found then
dbms_output.put_line('没有对应记录');
end;
1.定义变量 var 变量名 类型;
2.调用函数,并且保存返回值
call 函数名(参数) into:变量名;
3.打印变量的值: print 变量名;
MYSQL: root
database对象 表名
show databases:查看所有的database对象
create database 名字;
use database名字;
show tables; 查看当前database对象要有哪些表
- Oracle 知识详解
- Oracle 知识详解
- Oracle知识原理详解
- 数据库知识:Oracle服务的知识详解
- Oracle数据库服务的相关知识详解
- Oracle-SQL知识详解(一)
- Oracle-SQL知识详解(二)
- Oracle-SQL知识详解(三)
- oracle知识
- oracle知识
- Oracle知识
- oracle知识
- Oracle 知识
- oracle知识
- oracle 知识
- Oracle知识
- 知识储备:Handler知识详解
- Oracle数据库服务的知识详解(一些非关键服务可关闭)
- JNI 之二 :java & c/c++ 相互通信及调用
- Java程序员从笨鸟到菜鸟之(十二)java异常处理机制
- JAVA学习路线及推荐书籍
- linux 统计 程序运行时间
- i9000刷ubuntu touch
- Oracle 知识详解
- 黑马程序员----JAVA基础之面向对象
- IT名企面试:IBM笔试题
- Java程序员从笨鸟到菜鸟之(十三)java网络通信编程
- Miracast Wifi Display Standard
- libjpeg库的简单使用使用----jpeg图片解压
- 括号匹配
- 腾讯笔试题答案
- 【转】【Google官方教程】前言:高效的Bitmap显示