实验报告:Oracle 数据库实验

一、 Oracle 数据库的创建,用户的创建与授权

Oracle 数据库是目前最为常用的关系型数据库之一,其功能强大,使用广泛。在本次实验中,我们将学习如何创建 Oracle 数据库,并创建用户,进行授权等操作。

  1. Oracle 数据库的创建

Oracle 数据库的创建需要进行如下步骤:

  • 安装 Oracle 软件,选择合适的版本和操作系统。
  • 安装完成后,启动 Oracle 软件,进入 SQLPlus 命令行界面。
  • 使用 SYS 用户登录,创建新的数据库实例。
SQL> connect sys as sysdba;
SQL> create database mydb;
  1. 用户的创建与授权

创建用户可以使用如下语句:

SQL> create user test identified by test;

其中,'test' 为用户名,'test' 为密码。

授权可以使用如下语句:

SQL> grant connect, resource, dba to test;

其中,'connect' 表示允许用户连接到数据库,'resource' 表示允许用户创建表、索引、序列等对象,'dba' 表示允许用户进行数据库管理员的所有操作。

二、 数据表的创建与修改,基础数据的导入与导出

在 Oracle 数据库中,数据表是存储数据的基本单位。数据表的创建可以使用如下语句:

SQL> create table employee (id number(10) primary key, name varchar2(20), age number(3), gender varchar2(10));

其中,'employee' 为表名,'id' 为列名,'number(10)' 表示数据类型为数字,'varchar2(20)' 表示数据类型为字符型,'primary key' 表示该列为主键。

数据表的修改可以使用如下语句:

SQL> alter table employee add salary number(10);

其中,'add salary' 表示增加一列名为 'salary' 的列。

基础数据的导入可以使用如下语句:

SQL> insert into employee values (1, 'Tom', 25, 'Male');

其中,1 为 'id' 列的值,'Tom' 为 'name' 列的值,25 为 'age' 列的值,'Male' 为 'gender' 列的值。

基础数据的导出可以使用如下语句:

SQL> select * from employee;

其中,'*' 表示所有列。

三、 数据表的一般查询、连接查询、嵌套查询与集合查询

在 Oracle 数据库中,数据表的查询可以使用如下语句:

SQL> select * from employee where age>25;

其中,'*' 表示所有列,'where age>25' 表示查询年龄大于 25 的员工。

连接查询可以使用如下语句:

SQL> select a.name, b.salary from employee a, salary b where a.id=b.id;

其中,'a' 和 'b' 表示两个数据表,'name' 和 'salary' 表示查询的列。

嵌套查询可以使用如下语句:

SQL> select * from employee where id in (select id from salary where salary>5000);

其中,'in' 表示在子查询中查询到的 'id' 值。

集合查询可以使用如下语句:

SQL> select name from employee where age>25 union select name from employee where gender='Male';

其中,'union' 表示求并集。

四、 视图、索引的创建

在 Oracle 数据库中,视图是一种虚拟的表,不存储数据,而是通过查询其他表的数据来生成。创建视图可以使用如下语句:

SQL> create view emp_salary as select a.name, b.salary from employee a, salary b where a.id=b.id;

其中,'emp_salary' 为视图名称,'select a.name, b.salary' 表示查询的列。

索引是一种提高查询效率的技术,创建索引可以使用如下语句:

SQL> create index idx_employee on employee(id);

其中,'idx_employee' 为索引名称,'id' 为创建索引的列。

五、 触发器、函数、包、游标、存储过程等 PL/SQL 编程的实现

在 Oracle 数据库中,PL/SQL 是一种嵌入式编程语言,可以实现触发器、函数、包、游标、存储过程等功能。

  1. 触发器

触发器是一种自动执行的程序,可以在特定的事件发生时触发。创建触发器可以使用如下语句:

SQL> create trigger trg_employee before insert on employee for each row begin insert into log values(:new.id, :new.name); end;

其中,'trg_employee' 为触发器名称,'before insert' 表示在插入数据之前触发,'log' 为另外一个数据表。

  1. 函数

函数是一种可以重复使用的程序,可以接收参数并返回一个值。创建函数可以使用如下语句:

SQL> create or replace function get_salary(id number) return number as salary number; begin select salary into salary from salary where id=id; return salary; end;

其中,'get_salary' 为函数名称,'id' 为参数,'salary' 为返回值。

包是一种将多个函数、存储过程封装起来的程序,提高了代码的重用性和可维护性。创建包可以使用如下语句:

SQL> create or replace package emp_pkg as function get_salary(id number) return number; end;
  1. 游标

游标是一种提供在数据表中移动的方式,可以实现对数据表的遍历。创建游标可以使用如下语句:

SQL> declare cursor c1 is select * from employee; emp employee%rowtype; begin open c1; loop fetch c1 into emp; exit when c1%notfound; dbms_output.put_line(emp.name); end loop; close c1; end;
  1. 存储过程

存储过程是一种可以执行多个 SQL 语句的程序,可以接收参数并返回多个值。创建存储过程可以使用如下语句:

SQL> create or replace procedure update_salary(id number, salary number) is begin update salary set salary=salary where id=id; end;

总结

本次实验我们学习了 Oracle 数据库的创建,用户的创建与授权,数据表的创建与修改,基础数据的导入与导出,数据表的一般查询、连接查询、嵌套查询与集合查询,视图、索引的创建,以及触发器、函数、包、游标、存储过程等 PL/SQL 编程的实现。这些基础知识对于我们进一步学习 Oracle 数据库和数据管理方面的知识都具有重要的作用。

Oracle 数据库实验报告:从基础到 PL/SQL 编程

原文地址: https://www.cveoy.top/t/topic/ohPb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录