Object Types
Oracle implements Objects through the use of TYPEs, defined in a similar way to packages. Unlike packages where the instance of the package is limited to the current session, an instance of an object type can be stored in the database for later use. The definition of the type contains a comma separated list of attributes/properties, defined in the same way as package variables, and member functions/procedures. If the a type contains member functions/procedures, the procedural work for these elements is defined in the TYPE BODY.
- Define A TYPE
- Define A TYPE BODY
- Defining A Table
- Constructors
- Data Access
- Issues
Define A TYPE
First we define a type PersonObj to represent a person. In this example, a person is defined by three attributes (first_name, last_name, date_of_birth) and one member function (getAge).
CREATE OR REPLACE TYPE PersonObj AS OBJECT (
first_name VARCHAR2(50),
last_name VARCHAR2(50),
date_of_birth DATE,
MEMBER FUNCTION getAge RETURN NUMBER
);
/
First we define a type PersonObj to represent a person. In this example, a person is defined by three attributes (first_name, last_name, date_of_birth) and one member function (getAge).
CREATE OR REPLACE TYPE PersonObj AS OBJECT (
first_name VARCHAR2(50),
last_name VARCHAR2(50),
date_of_birth DATE,
MEMBER FUNCTION getAge RETURN NUMBER
);
/
Define A TYPE BODY
Next we define a TYPE BODY to add functionality to the getAge member function.
CREATE OR REPLACE TYPE BODY PersonObj AS
MEMBER FUNCTION getAge RETURN NUMBER AS
BEGIN
RETURN Trunc(Months_Between(Sysdate, date_of_birth)/12);
END getAge;
END;
/
Next we define a TYPE BODY to add functionality to the getAge member function.
CREATE OR REPLACE TYPE BODY PersonObj AS
MEMBER FUNCTION getAge RETURN NUMBER AS
BEGIN
RETURN Trunc(Months_Between(Sysdate, date_of_birth)/12);
END getAge;
END;
/
Define A TYPE
First we define a type PersonObj to represent a person. In this example, a person is defined by three attributes (first_name, last_name, date_of_birth) and one member function (getAge).
CREATE OR REPLACE TYPE PersonObj AS OBJECT (
first_name VARCHAR2(50),
last_name VARCHAR2(50),
date_of_birth DATE,
MEMBER FUNCTION getAge RETURN NUMBER
);
/
First we define a type PersonObj to represent a person. In this example, a person is defined by three attributes (first_name, last_name, date_of_birth) and one member function (getAge).
CREATE OR REPLACE TYPE PersonObj AS OBJECT (
first_name VARCHAR2(50),
last_name VARCHAR2(50),
date_of_birth DATE,
MEMBER FUNCTION getAge RETURN NUMBER
);
/
Define A TYPE BODY
Defining A Table
Now the object type is defined we can use it as a datatype in a table.
Now the object type is defined we can use it as a datatype in a table.
Constructors
To insert data into the PEOPLE table we must use the PersonObj() constructor. This can be done as part of a regular DML statement, or using PL/SQL.
INSERT INTO people
VALUES (1, PersonObj('John','Doe',
TO_DATE('01/01/1999','DD/MM/YYYY')));
COMMIT;
DECLARE
v_person PersonObj;
BEGIN
v_person := PersonObj('Jane','Doe',
TO_DATE('01/01/1999','DD/MM/YYYY'));
INSERT INTO people VALUES (2, v_person);
COMMIT;
END;
/
To insert data into the PEOPLE table we must use the PersonObj() constructor. This can be done as part of a regular DML statement, or using PL/SQL.
INSERT INTO people
VALUES (1, PersonObj('John','Doe',
TO_DATE('01/01/1999','DD/MM/YYYY')));
COMMIT;
DECLARE
v_person PersonObj;
BEGIN
v_person := PersonObj('Jane','Doe',
TO_DATE('01/01/1999','DD/MM/YYYY'));
INSERT INTO people VALUES (2, v_person);
COMMIT;
END;
/
Data Access
Once the data is loaded it can be accessed using the dot notation.
- alias.column.attribute
- alias.column.function()
SELECT p.id,
p.person.first_name,
p.person.getAge() age
FROM people p;
ID PERSON.FIRST_NAME AGE
---------- --------------------------- ----------
1 John 2
2 Jane 2
2 row selected.
SQL>
2 comments:
Thank you for sharing wonderful information with us to get some idea about that content. check it once through
Machine Learning With TensorFlow Training and Course in Tel Aviv
| CPHQ Online Training in Beirut. Get Certified Online
Do you understand there is a 12 word phrase you can speak to your crush... that will induce deep feelings of love and impulsive appeal for you deep inside his chest?
Because deep inside these 12 words is a "secret signal" that triggers a man's impulse to love, cherish and look after you with his entire heart...
=====> 12 Words That Trigger A Man's Love Instinct
This impulse is so built-in to a man's mind that it will drive him to try harder than before to build your relationship stronger.
Matter-of-fact, fueling this all-powerful impulse is absolutely essential to having the best possible relationship with your man that as soon as you send your man one of the "Secret Signals"...
...You'll immediately find him open his heart and soul for you in such a way he never experienced before and he will identify you as the one and only woman in the world who has ever truly tempted him.
Post a Comment