当前位置:网站首页>数据库高级学习笔记--对象类型
数据库高级学习笔记--对象类型
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;
边栏推荐
- 网络工程——软科中国大学专业排名
- IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望
- [swintransformer source code reading II] window attention and shifted window attention
- 使用 OpenSSL 创建ssl证书
- MySQL 8.0.30 GA
- Scalable search bar, imitating Huawei application market
- 2.9.5 Ext JS的Object类型处理及便捷方法
- 21 day learning challenge - "AUTOSAR from introduction to mastery - practical part"
- Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline
- [one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network
猜你喜欢
![[high number] high number plane solid geometry](/img/fc/da6aefed48f4adbaf58995b5e8fa46.png)
[high number] high number plane solid geometry

Promise实例如何解决地狱回调
![Magic brace- [group theory] [Burnside lemma] [matrix fast power]](/img/cf/606d1bc7cd877771afbdd7640b718c.png)
Magic brace- [group theory] [Burnside lemma] [matrix fast power]

JDBC connection database

2022 high voltage electrician examination simulated 100 questions and simulated examination

Go language slice vs array panic runtime error index out of range problem solving

51 single chip microcomputer storage: EEPROM (I2C)

Detailed introduction of v-bind instruction
![[package deployment]](/img/6f/93a35436947311bc2305adcb0df1a6.png)
[package deployment]

Problems encountered in upgrading golang to version 1.18.4
随机推荐
376. Swing sequence [greedy, dynamic planning -----]
Technology sharing | quick intercom integrated dispatching system
Common tool functions are constantly updated
Window source code analysis (II): the adding mechanism of window
LeetCode - 哈希表专题
C signed and unsigned byte variables
[gossip] the development of programmers needs two abilities most
Go language slice vs array panic runtime error index out of range problem solving
ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
【打包部署】
[Download] several tools for brute force cracking and dictionary generation are recommended
【AUTOSAR-RTE】-3-Runnable及其Task Mapping映射
对话MySQL之父:代码一次性完成才是优秀程序员
express搭建一个简易的本地后台(一)
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
脉冲风采|Committer 专访——腾讯工程师张大伟喊你吃“螃蟹”啦
MATLAB的数列与极限运算
Oracle creates users with query permission only
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
Source code analysis of view event distribution mechanism