当前位置:网站首页>数据库高级学习笔记--对象类型
数据库高级学习笔记--对象类型
2022-07-28 09:06:00 【萍果馅是年糕】
1.对象类型
人:对象,万物皆是对象
属性:身高、体重、年龄 用来描述对象的特征的
方法:对象的行为特征:跑、走、说
属性与方法组成
对象的属性:对象类型的字段
对象的方法:对象类型的函数或者过程
对象类型有时也为用户自定义类型
可以在对象类型中写过程和函教
对象类型包括对象类型规范和对象类型体两个部分
--对象类型规范:对象与应用的接口,用于定义对象的公有属性和方法
--对象类型体:用于实现对象类型规范所定义的公有方法(用于实现
--对象类型规范中定义的函数或者过程的具体实现代码)。
2.创建简单对象类型
只有对象类型规范,不需要创建对象类型体
语法:
create [or replace] type 对象类型名称 as object(
字段1 数据类型
字段2 数据类型
)
--例:创建一个学生的对象类型
create or replace type Student01 as object(
name varchar2(20),
sex varchar2(20),
age number(20)
)
--使用对象类型
--(1)可以将对象类型当做普通类型来使用,在创建对象的时候,作为对象属性的数据类型
create or replace type Student01_test as object(
id number(20),
sx Student01
)
--可以在创建表的时候使用对象类型
create table Student03_table(
no number(20),
student Student01_test
)
select * from Student03_table;
-----------------
--创建对象类型
create or replace type obj_emp_01 as object(
ename_obj varchar2(20),
job_obj varchar2(20),
sal_obj number(20)
)
-----使用对象类型
declare
emps obj_emp_01;
begin
select obj_emp_01(ename,job,sal) into emps from emp where empno=7788;
dbms_output.put_line(emps.ename_obj);
dbms_output.put_line(emps.job_obj);
dbms_output.put_line(emps.sal_obj);
end;
3.创建带用函数的对象类型
--需要创建对象规范和对象体
对象规范语法:
create [or replace] type 对象类型名称 as object(
字段1 数据类型,
字段2 数据类型,
member function 函数名称(函数类型) return 返回值类型 ----(推荐使用)意思是对象方法可以使用函数或者存储过程
member procedure 过程名称(过程类型) -- 使用函数更好
)
--对象体中的对象类型名称必须与对象规范中的对象类型名称一致
对象体语法:
create [or replace] type body 对象类型名称 as
member function 函数名称(参数名称 in|out 参数类型)return 返回值类型 is
变量名 数据类型;
begin
函数的实现代码;
end;
end;
--例:创建学生的对象类型
create or replace type Student03 as object(
name varchar2(20),
sex varchar2(20),
birthday date,
--定义一个函数从生日中获取年龄
member function get_age return number
)
--创建对象体,编写get_age的具体实现的代码
create or replace type body student_1 as
member function get_age return number is
v_age number;
begin
select floor(months_between(sysdate,birthday)/12) into v_age from dual;
return v_age;
end;
end;
--创建测试表使用student_04类型
create table student_1_table(
id number(20),
students student_1
)
select * from student_1_table;
--插入测试数据
insert into student_02_table values(1,'学生','女',TO_DATE('19950108'),)
select s.students.get_age() from student_02_table s;
---对象表
---指包含对象类型列的表
行对象是指直接基于对象类型所建立的表
列对象是指包含多个列的对象表
---创建行对象
create table 表名 of 对象类型
create table student_tables of obj_emp_01;
select * from student_tables;
--插入数据
---(1)普通插入
insert into student_tables values('循环','程序员','1110');
---(2)使用构造函数插入数据
insert into student_tables values(obj_emp_01('学姐','程序员','2220'));
--检查行对象数据
查询工作
declare
jobs varchar2(20);
begin
select value(s) into jobs from student_tables s where s.ename_obj='循环';
dbms_output.put_line(jobs);
end;
---练习
(1)创建对象类型:
对象名称:emp_object
对象属性:
员工编号
员工姓名
员工薪资
部门编号
对象方法:---使用两个函数
根据编号获取指定员工的工资
根据部门编号查询部门总薪资
--创建对象规范 ---定义下面要用到的参数,函数
create or replace type emp_object as object(
m_empno number(4),
m_ename varchar2(10),
m_sal number(7,2),
m_deptno number(2),
member function GetSalByEmpno(m_empno in number) return number,---定义参数时只需要定义数据类型,不需要长度
member function GetSunSalByDeptno(m_deptno in number) return number
);
--创建对象体
create or replace type body emp_object as
member function GetSalByEmpno(m_empno in number) return number as
r_sal number; --注意函数返回值,需要自定义
begin
select sal into r_sal from emp where empno=m_empno;
return r_sal;
end;
member function GetSunSalByDeptno(m_deptno in number) return number as
r_sals number;
begin
select sum(sal) into r_sals from emp where deptno=m_deptno;
return r_sals;
end;
end;----注意
(2)实例化emp_object
输出根据编号获取的指定员工的工资
输出根据部门编号查询到的部门总薪资
--实例化emp_object
declare
m_emp emp_object;
begin
m_emp:=emp_object(7788,'',0,20); ----与对象规范化定义的变量一一对应
dbms_output.put_line(m_emp.GetSalByEmpno(m_emp.m_empno));
dbms_output.put_line(m_emp.GetSunSalByDeptno(m_emp.m_deptno));
end;
边栏推荐
- Dn-detr paper accuracy, and analyze its model structure & 2022 CVPR paper
- ActivityRouter源码解析
- 2022 high voltage electrician examination simulated 100 questions and simulated examination
- Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
- 376. 摆动序列【贪心、动态规划------】
- 力扣376-摆动序列——贪心
- matlab基本操作
- 常用工具函数 持续更新
- 树上启发式合并
- 376. Swing sequence [greedy, dynamic planning -----]
猜你喜欢

What is cross domain? How to solve the cross domain problem?

Opencv4.60 installation and configuration

mysql 最大建议行数2000w,靠谱吗?
![[Guangxi University] information sharing of postgraduate entrance examination and re examination](/img/25/e35de6b9d803c9a80e0d2816aaad87.jpg)
[Guangxi University] information sharing of postgraduate entrance examination and re examination

Salted fish esp32 instance - mqtt lit LED

Network engineering -- ranking of Chinese universities in Soft Science

QT basic hand training applet - simple calculator design (with source code, analysis)
![[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates](/img/ae/096b558bc1342447205b442a244aae.png)
[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates
![Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline](/img/9b/b8b1148406e8e521f12ddd5c12bf89.png)
Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline

Final keyword and enumeration type
随机推荐
IDC script file running
MATLAB的符号运算
Personal blog applet
Conditions and procedures of stock index futures account opening
2022 high voltage electrician examination simulated 100 questions and simulated examination
[package deployment]
Window源码解析(四):Window的删除机制
Alibaba cloud server setup and pagoda panel connection
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
会议OA系统
[solution] error in [eslint] eslint is not a constructor
ShardingSphere简介(一)
Detailed introduction of v-bind instruction
MATLAB的实时编辑器
Source code analysis of activityrouter
C# 之 方法参数传递机制
就这么一个简单的校验,80%的程序员却做不到,更不理解!
What is the difference between these two sets of code?
LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符
Retrofit source code analysis