实验报告:Oracle数据库实验

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

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

1、Oracle数据库的创建

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

(1)安装Oracle软件,选择合适的版本和操作系统。

(2)安装完成后,启动Oracle软件,进入SQLPlus命令行界面。

(3)使用SYS用户登录,创建新的数据库实例。

SQL> connect sys as sysdba; SQL> create database mydb;

2、用户的创建与授权

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

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是一种嵌入式编程语言,可以实现触发器、函数、包、游标、存储过程等功能。

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

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为另外一个数据表。

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

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;

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

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;

存储过程是一种可以执行多个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数据库和数据管理方面的知识都具有重要的作用

生成一份课程报告不少于两千字的实验报告要求至少包括1Oracle数据库的创建用户的创建与授权; 2数据表的创建与修改基础数据的导入与导出; 3数据表的一般查询、连接查询、嵌套查询与集合查询; 4视图、索引的创建; 5触发器、函数、包、游标、存储过程等PLSQL编程的实现; 所有内容要求有理论总结和实例说明

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

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