Plsql programming Object-oriented operations
1) Understanding the role of object type composition and its components
2) Create a Simple object type and use the
3) Create complex object types and use the
4) Learn to establish reference object types and use
The object type is a user-defined composite data type, which encapsulates the process and function of the data structure and structure, in order to describe the specific characteristics that the object of the world is abstracted out, both attributes and methods should cover the public characteristics of the object, such as each employee has the code, name, salary, So the definition of Employee_type type should include these characteristics, object instance is the concrete implementation of the object type, is to abstract a specific thing according to the model, the model built a car.
For example, employees Scott and Smith are two object instances of the object type Employee_type.
Create or replace type Employee_type is
(ename varchar2 (), Eno varchar2 (), Sal number (6,3));
CREATE table Employee_tab of Employee_type;
If you want to create an object type, you must complete both the Object type specification (property and method writing) and the object type body (the implementation of the Refinement method).
First, the top object type specification requires attention to the following points
1) cannot have default value, cannot be null or default 4not null is not allowed
2) Some data types cannot be used such as plsql-specific types, Binary_integer%type,%rowtype,ref_cursor record Pls_integer, etc.
Two, the object type body inside can have three kinds of methods the default is the construction method,
And one is the member method, the user accesses the object instance data, employ employe_type; Employ.method ()
static method, Employe_type.method (); Used to access the object type, Object_type.method ();
Map method, the order method can only compare the size of two object instances.
Http://www.cnblogs.com/lanzi/archive/2011/03/30/1999790.html
Object tables are two types of tables that contain object types,
One is a row object, that is, the type stores data as a row
CREATE table Employe_tab of Employe_type; that is, the property of type is the column of the table
One is a column object, that is, the data stored by the type is a few columns of the table
CREATE TABLE Employtable (
ID number,dname varchar2 (10),
Employee Employe_type, salary number (6,2));
The reference type ref data type, which is a logical pointer to a row object, Oracle built-in data type, can use different tables to share objects to reduce memory usage.
CREATE TABLE Department (
DNO number (2), Dname varchar2 (Ten), EMP REF employee_type);
Formally start to write object types first establish the object type specification, and then write to build the object type body if there is no method inside the type specification, then you do not have to write the object type body.
Create or replace type Type_name as Object (
Attribute1 datatype[,attribute2 Datetype,...],
[Member|static Method1,]
Create or replace type body type_name as|is
Member|static method1 body;
Member|static METHOD2 body;
...
eg
Create or replace type Person_type1 as Object (creates an object type specification without a method, so there is no need to refine the body
Name VARCHAR2 (2), Gender varchar2 (2), birthdate date);
Use object types to create tables with types
CREATE table Person_tab1 of Person_type1; line Object
CREATE table person_tab2 (; Column Object
Eno number (6), Person Person_type1,
Sal Number (6,2)
);
eg
Create or replace type Person_type2 as Object (
Name VARCHAR2 (2), Gender varcahr2 (+), birthdate date,
Member Procedure change_address (new_addr varchar2),
member function Get_info return VARCHAR2
); object type specification with methods
Perfect Body Object type body
Create or replace type body person_type2 is
Member Procedure change_address (new_addr varchar2)
Is
Begin
ADDRESS:=NEW_ADDR;
End
member function Get_info return VARCHAR2
Is
V_info varchar2 (100);
Begin
v_info:= ' name: ' | | name| | ', date of birth ' | | Birthdate
return v_info;
End
End
Use object types to create tables with types
CREATE TABLE EMPLOYEE_TAB2 (
Eno number (6,2), person person_type2,
Sal Number (6,2), Job varchar2 (10)
);
INSERT into EMPLOYEE_TAB2 values (person_type2 (' Wang Nan ', ' Male ', to_date (' 20141212 ', ' YYYYMMDD '), ' Hohhot '), 1,2000, ' senior Mechanic ');
Plsql when retrieving Row object data, you must use the Vlue function if you put the data into an object type variable
Declare
Person Person_type1;
Begin
Select Vlue (p) into the person from EMP p
where p.name = ' &name ';
Dbms_output.put_line (' Sex is ' | | Person.gender);
End
Create a table of column objects that will be placed only in the class object variable when retrieving data
Declare
Person2 Person_type1;
Begin
Select Person,sal to Person2,salary from emp where empno= ' &no ';
End
Declare
V_person person_type2;
Begin
Select person to V_person from EMPLOYEE_TAB2
where eno=&no;
V_person.change_address (' aaaa '); Raw data for instance access
Dbms_output.put_line (V_person.get_info); Instance accesses the
End
This article from "Yun Weibang" blog, declined reprint!
Plsql programming Object-oriented operations