大型数据库Oracle基础

  • 一、创建数据库和表
  • 二、创建数据库和表
  • 三、表的CRUD操作
  • 四、数据库的查询
  • 五、数据库安全性
  • 六、存储过程的使用
  • 七、数据库综合设计

一、创建数据库和表

1.详细描述Oracle 11g的安装步骤(你在安装过程中是否遇到了困难,如何解决的)。

安装步骤:

第一步:将两个文件一起解压到同一目录下的同一文件夹内, 路径名称中不要出现中文,也不要出现空格等不规则字符。解压完成后,到相应路径下,找到setup.exe,双击运行,安装Oracle 11g。

第二步:配置安全更新。电子邮件可写可不写,然后下一步。

第三步:安装选项。直接选择默认的“创建和配置数据库”,单击下一步。

第四步:系统类,直接选择默认的桌面类,单击下一步

第五步:典型安装。选好目录,输入口令密码。单击下一步

第六步:先决条件检查。 直接下一步

第七步:概要,配置信息可以保存成文件或不保存文件,点击完成

第八步:等待安装


2.安装完oracle 11g,电脑的开机速度会变慢,为什么?怎样解决?

通过以下设置:windows10下,打开计算机管理,找到服务和应用程序下的服务,把oracle开头的所有服务,全部改为“手动”启动


3.oracle 11G这个软件与你以前用过的软件有什么区别(可以从用法,复杂度以及其他方面来表述)

数据持久性:
oracle保证提交的数据均可恢复,因为oracle把提交的sql操作线写入了在线联机日志文件中,保持到了磁盘上,如果出现数据库或主机异常重启,重启后oracle可以考联机在线日志恢复客户提交的数据。

mysql:
默认提交sql语句,但如果更新过程中出现db或主机重启的问题,也许会丢失数据。

用户权限:
mysql的用户权限管理,是放到mysql自动带的一个数据库mysql里面的,而oracle是用户权限是根着表空间走的。

表字段类型:
oracle的表字段类型也没有mysql多,并且有很多不同


4.SQLPLUS的常用命令有哪些,你学会使用了吗?(请举例)

show命令

 show 命令用来显示当前sqlplus环境中的系统变量、错误信息、初始化参数、当前用户信息等

save命令

在SQL*PLUS中可以执行一条或若干条sql命令,它们被存放在缓冲区中,
且缓冲区中只能存放最近刚执行的sql或pl/sql ,(后进的覆盖之前的),
如果要保存以往执行过的语句,要使用save。

get命令

上面说过save命令用来保存,那么get命令就是用来读取的,
将保存后的文件读取到缓冲区中进行显示

edit命令

使用edit命令可以将缓冲区的内容复制到一个文件中 ,默认文件afiedt.buf ,
当然也可以制定一个已经命了名存在的文件

二、创建数据库和表


1、使用DBCA创建数据库(写出大致步骤)

1)打开oracle下的工具DataBase Configuration Assistan

2)创建数据库,选择一般用途,然后填写全局数据库名和SID,然后一直默认下一步,直到出现数据库身份证明窗口,设置所有账户使用的同一口令,然后一直默认下一步,在恢复配置窗口可以勾选启用归档,然后一直默认下一步,直到完成安装即可。


2在ORCL数据库中创建表C表和S和表结构如表1,2所示。

(1)使用SQL语句分别创建表

创建命令:

create table C
(CNO    NUMBER(2) PRIMARY KEY,CNAME VARCHAR2(20),NUM    NUMBER(3));create table S
(SNO    NUMBER(4) PRIMARY KEY,SNAME VARCHAR2(10) UNIQUE,SAGE    NUMBER,SEX  CHAR(2),CNO NUMBER(2));

3.根据下图BOOK,READER,BORROW创建表

Book:

create table BOOK
(NO NUMBER(6) PRIMARY KEY,TITLE VARCHAR2(33),AUTHOR VARCHAR2(12),PUBLISH VARCHAR2(14),PUB_DATE DATE,PRICE NUMBER(2))

Reader:

create table READER
(
RNO NUMBER(6) PRIMARY KEY,
RNAME VARCHAR(4))

Borrow:

CREATE TABLE BORROW
(
NO NUMBER(6),
RNO NUMBER(6),
BORROW_DATE DATE
CONSTRAINT PRIMARY KEY(NO,RNO))

三、表的CRUD操作

请参照实验2的两张表完成以下工作
1.为student表建立一个外键约束,其CNO列参照class表的CNO列

Alter table S add constraint FK_S_C foreign key (CNO)
references C (CNO);

2.为student表sage列添加一个检查约束,保证该列取值在0-100之间

Alter table S add constraint CK_SAGE check(SAGE>=0
and SAGE <=100);

3.为student表的sex列添加一个检查约束,保证该列取值为‘M’或’F‘

Alter table S add constraint CK_SEX check(SEX=’F’ or SEX=’M’);

4.创建一个视图,视图内容是年龄大于19岁的A1721班同学的基本信息。

Create view S_INFO(SNO,SNAME,SAGE,SEX,CNO) as select
S.SNO,S.SNAME,S.SAGE,S.SEX,S.CNO from S,C
Where S.SAGE>19 and C.CNAME=’A1721’;

5.建立一个序列并应用在student的sno列上,并查看序列的当前值和下一个值

Create sequence s_sno_seq
Start with 1
Increment by 1
Nomaxvalue
Nocycle
Cache 10;
Insert into S(SNO) values(s_sno_seq);
Select s_sno_seq.currval from dual;
Select s_sno_seq.nextval from dual;

6.在实验2所建的BOOK表的NO列上建立一个唯一索引

Create unique index NOX_BOOK_UID ON BOOK(NO);

7.建立一个索引,按学生的年龄降序排列

Create index SAGE_INDEX on S(SAGE DESC);

8.根据下面BOOK,READER,BORROW表结构插入下图数据

(1)三张表的数据插入命令(每张表各插入两条记录):
Book:

Insert all
Into book values()
Select 1 from dual;

Reader:

Insert all)
Into reader values()
Select 1 from dual;

Borrow:

Insert all
Into borrow values()
Select 1 from dual;

(2)将图书标号为100007的图书价格改为29

Update BOOK set price=29 where no=100007;

(3)删除价格低于30的图书信息

Delete from book where price<30;

(4)将读者李凤的借书信息删除
Delete from borrow b,reader r where b.rno=r.rno and r.rname=’李凤’;

9.将employees表中员工信息按照不同部门号分别复制到emp10,emp20,emp30和emp_other表中。同时,将工资低于5000元的员工信息复制到lowsal表中,将工资高于10000元的员工信息复制到highsal表中,将工资在5000~10000元之间的员工信息复制到middlesal表中。(employees位于HR用户模式中)

create table lowsal
as
select * from employees where 1=2;
create table middlesal
as
select * from employees where 1=2;
create table highsal
as
select * from employees where 1=2;insert allwhen department_id=10 then into emp10when department_id =20 then into emp20when department_id =30 then into emp30when department_id =40 then into emp_otherwhen salary<5000 then into lowsalwhen salary>10000 then into highsalelse into middlesal
select * from employees;

四、数据库的查询

根据oracle 的HR用户下的EMPLOYEES表及相关表的结构和数据,做以下查询

1.查询20号部门的所有员工信息

  Select * from employees where department_id = 20;

2.查询每个员工的工号,工资,奖金和工资与奖金的和

 select employee_id,salary,salary*commission_pct,salary*(1+nvl(commission_pct,0))from employees;

3.查询奖金高于工资(SAL)的员工信息

 select * from employees where salary*commission_pct>salary and commission_pct is not null;(数据库表中没有符合条件的记录,故显示结果是未选定行)

4.查询奖金高于工资的20%的员工信息

   select * from employees where commission_pct*salary > salary*0.2;

或者

   Select * from employees where commission_pct>0.2;

5.查询员工名(first_name)中不包含字母“S”的员工

Select first_name from employees
where first_name not like ‘%S%’;

6.查询所有职位名称不是stock manager和purchasing manager且工资大于或等于2000元的员工的详细信息

select * from employees where job_id not in(
select job_id from jobs where job_title
in ('Stock Manager','Purchasing Manager')) and salary >= 2000;

7.查询至少有一个员工的部门信息

select * from departments d where
exists(select * from employees
where department_id=d.department_id);

或者

Select d.department_id,d.
department_name,count(employee_id)
from employees e right join
departments d on d.department_id=e.department_id
group by d.department_id,d.department_name,
e.department_id having count(e.employee_id)>=1;

8.查询各个部门中不同职位的最高工资

select department_id,max(salary) from employees
group by department_id having department_id is not null;

查询各个部门的最高工资

Select * from jobs;Select job_id,max(salary) from employees group by job_id;

9.查询各个部门的人数和平均工资

Select department_id,count(*),avg(salary) from employees
group by department_id having department_id is not null;

10.查询最低工资大于5000元的各种工作

Select * from jobs where job_id in(Select job_id from employees group by job_id having min(salary)>5000
);

11.查询与140号员工从事相同工作的所有员工信息

Select * from employees where job_id = (Select job_id from employees where employee_id=140
);

12.查询工资高于30号部门中所有员工的工资的员工姓名和工资

Select first_name,last_name,salary
from employees where salary> (select max(salary)
from employees department_id=30);

或者

Select * from employees where salary >
all (select salary from employees
where department_id=30) and department_id!=30;

13.查询工资高于本部门平均工资的员工的信息

方法一:

Select * from employees e1
where salary>(select avg(salary)
from employees e2 where e2.department_id=e1.department_id);

方法二:

select * from employees e,
(select department_id,avg(salary) avgsal
from employees group by department_id) v
where e.department_id=v.department_id and e.salary>v.avgsal;

14.查询所有员工工资都大于5000元的部门的信息及其员工信息

Select * from employees e join departments d
on d.department_id in (select distinct department_id
from employees where department_id
not in (select distinct department_id
from employees where salary<5000))
and d.department_id=e.department_id;

错误写法:

Select * from employees e join departments d
on d.department_id in (select distinct department_id
from employees where salary>5000
and department_id is not null)
and d.department_id=e.department_id;

15查询各月倒数第2天入职的员工信息

select * from employees where hire_date=last_day(hire_date)-1;

16.查询员工名(first_name)正好为6个字符的员工的信息

Select * from employees where length(first_name)=6;

17.查询工龄大于或等于10年的员工信息

方法一:

Select * from employees where((sysdate-hire_date)/365)>=10;

方法二:

Select * from employees where
trunc(months_between(sysdate,hire_date)/12)>=10;

格式:MONTHS_BETWEEN(DATE1,DATE2) MONTHS_BETWEEN函数返回两个日期之间的月份数

select trunc(sysdate) from dual --2013-01-06 返回的是1今天的日期为2013-01-06;

18.查询人数最多的部门的信息

方法一:

Select * from (select d.* from dept d,
(select department_id,count(*) enum
from employees group by department_id) view
where d.department_id=view.department_id
order by view.enum desc) where rownum=1;

方法二:

Select * from departments where department_id
in(select department_id from employees
group by department_id having count(*)>=all(Select count(*) from employees group by department_id));

19.查询入职日期早于其直接上级领导的员工信息

Select * from employees e1
where e1.hire_date<(select e2.hire_date
from employees e2 where e2.employee_id=e1.manager_id);

20.查询所有部门及其员工信息,包括那些没有员工的部门

Select d.department_id,d.department_name,
e.first_name,e.last_name from departments d
left join employees e on d.department_id=e.department_id;

21.查询工资高于公司平均工资的所有员工信息

select * from employees where salary > (select avg(salary)
from employees);

22.查询没有奖金或奖金低于100元的员工信息

Select * from employees where commission_pct
is null or commission_pct*salary<100;

23.统计各个职位的员工人数与平均工资

Select job_id,count(employee_id),avg(salary)
from employees group by job_id;

24.统计每个部门中各职位的人数与平均工资

Select department_id,job_id,count(*),avg(salary)
from employees group by department_id,job_id
having department_id is not null;

25.查询在2月份入职的所有员工信息

Select * from employees where to_char(hire_date,'mm')=2;

26.查询所有员工工资都在4000-8000元之间的部门的信息

select * from departments where department_id
in(select department_id from employees
group by department_id having min(salary)>=4000
and max(salary)<=8000);

五、数据库安全性

1.创建用户

(1)创建一个用户xiaohong,密码为oracle;

Create user wanghongidentified by oracle;

(2)授予连接数据库权限、创建表权限,并允许其将权限转授予其它用户;

Connect system/manager@orclGrant create session,create table to xiaohong
with admin option;

(3)设置其默认表空间为USERS,并分配20M的配额;

ALTER USER xiaohongDEFAULT TABLESPACE USERSQUOTA 20M ON USERS;

(4)连接到xiaohong账户下,将创建表权限授予SCOTT;

Connect xiaohong/oracleGrant create table to scott;

(5)在xiaohong账户下创建表xs,字段为xm,cj;

Connect xiaohong/oracle
Create table xs(
Xm varchar2(20),
Cj varchar2(20)
);

(6)将xs表的插入数据权限授予SCOTT;

Connect xiaohong/oracle Grant insert on xs to scott;

(7)连接到SCOTT账户下,插入一条数据(lucy,80)。

Connect scott/tigerInsert into xiaohong.xs values(‘lucy’,80);

2.创建角色
(1)连接到system账户下,授予xiaohong创建角色的权限;

Connect system/manager@orclGrant create role to xiaohong;

(2)创建一个以你的姓名的拼音首字母为名字的用户;

Create user hys identified by oracle;

连接到xiaohong账户下,创建一个角色student,密码为student;

Connect xiaohong/oracleCreate role student identified by student;

(3)授予student角色查询xs表的权限;

Grant select on xs to student;

(5)将student角色赋予你自己的名字的用户。

Grant student to xxx;

六、存储过程的使用

1.创建添加职员记录的存储过程EmpAdd,通过参数接收职员信息,然后将数据插入到EMP表中。

Create or replace procedure EmpAdd(P_empno in number,P_ename in varchar2
)
Is begin
Insert into emp(empno,ename) values(p_empno,p_ename);
End EmpAdd;

2.调用EmpAdd,往EMP表中插入一条数据,具体数据由自己组织,然后用SQL语句查询EMP表,检查存储过程是不是正确执行。

Exec EmpAdd(999,’赵六’);Select * from emp;

3.区分in、out和inout三类参数
(1)编写一个计算EMP表中指定部门职员人数的存储过程count_num1,定义一个in类型的参数bm用于输入部门信息,定义一个out类型的参数person_num用于输出部门人数。

Create or replace procedure count_num(Bm in varchar2,Person_num out number
) as begin
Select count(*) into person_num from emp e,dept d
Where e.deptno=d.deptno and dname=bm;End count_num;

(2)编写一个与(1)相同的存储过程count_num2,但参数类型都定义为inout类型。

Create or replace
Procedure count_num2(
Bm inout varchar2,
Person_num inout number)
As beginSelect count(*) into person_numFrom emp e,dept dWhere e.deptno=d.deptno and dname=bm;End count_num2;

(3)分别调用这两个过程,比较两者在参数使用上的不同之处。

Variable p_num number;
Exec count_num(‘salary’,:p_num);
Print p_num
Exec count_num2(‘salary’,5);

(4)创建一个函数,用部门号作为参数,返回该部门的平均工资

Create or replace function return_avgsall(P_)deptno emp.deptno%type
)
Return emp.sal%type
As v_avgsal emp.sal%type;
Begin Select avg(sal) into v_avgsal from emp where
Deptno= p_deptno;Return v_avgsal;
ExceptionWhen no_data_found thenDbms_output.put_line(‘the deptnois invalid!’);
end return_avgsal;

(5)创建一个行级触发器,当dept删除记录时,将emp的对应记录的deptno设置为空。

Create or replace trigger del_deptBefore delete on dept
For each row
Begin If deleting thenUpdate emp set emp.deptno = null where emp.deptno =:
Old.deptno;End if;
End del_dept;

七、数据库综合设计

1.PL/SQL程序块

编写一个程序块,完成50以内偶数的累积。

Declaresum_i int :=1;begin
for counter in 1..50 loop
if mod(counter,2)=0 then sum_i:=counter*sum_i;
end if;
end loop;dbms_output.put_line('50以内偶数的累积之和是'||sum_i);
end;

注意:dbms_output.put_line使用前需要使用set serveroutput on命令。


2.时间戳

(1)显示一个时间戳,精确到纳秒。

declare v_time timestamp(9):=systimestamp;
begin dbms_output.put_line(v_time);
end;

(2)创建一个只包含一个数值型字段的表,往表中插入10000条记录,利用时间戳计算所用时间。

Create table t1(n number);
Set serveroutput on
declare start_time timestamp;end_time timestamp;n number;
beginn:=1;start_time:=systimestamp;while n<=10000loopinsert into t1 values(n);n:=n+1;end loop;end_time:=systimestamp;dbms_output.put_line(start_time);dbms_output.put_line(end_time);dbms_output.put_line(end_time-start_time);
end;

3.记录、PL/SQL表

(1)创建一个记录类型保存学生数据,类型名为STUDENT,包含XM,XB,BJ三个变量,然后定义一个该类型的变量STUDENT1,输入学生本个的信息,并输出。

declaretype stu_record is record(XM varchar2(20),XB varchar2(20),BJ varchar2(20)
);
student1 stu_record;
beginstudent1.XM:="zhangsan";student1.XB:="nan";student1.BJ:="zhongguo";
dbms_output.put_line(student1.XM);
dbms_output.put_line(student1.XB);
dbms_output.put_line(student1.BJ);
end;

(2)创建一个基类型为字符型的PL/SQL表类型,保存学生的姓名,然后定义一个该类型的变量STUDENT2,输入5名同学的姓名,并输出。

declaretype t_tabl is table of varchar2(20) index by  binary_integer;student2 t_tabl;
beginstudent2(1):='zhangsan';student2(2):='lisi';student2(3):='wangwu';student2(4):='zhaoliu';student2(5):='qianqi';
dbms_output.put_line(student2(1));
dbms_output.put_line(student2(2));
dbms_output.put_line(student2(3));
dbms_output.put_line(student2(4));
dbms_output.put_line(student2(5));
end;

4.显示游标

编写一个程序块,定义一个游标,用于处理EMP表中工资大于1500的员工信息,如果员工的工资在1500到2000之间,则工资加上200,如果大于2000,则工资加上100,修改结果保存到EMP表中,并显示输出。定义处理数据的变量要求用到%type或%rowtype。

  declareno employees.employee_id%type;sal employees.salary%type;cursor emp_cur is select employee_id,salary from employees where salary>=1500;
beginopen emp_cur;fetch emp_cur into no,sal;while emp_cur%FOUND loopcasewhen sal<=2000 then update employees set salary=salary+200 where employee_id=no;when sal>2000 then update employees set salary=salary+100 where employee_id=no;else null;end case;
dbms_output.put_line(to_char(no)||'   '||to_char(sal));
fetch emp_cur into no,sal;--读取数据到变量
end loop;close emp_cur;
end;
 declareno employees.employee_id%type;sal employees.salary%type;cursor emp_cur is select employee_id,salary from employees for update;
beginfor cur in my_cur loopif cur.salary>1500 and cur.salary<2000 thenupdate cur set cur.salary:=cur.salary+200;elseif cur.salary>2000 thenupdate cur set cur.salary:=cur.salary+100;else null;end if;end loop;
end;

5.异常处理
编写一个PL/SQL块,修改员工号为201的员工工资为8000元,保证修改后的工资在职位允许的工资范围之内,否则取消操作,并说明原因


好了,喜欢的话欢迎点赞关注,一起交流学习!!!

大型数据库Oracle基础练习相关推荐

  1. Oracle 基础教程

    oracle基础教程 目录 1.oracle的安装 2.变量和类型 3.控制语句 4.oracle用户及授权管理 5.管理数据表 6.查询数据表 7.操纵数据表 8.SQL内置函数 9.触发器 10. ...

  2. oracle fra空间不足,ORACLE 基础解决方案1_扩大FRA区

    ORACLE 基础解决方案(一) Reviewing the Database Architecture Reviewing the Database Architecture 回顾数据库结构 For ...

  3. oracle 基础1

    目    录 一. 数据库基础部分 第一章       oracle基础介绍 第二章  数据查询语言(QL) 第三章  数据定义语言(DDL) 第四章  Oracle数据分区表 第五章  PL/SQL ...

  4. Oracle 基础知识——客户端连接oracle数据库服务端的四种方法

    Oracle基础知识 Oracle Database 11g 快捷版 (Oracle Database XE) 是一款基于 Oracle Database 11g 第 2 版代码库的小型入门级数据库, ...

  5. Oracle基础 游标

    原文:Oracle基础 游标 一.游标 游标用来处理从数据库中检索的多行记录(使用SELECT语句).利用游标,程序可以逐个地处理和遍历一次检索返回的整个记录集. 为了处理SQL语句,Oracle将在 ...

  6. oracle:oracle基础知识(一)

    oracle基础知识(一) oracle基础入门学习笔记:视频链接 文章目录 数据库登录角色及表结构 表空间 创建表空间 认识表 管理表 列的增,删.改 表的重命名 删表 向表中插入数值 复制表 表中 ...

  7. oracle:oracle基础知识(二)

    oracle基础知识(二)笔记:高级查询 文章目录 分组查询 多属性分组语法: 过滤查询 group by 语句增强 sqlplus报表功能 多表查询 等值连接 外连接 自连接 子查询 子查询中的空值 ...

  8. Oracle登陆SQL Plus,Oracle基础学习登陆SQLPLUS(一)

    或 system/(password) 连接到本地的最高帐号 Sql>help index Enter Help [topic] for help. Sql>show all 显示当前SQ ...

  9. oracle基础知识文档,Oracle 基础知识分享PPT

    因测试组需求,所以把Oracle基础知识整理成了PPT,并讲解了一下(PPT无风格,简约派吐舌头). Oracle 是以高级结构化查询语言(SQL)为基础的大型关系数据库,通俗地讲它是用方便逻辑管理的 ...

最新文章

  1. ListFileItem fileItems=sfu.parseRequest(request); fileitems 为,空
  2. tensorflow官方文档_开源分享:最好的TensorFlow入门教程
  3. C语言的一些有趣的冷知识
  4. Windows Server 2008 R2忘记密码导致无法修改密码
  5. python和java后期发展_Python与java的发展前景谁最大
  6. ibm mq的交互命令模式_IBM的完整形式是什么?
  7. 闪退没由报错_关于floor()报错注入,你真的懂了吗?
  8. python访问文件被拒绝_python – uWSGI服务器日志…权限被拒绝读取文件…哪个文件?...
  9. 轻松实现Web数据库的安全
  10. python语法学习第七天--文件
  11. IPhone开发从零开始之1-构思你的产品
  12. centos7 安装pgadmin3
  13. 文件夹访问被拒绝,您需要权限来执行操作
  14. ESlint语法检测工具
  15. Informatica使用操作流程--Router(由器器)、排序、序列 使用 案例6
  16. 木马病毒:英国歌手Amy Winehouse临死前的最后影片,勿点
  17. Could not find any downloads that satisfy the requirement opencv-python
  18. 【入门-08】系统控制单元(SCU)
  19. sa-token使用简单使用
  20. KNN的数据插补方法总结

热门文章

  1. 矩形中分出正方形,递归优化
  2. 【jzoj4598】【准备食物】【字典树】
  3. python爬微信公众号视频_python爬虫公众号所有信息,并批量下载公众号视频
  4. 2017计算机信息类ei,2017年EI收录的中国期刊目录.pdf
  5. python编程编程培训班
  6. 《Hadoop系列》脚本开发自动化配置伪分布式Hadoop
  7. otis电梯服务器tt使用说明_OTIS电梯TT使用说明
  8. GBase 8a管理集群gcware的日志-vote leader、flower、candidate部分
  9. 在Android面试前背八股和学面试技巧真的有用吗?
  10. 碧蓝航线最新服务器2018,2018最新碧蓝航线建造时间表 舰娘建造时间表一览