下面介绍oralce中面向对象的基本语法
一:抽象数据类型
创建地址类型,一定要加as object,还可以在类型中加过程或方法
createorreplace type addressasobject(
province varchar2(10), --省份属性
city varchar2(10) --市属性
)notfinal; --notfinal表示该类型可以有子类型
定义一个子类型,underaddress说明这个类型继承至address类型
createorreplace type detailAddressunderaddress (
street varchar2(20) --街道属性第3个成员
);
创建员工信息表,最后一列是detailAddress类型
droptableempInfo
createtableempInfo (
eName varchar2(20) , --员工姓名
eSexchar(2), --性别
eAgeint, --年龄
eAddress detailAddress --员工地址
);
--增加数据,只能用构造方法
insertintoempInfovalues(‘aaa‘,‘男‘, 28, detailAddress(‘湖北‘,‘襄樊‘,‘八一路‘));
--查询
select*fromempInfowhereeSex =‘男‘;
select*fromempInfo ewheree.eAddress.city =‘武汉‘; --如果查询条件包
含属性必须用表的别名
--更新有2种方式:
--第一种方式:整体更新
updateempInfo esete.eAddress = detailAddress(‘湖北‘,‘武汉‘,‘武昌‘)wheree.eName =‘ccc‘;
--第二种方式:只更新抽象类型的某一列
updateempInfo esete.eAddress.city =‘武汉‘wheree.eName =‘ccc‘;
--删除
deletefromempInfo ewheree.eAddress.city =‘武汉‘;
--为抽象数据类型的属性建立索引
createindexidxemponempInfo(eAddress.city);
--删除
droptableempInfo;
droptype address force; --强制删除抽象类型
二:对象表,表中的每一行就是一个对象
--创建抽象数据类型person,并作为基类型
createorreplace type personasobject(
pName varchar2(20), --姓名
pSexchar(2), --性别
pAgeint--年龄
)notfinal;
--创建子类型student,继承person,后面不要加asobject
createorreplace type studentunderperson (
stuIdint
);
--创建对象表stuInfo
createtablestuInfoofstudent;
--为对象表创建主键约束
altertablestuInfoaddconstraintpk_stuInfoprimarykey(stuId);
--插入数据,当普通表插入
insertintostuInfovalues(‘aaa‘,‘男‘, 29, 1001);
--插入数据,用构造方法
insertintostuInfovalues(student(‘bbb‘,‘男‘, 26, 1002));
--查询,当普通表用
select*fromstuInfowherestuId = 1002;
--更新和删除都用普通的sql语句即可
updatestuInfosetpAge = 29wherepName =‘ccc‘;
deletefromstuInfowherestuId = 1001;
--ref(表别名)函数用来返回对象的OID,也就是对象标识符,对象表也有rowid
selectrowid,ref(s)fromstuInfo s;
--创建学生分数表,注意外键
createtablestuScore (
sturefstudent, --stu这一列的值必须出现在stuInfo表中,且stu这一列存的对象的OID而不是对象本身
scoreint--分数
);
insertintostuscoreselectref(s), 90fromstuInfo swherestuId = 1001;
insertintostuscoreselectref(s), 80fromstuInfo s; --插入3行数据
insertintostuscoreselectref(s), 70fromstuInfo swherestuId = 1003;
--查询
select*fromstuScore;
--deref(列名)函数可以把OID还原为对象,主键列显示有问题
selectderef(s.stu), scorefromstuScore swheres.stu.stuId = 1001;
--修改,以下2个都可以
updatestuScoresetscore=100wherestu = (selectref(s)fromstuInfo swherestuId = 1001);
updatestuScore ssetscore = 99wheres.stu.stuId = 1001;
--删除
deletefromstuScorewherestu = (selectref(s)fromstuInfo swherestuId = 1001);
deletefromstuScore swheres.stu.stuId = 1001;
三:可变数组
--就是一个可以存储多个值的有最大长度的数组,数组的成员可以是任意类型
--建立一个可变数组类型,长度是10,存放的数据类型是number(4)
createorreplace type arrTypeasvarray(10)ofnumber(4);
createorreplace type scoreTypeasobject(
subName varchar2(10),
scoreint
);
--创建一个长度为10的可变数组,存放数据类型是scorType
createorreplace type arrScoreTypeasvarray(10)ofscoreType;
--创建学生信息表
--droptablestuInfo;
createtablestuInfo (
stuIdintprimarykey,
score arrScoreType --可变数组,最多10个成员
);
--插入数据,用可变数组的构造函数
insertintostuInfovalues(1, arrScoreType(
scoreType(‘sql‘, 50), scoreType(‘C#‘, 80), scoreType(‘java‘, 90)));
insertintostuInfovalues(2, arrScoreType(
scoreType(‘sql‘, 60), scoreType(‘C#‘, 85), scoreType(‘java‘, 95), scoreType(‘html‘, 60)));
insertintostuInfovalues(3, arrScoreType(
scoreType(‘sql‘, 70), scoreType(‘java‘, 93)));
--查询
select*fromstuInfo; --查询结果是集合
--如何才能查询出可变数组里的数据呢?思路是:用table函数把集合转化为表,然后再从这个表查询数据
select*fromtable(selects.scorefromstuInfo swheres.stuId = 2);
--table函数里面只能是一个可变数组
selects.stuId, t.*fromstuInfo s,
table(selectscorefromstuInfowherestuId = s.stuId) t
wheres.stuId = 2;
--更新,整个可变数组一起更新,不能只更新数组的某个元素
updatestuInfosetscore = arrScoreType(
scoreType(‘sql‘, 50), scoreType(‘C#‘, 80))wherestuId = 1;
四:嵌套表
--创建抽象类型
createorreplace type scoreTypeasobject(
subName varchar2(10),
scoreint
);
--创建嵌套表类型
createorreplace type nestTableistableofscoreType;
--创建包含嵌套表的学生信息表
createtablestuInfo (
stuIdint,
score nestTable --其实存的是引用,实际数据存在abc表中
) nestedtablescore storeasabc;
--nestedtablescore storeasabc意思是:stuInfo这个表中的score这一列是嵌套表类型,嵌套表实际是存在abc这个表中
--增删和可变数组一样
insertintostuInfovalues(3, nestTable(
scoreType(‘sql‘, 70), scoreType(‘java‘, 93)));
--查询,思路:把嵌套表先查出来,然后把嵌套表和stuInfo进行联合查询
select*fromtable(selectss.scorefromstuInfo sswherestuId = 3);
selects.stuId, t.*fromstuInfo s,table(selectss.scorefromstuInfo sswherestuId = s.stuId) t
wheres.stuId = 3;
--更新
updatetable(selectss.scorefromstuInfo sswherestuId=3) t
sett.score = 80wheret.subName =‘sql‘;
--删除
deletefromtable(selectss.scorefromstuInfo sswherestuId = 3) t
wheret.subname=‘sql‘;
可变数组和嵌套表的异同:
相同点:
1、都是抽象类型
2、都可以作为表中某列的数据类型(record和快表是不能作为列的数据类型的)
不同点:
1、可变数组本身就存放在原表中,而嵌套表存放在另外的表中
2、可变数组有大小限制,而嵌套表没有
3、可变数组更新时必须更新整个可变数组,而嵌套表更新时可以只更新嵌套表中的部分记录
原文:http://my.oschina.net/u/2273582/blog/493931