当前位置:网站首页>数据库高级学习笔记--对象类型
数据库高级学习笔记--对象类型
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;
边栏推荐
- 对话MySQL之父:代码一次性完成才是优秀程序员
- 51 single chip microcomputer storage: EEPROM (I2C)
- FPGA development learning open source website summary
- ArrayList内部原理解析
- 【AUTOSAR-RTE】-3-Runnable及其Task Mapping映射
- 【解决】ERROR in [eslint] ESLint is not a constructor
- Express builds a simple local background (1)
- [Guangxi University] information sharing of postgraduate entrance examination and re examination
- ECCV 2022 | can be promoted without fine adjustment! Registration based anomaly detection framework for small samples
- Informatics Olympiad all in one 1617: circle game | 1875: [13noip improvement group] circle game | Luogu p1965 [noip2013 improvement group] circle game
猜你喜欢
![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
![376. Swing sequence [greedy, dynamic planning -----]](/img/c3/46cdd8c9320c529171cbf963c768a7.png)
376. Swing sequence [greedy, dynamic planning -----]

咸鱼ESP32实例—MQTT 点亮LED

使用 OpenSSL 创建ssl证书

2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
![[C language] detailed explanation sequence table (seqlist)](/img/60/c8cee6a6afe57247aba583291cc99b.png)
[C language] detailed explanation sequence table (seqlist)

RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline

技术分享| 快对讲综合调度系统

DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文

Regular expressions for positive and negative values
随机推荐
Alibaba cloud server setup and pagoda panel connection
ARouter源码解析(一)
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
LeetCode - 哈希表专题
2022 safety officer-c certificate special operation certificate examination question bank and answers
脉冲风采|Committer 专访——腾讯工程师张大伟喊你吃“螃蟹”啦
go语言切片Slice和数组Array对比panic runtime error index out of range问题解决
IJCAI 2022 | the latest overview of graph structure learning: research progress and future prospects
MySQL 8.0.30 GA
How view works
Regular expressions are hexadecimal digits?
[C language] detailed explanation sequence table (seqlist)
Leetcode - hashtable topic
《我的Vivado实战—单周期CPU指令分析》
【打包部署】
什么是跨域?如何解决请跨域问题?
express搭建一个简易的本地后台(一)
Promise实例如何解决地狱回调
使用 OpenSSL 创建ssl证书
业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)