【Oracle数据库】期末复习DAY3

写在前面:

本来今天的计划是继续看计组的,但是太让人头疼了。

先把前几天做的Oracle数据库的题看一看,然后再把计组第三单元复习完,练一练题,夜晚看看TCP/IP,差不多这样就足够了。

                                              

 


Oracle数据库知识点

1.与ORACLE相关的数据库

MySql 小型数据库 (免费,开源)
Sqlserver 中大型数据库
Oracle 大型数据库
Db2(IBM公司)

2.连接ORACLE数据库

 3.ORACLE常用命令

查询员工表
select * from scott.emp;(注意分号)


查询名字为SCOTT的员工信息
select * from scott.emp where ename = 'SCOTT';


查询员工scott创建的表
select * from scott.dept;


在员工表插入一个员工数据
insert into scott.emp(empno,ename) values(800,'OCEANMIX');


删除某个员工信息
delete from scott.emp where ename = 'OCEANMIX';


创建一个表(Oracle中没有自增列)
create table UserInfo(
id int primary key,
ename nvarchar2(64)
);


//向表中添加数据
insert into userinfo values(1,'oceanmix');


//删除表
drop table userinfo;
 

//修改员工某项数据
update scott.emp set sal ='5000' where ename = 'OCEANMIX';


//输入ed;创建表(decimal(3,2)表示总共3位,小数占据2位)
create table emp
(
id int primary key,
ename nvarchar2(64) default 'test',
money decimal(3,2)
)
 

查看当前登陆的用户 show user
清除屏幕命令 clear
登陆命令 conn 用户名/密码
解锁命令 alter user scott account unlock;
修改别人密码命令 alter user scott identified by 123456;
修改自己密码命令 password(最后还需输入commit;确认提交、适用于CMD窗口中)
创建用户命令 create user cz identified by 123456;
删除用户命令 drop user 用户名(注意:自己不能删除自己,如果删除当前连接用户需要在菜单Session处的Log off注销掉正在连接的用户)
进入编辑窗口 ed(ok后输入/执行编辑窗口内的命令)
显示错误 show error;
 

4.ORACLE权限

注意:一个新用户是不具有任何权限的

权限授予命令:grant creat/select/delete... to sb;
授予登陆权限:grant create session to cz;
授予查询权限:grant select on scott.emp to cz;
授予删除权限:grant delete on scott.emp to cz;
一次性权限授予:grant insert,delete,update,select on scott.emp to cz;
建表权限:grant create table to zz;
对表空间进行操作的权限:grant unlimited tablespace to zz;

权限回收命令:revoke select/insert/delete/update on sp from sb;
回收cz用户对于scott.emp表的查询权限:revoke select on scott.emp from cz;
回收cz用户对于scott.emp表的增删改查四项权限:revoke insert,delete,update,select on scott.emp from cz;
 

5.权限的传递

权限传递方式:
1.with admin option权限不能被级联取消
2.with grant option权限会被级联取消

注意:默认情况下权限是不能进行传递的

system将授予他人登录权限(以授予cz为例):grant create session to cz with admin option;(with admin option权限不能被级联取消)

(cz授予zz登录权限):grant create session to zz

(system回收cz的登录权限):revoke create session from cz

(system将授予他人查询scott.emp表权限授予cz):grant select on scott.emp to cz with grant option;(with grant option权限会被级联取消)

(system回收cz的授予他人查询scott.emp表权限):revoke select on scott.emp from cz;

6.权限的分类

1.系统权限(登录、创建表、创建表空间、创建视图)-->代表着大的范围
2.对象权限(针对某一个特定对象下面的细分权限)-->代表着小的范围

7.角色授权

1.系统的三个重要角色:(Connection、Resource、dba),其中dba的权限等于system
2.相关查询:(SQL Window处查询、F8查询、注意:在SQLWindow中查询时,需要将查询条件更改为英文大写)

--查询所有的角色
select * from dba_roles;


--查询指定角色拥有的系统权限
select * from dba_sys_privs where grantee= '角色名';


--查询指定角色是否存在于角色表
select * from dba_roles where role ='角色名'

--查询指定角色的对象权限
select * from dba_tab_privs where grantee= '角色名';


--查询隶属指定角色的角色
select * from role_role_privs where role='角色名';

例题:

 

8.Oracle中Char、Varchar2、Nvarchar2的问题

9.视图

1.根据视图来操作一张表的增删改查是可行的(但前提是必须拥有操作该表的权限,如果不是自己创建的表,需要授权)
2.视图可以存放多张表的查询结果,这是同义词不具备的
3.视图可以是只读的(不允许增删改)

命令:
--查询所有的视图
select * from dba_views;


--查询指定的视图
select * from dba_views where view_name='视图名';


--查询指定用户创建的视图
select * from dba_views where owner ='用户名';


--创建一个视图
create view view_emp
as
select * from scott.emp


--创建只读视图
create view view_emp
as
select * from scott.emp
with read only


--如果创建的视图已经存在(即视图名重复,使用如下代码解决)-->有创建视图的作用
create or replace view view_emp
as
select * from scott.emp
with read only

--创建存放多张表查询结果的只读视图
create view view_emp
as
select e.ename,d.dname from scott.emp e join scott.dept d on e.deptno = d.deptno
with read only

10.序列

--查询Oracle的默认序列
select * from dba_sequences;


--查询Oracle的默认序列前5条数据
select * from dba_sequences where rownum<=5;


--查询自定义序列
select mysequ.nextval from dual;


--创建序列
create sequence mysequ
minvalue 1 --最小值
increment by 1 --每次增长数
nomaxvalue --没有最大值
nocache
nocycle


--修改序列
alter sequence mysequ
minvalue 1 --最小值
increment by 1 --每次增长数
maxvalue 100 --有最大值
nocache
cycle --超出最大值后从1重新开始


--删除序列
drop sequence mysequ;


--序列在表中的使用
insert into scott.emp(EMPNO,ENAME) values(mysequ.nextval,'OCEANMIX');


11.表空间

1.Oracle数据表存储在表空间内、新创建的用户是没有权限操作表空间的
2.临时表空间主要是做排序、索引之类的操作,避免操作表空间(表空间数据量大,操作时间较长),临时表空间在使用完后立即释放
3.执行删除表空间命令并没有彻底删除表空间文件(即DBF文件、可能是为了防止误删除和方便恢复数据)

--查询默认的表空间
select * from dba_tablespaces


--查询指定表空间下面的表
select * from dba_tables where tablespace_name='SYSTEM';


--查询数据库中所有用户的默认表空间、临时表空间temporary_tablespace
select USERNAME,DEFAULT_TABLESPACE,TEMPORARY_TABLESPACE from dba_users


--查询数据库中指定用户的默认表空间、临时表空间
select USERNAME,DEFAULT_TABLESPACE,TEMPORARY_TABLESPACE from dba_users where username='SCOTT'


--修改指定用户的默认表空间为users
alter user cz default tablespace users;


--授予指定用户操作表空间的权限
grant unlimited tablespace to cz;


--查询默认表空间存储位置
select * from dba_data_files;


--创建表空间
create tablespace OWSLA --表空间名字
datafile 'D:\APP\ADMINISTRATOR\ORADATA\ORCL\OWSLA.DBF' --表空间文件路径
size 20m --表空间大小


--SQLServer中将Test表的数据全部复制到Test2(表数据转移)
select * into Test2 from Test


--删除非空表空间(即该表空间下面还存在表、删除意味着将删除表的数据)
drop tablespace OWSLA INCLUDING CONTENTS;
 

 12.ORACLE系统函数

1.数值函数

--求绝对值
select abs(123) from dual


--进一法(123.1得出124)
select ceil(123.1) from dual


--去尾法(-123.9得出-124、123.9得出123)
select floor(-123.9) from dual


--四舍五入
select round(123.46) from dual

--四舍五入保留2..n位小数
select round(123.44,2) from dual

--数字截取·默认截取整数
select trunc(123.45) from dual


--数字截取·截取小数后几位
select trunc(123.4567,1) from dual


--数字截取·截取小数前几位(得出100)
select trunc(123.4567,-2) from dual

2.字符函数


--转换成大写
select upper('abc') from dual


--转换成小写
select lower('ABC') from dual


--去掉字符中所有空格
select trim(' AbC ') from dual


--去掉左边空格
select ltrim(' AbC ') from dual


--去掉左边指定的字符
select ltrim('abcd','dca') from dual


--去掉右边指定的字符
select rtrim('abcd','dca') from dual


--替换函数
select replace('this is a boy','boy','girl') from dual
select replace('this is a boy',' ','') from dual --去掉字符中的空格


--字符串相加(下面两个例子都得出aabb)
select 'aa'||'bb' from dual
select concat('aa','bb') from dual


--截取字符串的函数(注意:Oracle中0和1都是从第一个字符开始的、后面的数字是截取的位数)
select substr('abcdef',1,3) from dual
select substr('abcdef',2,3) from dual

 
--求长度的函数
select length('a') from dual

3.转换函数

--转化成数字
select to_number('10') from dual


--转化成字符串
select to_char(sysdate) from dual


--转化成字符串,格式化时间
select to_char(sysdate,'yyyy-mm-dd') from dual


--完整的年月日时分秒的时间
select to_char(sysdate,'yyyy-mm-dd hh:Mi:ss') from dual


--时间24小时制
select to_char(sysdate,'yyyy-mm-dd hh24:Mi:ss') from dual

4.处理Null的函数


--为空返回指定的代替,不为空返回自己
select nvl('','b') from dual


--不为空返回参数2为空返回参数3
select nvl2('','b','c') from dual
 

13.ORACLE存储过程

1.优点:
存储过程会预编译sql语句,编译一次后执行不再需要编译,执行效率快一点
可以在存储过程进行sql编码,可以减少服务器压力

2.缺点:代码量多,很难维护

3.命令

--Oracle存储过程输出控制
set serveroutput on;
set serveroutput off;


--1.创建一个输出系统时间的存储过程
create procedure proc_sel
as
begin
dbms_output.put_line(to_char(sysdate,'yyyy-mm-dd'));
end;


--2.定义存储过程求2个数字之和
create procedure proc_sum(p int,p2 int)
as
begin
dbms_output.put_line(p+p2);
end;


--3.定义存储过程求2个数字之和,并提供返回参数
create or replace procedure proc_sum(p int,p2 int,pout out int)
as
begin
pout := p+p2;
end;


--4.创建根据ename查询并返回Job的存储过程
create or replace procedure proc_sel(pename nvarchar2,pout out nvarchar2)
as
begin
select job into pout from scott.emp where ename = pename;
end;


--调用Oracle存储过程
begin
proc_sel;
end;


--调用Oracle存储过程
begin
proc_sum(6,6);
end;


--调用有返回参数的存储过程
declare pr int;
begin
proc_sum(3,6,pr);
dbms_output.put_line(pr);
end;


--调用有返回参数的存储过程
declare pr nvarchar2(64);
begin
proc_sel('SCOTT',pr);
dbms_output.put_line(pr);
end;


--修改存储过程
create or replace procedure proc_sel
as
begin
dbms_output.put_line(to_char(sysdate,'yyyy-mm-dd'));
end;

14.ORACLE游标—>cursor

--定义一个游标(数据源主要是表中字段)
declare cursor cur_sel
--给游标关联数据源
is select ename,sal,job from scott.emp;
v_ename nvarchar2(64);
v_sal int;
v_job nvarchar2(64);
begin
--打开游标(真正去执行关联的sql语句)
open cur_sel;
loop
--读取一行游标,往下读取一行
fetch cur_sel into v_ename,v_sal,v_job;
dbms_output.put_line(v_ename||':'||v_sal||':'||v_job);
exit when cur_sel%notfound;
end loop;
--关闭游标
close cur_sel;
end;


--定义一个游标(数据源主要是一个表)
declare cursor cur_sel
--给游标关联数据源
is select * from scott.emp;
--emp表一行的数据类型
emp_row scott.emp%rowtype;
begin
--打开游标(真正去执行关联的sql语句)
open cur_sel;
loop
--读取一行游标,往下读取一行
fetch cur_sel into emp_row;
dbms_output.put_line(emp_row.empno||':'||emp_row.ename||':'||emp_row.job||':'||emp_row.sal);
exit when cur_sel%notfound;
end loop;
--关闭游标
close cur_sel;
end;


--1.先定义一个用来绑定数据源和打开游标的存储过程
create or replace procedure proc_sel(rcursor out sys_refcursor)
as
begin
open rcursor for select * from scott.emp;
end;


--2.存储过程直接查询一个表的所有数据(借助游标、游标事先存储了一个表的所有数据,所以可以直接输出)
declare rcursor sys_refcursor;
emp_row scott.emp%rowtype;
begin
proc_sel(rcursor);
loop
fetch rcursor into emp_row;
dbms_output.put_line(emp_row.empno||':'||emp_row.ename||':'||emp_row.job||':'||emp_row.sal);
exit when rcursor%notfound;
end loop;
--关闭游标
close rcursor;
end;

 

参考链接

Supongo que te gusta

Origin blog.csdn.net/m0_45863636/article/details/121731067
Recomendado
Clasificación