1. Overview object is a new feature in Oracle8i or later, the object is actually packaged for a set of data and operations, the abstraction of the object is class. In object-oriented technologies, subjects involve the following important features: encapsulation by encapsulation of data and operations, exposing users concerned as an interface, other data and operations are hidden to the inside of the object, so that it is easy User uses and maintenance. Inheritability objects have inheritance, enhance the scalability of the program, suitable for development of large projects through this feature. Polymorphism There are different objects to be referenced at runtime, and its execution results are different. This feature is called polymorphism. It is because of the object-oriented many advantages, Oracle has joined the full support of this feature in the 8.0 version, and the next part focuses on the object-oriented program design in Oracle. Second, Oracle's object-oriented programming is since the object has so many advantages, then how do you quote it in the Oracle database? Object definitions in Oracle do two steps: First define object types. Define object types Calculatory package type, that is, divided into object type heads (or object specifications, specification) and object types (BODY). Object Type Head includes a declaration of the properties and methods of the object type, and the object type body contains the specific implementation of the object type. For example, define an Empobj object type, the code is as follows:
create or replace type empObj as object (emp_id number (5), emp_name varchar2 (20), emp_salary number (4), - object's functionmember function addsalary (ext_salary number) return varchar2,); - object's body create or replace type body Empobj As Member Function Addsalary Return VARCHAR2 IS BEGIN EMP_SALARY: = EMP_SALARY EXT_SALARY; RETURN TO_CHAR (EMP_SALARY); End Addsalary; END; Especially To note that if the object does not have a member function section, then this object type is defined with only object type heads section. The object instance is then defined. After defining the object type, you can directly define the instance, such as defining an Empobj instance object, the code is as follows:
v_empobj1 Empobj; After these two steps, you can reference the properties and methods of the object instance, reference symbols ".", such as
v_empobj1.emp_id; // Reference EMP_ID attribute v_empobj1.addsalary (300); // Reference ADDSAlary method In addition, it can be initialized in the form of a constructor when initializing the object without member functions, pay attention, pay attention, at this time no need to explicit Define the constructor. For example, initialize the above v_empobj1 object (assuming that there is no member function can perform this initialization), the code is as follows:
v_empobj1 Empobj: = Empobj (10005, 'Jack', 6500); due to the Oracle database is a relational database, its storage data is carried out in the form of a two-dimensional table, and the object is an entity that encapsulates data and operations. Storage information is often multi-dimensional information, how is the storage of the object in the Oracle database? (Here, it is added, the object declared in the PL / SQL block is a temporary object, and the system will automatically recover the resource allocated after it exceeds its role area, but if you need to save the object, you must store it in the database) In fact, the object is divided into two types in the Oracle database: 1. Object column. That is, the data type of the column in the data table can be defined as an object type, so that the object can be stored in the data column. For example, define a table Table1, where the EMP column can be used to store objects. Create Table Table1 (ID Number (2); EMP Empobj;); 2. Object line. You can create an object table, where each column represents an attribute in the object, such a row record is an object. For example, define an EMP table as follows:
Create Table EMP (EMP_ID NUMBER (5); EMP_NAME VARCHAR2 (20); EMP_SALARY NUMBER (4);); This record of the EMP table is an Empobj object, which is inserted into a table.
INSERT INTO EMP VALUES (Empobj (10006, 'Marry', 5000); note that the type of type and object in the table should be partially corresponding to the object type of the object, and other stores will ignore the information of the member function of the object.
Third, the object operation and comparison can operate the object using the DML statement, the syntax of its operation is exactly the same as the general data type, such as returning the object in the Table1 table is Empobj (10002, 'Mike', 3000) record:
Select * from table1 where EMP = Empobj (10002, 'Mike', 3000); If the size of the object is required, it is difficult to handle with a general method. After all, the object contains a set of attributes and cannot be combined. Comparison. You can use the object to add the MAP method and the Order method to resolve this issue. The former is more large by returning the objects of the object to a representative object, and the latter is to obtain the object by comparing the duty of the two objects. the size of. Due to the similarities of the two, the MAP member function of the use is as follows: