当前位置:网站首页>数据库高级学习笔记--对象类型
数据库高级学习笔记--对象类型
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;
边栏推荐
- 技术分享| 快对讲综合调度系统
- C# 窗体应用使用对象绑定 DataGridView 数据绑定
- Dn-detr paper accuracy, and analyze its model structure & 2022 CVPR paper
- Express builds a simple local background (1)
- Arouter source code analysis (II)
- ECCV 2022 | 无需微调即可推广!基于配准的少样本异常检测框架
- final关键字和枚举类型
- mysql 最大建议行数2000w,靠谱吗?
- SD卡介绍(基于SPEC3.0)
- Database core system
猜你喜欢

对话MySQL之父:代码一次性完成才是优秀程序员

2022 high voltage electrician examination simulated 100 questions and simulated examination

mq的学习

51 single chip microcomputer storage: EEPROM (I2C)

21天学习挑战赛-《Autosar从入门到精通-实战篇》
![[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

股指期货开户的条件和流程

咸鱼ESP32实例—MQTT 点亮LED

MQTT. JS introductory tutorial: learning notes

【广西大学】考研初试复试资料分享
随机推荐
脉冲风采|Committer 专访——腾讯工程师张大伟喊你吃“螃蟹”啦
Face warp - hand tear code
IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望
opencv安装配置测试
《我的Vivado实战—单周期CPU指令分析》
Source code analysis of view event distribution mechanism
Go language slice vs array panic runtime error index out of range problem solving
MQTT.js 入门教程:学习笔记
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
express搭建一个简易的本地后台(一)
C# 窗体应用使用对象绑定 DataGridView 数据绑定
With frequent data leakage and deletion events, how should enterprises build a security defense line?
Introduction to shardingsphere (I)
2022 supplementary questions for the first session of Niuke multi school
Scalable search bar, imitating Huawei application market
Changes in the relationship between data and application in IT industry
2022 safety officer-b certificate examination simulated 100 questions and answers
Hexadecimal representation of negative numbers
2.9.5 Ext JS的Object类型处理及便捷方法
[high number] high number plane solid geometry