Oracle Tipe Objek PL/SQL: CREATE TYPE dengan Contoh
⚡ Ringkasan Cerdas
Tipe objek dalam PL/SQL membawa pemrograman berorientasi objek ke Oracle, memungkinkan Anda memodelkan entitas dunia nyata dengan atribut dan metode. Mereka mendukung konstruktor, pewarisan, dan perbandingan kesamaan, sehingga satu tipe tingkat skema dapat menyimpan dan memproses data terstruktur.

Apa Tipe Objek di PL/SQL?
Object-Oriented Programming is especially suited to building reusable components and complex applications. Programs are organized around "objects" rather than "actions"; that is, they are designed to work with and interact with an entire object rather than a single action. This approach lets the programmer populate and manipulate details at the object-entity level.
Gambar di bawah ini menggambarkan contoh tipe objek di mana rekening bank diperlakukan sebagai entitas objek. Atribut objek menyimpan nilai — untuk rekening bank, nilai-nilai tersebut adalah nomor rekening, saldo bank, dan sebagainya — sedangkan metode objek menjelaskan tindakan seperti menghitung suku bunga atau menghasilkan laporan bank, yang masing-masing membutuhkan proses untuk diselesaikan.
In PL / SQLPemrograman berorientasi objek didasarkan pada tipe objek. Tipe objek dapat mewakili entitas dunia nyata apa pun. Bagian-bagian di bawah ini membahas tipe objek, komponen-komponennya, dan cara membuat serta menggunakannya.
Komponen Jenis Objek
Tipe objek PL/SQL pada dasarnya terdiri dari dua komponen.
- atribut
- Anggota/Metode
atribut
Atribut adalah kolom atau bidang tempat data disimpan. Setiap atribut dipetakan ke tipe data yang mendefinisikan tipe pemrosesan dan penyimpanan untuk atribut tersebut. Suatu atribut dapat berupa tipe data apa pun yang valid. Tipe data PL/SQL, atau bisa juga dari tipe objek lain.
Anggota/Metode
Anggota, atau metode, adalah subprogram Didefinisikan di dalam tipe objek. Parameter ini tidak digunakan untuk menyimpan data; sebaliknya, parameter ini mendefinisikan pemrosesan yang dilakukan di dalam tipe objek — misalnya, memvalidasi data sebelum mengisi objek. Parameter ini dideklarasikan dalam spesifikasi tipe objek dan didefinisikan dalam badan tipe objek. Badan bersifat opsional: jika tidak ada anggota, tipe objek tidak memiliki bagian badan.
Buat Objek di Oracle
An object type cannot be created at the subprogram level; it can be created only at the schema level. Once the object type is defined in the schema, it can be used in subprograms. The object type is created using the "CREATE TYPE" statement, and the type body can be created only after its object type exists.
Tangkapan layar di bawah ini menunjukkan sintaks CREATE TYPE untuk spesifikasi objek dan sintaks CREATE TYPE BODY yang mendefinisikan metode-metodenya.
CREATE TYPE<object_type_name> AS OBJECT ( <attribute_l><datatype>, . . ); / CREATE TYPE BODY<object_type_name> AS OBJECT ( MEMBER[PROCEDURE|FUNCTION]<member_name> IS <declarative section> BEGIN <execution part> END; . . ); /
Penjelasan Sintaks:
- Sintaks di atas menunjukkan pembuatan OBJECT dengan atribut dan BODY OBJECT dengan metode.
- Metodenya juga bisa kelebihan beban di badan objek.
Inisialisasi Deklarasi Tipe Objek
Like other components in PL/SQL, object types must be declared before they are used in a program. Once the object type is created, it can be used in a subprogram's declarative section to declare a variable of that object type.
Setiap kali sebuah variabel dideklarasikan sebagai tipe objek, pada saat eksekusi (runtime) akan dibuat sebuah instance baru dari tipe objek tersebut, dan instance yang baru dibuat ini akan dirujuk oleh nama variabel tersebut. Dengan cara ini, satu tipe objek dapat menyimpan beberapa nilai di bawah instance yang berbeda.
Tangkapan layar di bawah ini menunjukkan sebuah variabel yang dideklarasikan sebagai tipe objek di dalam sebuah blok PL/SQL.
DECLARE <variable_name> <object_type_name>; BEGIN . . END; /
Penjelasan Sintaks:
- Sintaks di atas menunjukkan deklarasi sebuah variabel sebagai tipe objek di bagian deklaratif.
Setelah variabel dideklarasikan sebagai tipe objek dalam subprogram, variabel tersebut secara atomik bernilai null — seluruh objek itu sendiri bernilai null. Objek tersebut harus diinisialisasi dengan nilai sebelum dapat digunakan dalam program. Objek diinisialisasi menggunakan konstruktor.
Konstruktor adalah metode implisit dari sebuah objek yang dapat dirujuk dengan nama yang sama dengan tipe objek tersebut. Tangkapan layar di bawah ini menunjukkan inisialisasi sebuah instance tipe objek.
DECLARE <variable_name> <object_type_name>; BEGIN <variable_name>:=<object_type_name>(); END; /
Penjelasan Sintaks:
- Sintaks di atas menunjukkan inisialisasi instance tipe objek dengan nilai null.
- Objek itu sendiri tidak lagi bernilai null setelah diinisialisasi, tetapi atribut di dalam objek tetap bernilai null sampai nilai diberikan kepadanya.
Konstruktor
Konstruktor adalah metode implisit dari sebuah objek yang dapat dirujuk dengan nama yang sama dengan tipe objek tersebut. Setiap kali objek dirujuk untuk pertama kalinya, konstruktor ini dipanggil secara implisit.
Anda juga dapat menginisialisasi objek menggunakan konstruktor ini. Konstruktor dapat didefinisikan secara eksplisit dengan mendefinisikan anggota dalam badan tipe objek dengan nama yang sama dengan tipe objek tersebut.
Contoh 1: In the following example, we use the object type member to insert records into the emp table with the values ('RRR', 1005, 20000, 1000) and ('PPP', 1006, 20000, 1001). Once the data is inserted, we display it using the object type member. We also use an explicit constructor to populate the manager id with the default value 1001 for the second record.
Kami melaksanakannya dalam langkah-langkah berikut.
- Langkah 1: Buat tipe Objek dan isi tipe Objek.
- Langkah 2: Buat blok anonim untuk memanggil tipe objek melalui konstruktor implisit untuk emp_no 1005.
- Langkah 3: Buat blok anonim untuk memanggil tipe objek melalui konstruktor eksplisit untuk emp_no 1006.
Langkah 1) Buat tipe Objek dan isi tipe Objek.
Tangkapan layar di bawah ini menunjukkan spesifikasi tipe objek emp_object yang sedang dibuat beserta atribut dan anggotanya.
CREATE TYPE emp_object AS OBJECT( emp_no NUMBER, emp_name VARCHAR2(50), salary NUMBER, manager NUMBER, CONSTRUCTOR FUNCTION emp_object(p_emp_no NUMBER, p_emp_name VARCHAR2, p_salary NUMBER) RETURN SELF AS RESULT), MEMBER PROCEDURE insert_records, MEMBER PROCEDURE display_records); /
Screenshot di bawah ini menunjukkan pembuatan body tipe emp_object dengan konstruktor eksplisit dan prosedur anggota.
CREATE OR REPLACE TYPE BODY emp_object AS CONSTRUCTOR FUNCTION emp_object(p_emp_no NUMBER,p_emp_name VARCHAR2, p_salary NUMBER) RETURN SELF AS RESULT IS BEGIN Dbms_output.put_line('Constructor fired..'); SELF.emp_no:=p_emp_no; SELF.emp_name:=p_emp_name; SELF.salary:=p_salary; SELF.manager:=1001; RETURN; END; MEMBER PROCEDURE insert_records IS BEGIN INSERT INTO emp VALUES(emp_no,emp_name,salary,manager); END; MEMBER PROCEDURE display_records IS BEGIN Dbms_output.put_line('Employee Name:'||emp_name); Dbms_output.put_line('Employee Number:'||emp_no); Dbms_output.put_line('Salary:'||salary); Dbms_output.put_line('Manager:'||manager); END; END; /
Code Penjelasan
- Code baris 1-9: Creating the 'emp_object' object type with 4 attributes and 3 members. It contains the definition of a constructor with only 3 parameters. (The actual implicit constructor contains the number of parameters equal to the number of attributes present in the object type.)
- Code baris 10: Membuat isi teks.
- Code baris 11-21: Defining the explicit constructor. Assigning the parameter values to the attributes and assigning the attribute 'manager' the default value '1001'.
- Code baris 22-26: Defining the member 'insert_records', in which the attribute values are inserted into the 'emp' table.
- Code baris 27-34: Defining the member 'display_records', which displays the values of the object type attributes.
Keluaran:
Ketik dibuat
Tipe badan dibuat
Langkah 2) Membuat blok anonim untuk memanggil tipe objek yang telah dibuat melalui konstruktor implisit untuk emp_no 1005.
Tangkapan layar di bawah ini menunjukkan blok anonim yang memanggil emp_object melalui konstruktor implisit.
DECLARE guru_emp_det emp_object; BEGIN guru_emp_det:=emp_object(1005,'RRR',20000,1000); guru_emp_det.display_records; guru_emp_det.insert_records; COMMIT; END;
Code Penjelasan
- Code baris 37-45: Memasukkan data menggunakan konstruktor implisit. Panggilan ke konstruktor berisi jumlah nilai atribut yang sebenarnya.
- Code baris 38: Declares 'guru_emp_det' as object type 'emp_object'.
- Code baris 41: The statement 'guru_emp_det.display_records' calls the 'display_records' member and the attribute values are displayed.
- Code baris 42: The statement 'guru_emp_det.insert_records' calls the 'insert_records' member and the attribute values are inserted into the table. The block then commits the ..
Keluaran:
Nama Karyawan : RRR
Nomor Karyawan: 1005
Gaji: 20000
Manajer : 1000
Langkah 3) Membuat blok anonim untuk memanggil tipe objek yang telah dibuat melalui konstruktor eksplisit untuk emp_no 1006.
Tangkapan layar di bawah ini menunjukkan blok anonim yang memanggil emp_object melalui konstruktor eksplisit dengan tiga argumen.
DECLARE guru_emp_det emp_object; BEGIN guru_emp_det:=emp_object(1006,'PPP',20000); guru_emp_det.display_records; guru_emp_det.insert_records; COMMIT; END; /
Keluaran
Employee Name:PPP Employee Number:1006 Salary:20000 Manager:1001
Code Penjelasan:
- Code baris 46-53: Memasukkan data menggunakan konstruktor eksplisit.
- Code baris 46: Declares 'guru_emp_det' as object type 'emp_object'.
- Code baris 50: The statement 'guru_emp_det.display_records' calls the 'display_records' member and the attribute values are displayed.
- Code baris 51: The statement 'guru_emp_det.insert_records' calls the 'insert_records' member and the attribute values are inserted into the table. Because only three arguments are passed, the explicit constructor sets the manager id to its default value 1001.
Warisan dalam Tipe Objek
Properti pewarisan memungkinkan tipe objek turunan untuk mengakses semua atribut dan anggota dari tipe objek induk, atau tipe objek utama.
Tipe objek turunan disebut tipe objek yang diwariskan, dan tipe objek induk disebut tipe objek induk. Sintaks di bawah ini menunjukkan cara membuat tipe objek induk dan turunan. Tangkapan layar di bawah ini menunjukkan sintaks tipe induk (SUPER) yang ditandai sebagai NOT FINAL.
CREATE TYPE <object_type_name_parent> AS OBJECT ( <attribute_l><datatype>, . . )NOT FINAL; /
Penjelasan Sintaks:
- Sintaks di atas menunjukkan pembuatan tipe SUPER. Klausa NOT FINAL memungkinkan tipe tersebut untuk diwariskan.
Tangkapan layar di bawah ini menunjukkan sintaks tipe yang diwariskan (SUB) yang dibuat dengan kata kunci UNDER.
CREATE TYPE<object_type_name_sub>UNDER<object_type_name_parent> ( <attribute_l><datatype>, . ); /
Penjelasan Sintaks:
- Sintaks di atas menunjukkan pembuatan tipe SUB. Tipe ini berisi semua anggota dan atribut dari tipe objek induk.
Contoh 1: In the example below, we use the inheritance property to insert a record with manager id '1002' for the record ('RRR', 1007, 20000). We execute the program in the following steps.
- Langkah 1: Buat tipe SUPER.
- Langkah 2: Buat tipe SUB dan isi.
- Langkah 3: Buat blok anonim untuk memanggil tipe SUB.
Langkah 1) Buat tipe SUPER atau tipe Induk.
Screenshot di bawah ini menunjukkan supertipe emp_object yang dibuat dengan klausa NOT FINAL.
CREATE TYPE emp_object AS OBJECT( emp_no NUMBER, emp_name VARCHAR2(50), salary NUMBER, manager NUMBER, CONSTRUCTOR FUNCTION emp_object(p_emp_no NUMBER,p_emp_name VARCHAR2(50), p_salary NUMBER)RETURN SELF AS RESULT), MEMBER PROCEDURE insert_records, MEMBER PROCEDURE display_records)NOT FINAL; /
Code Penjelasan:
- Code baris 1-9: Creating the 'emp_object' object type with 4 attributes and 3 members. It contains the definition of a constructor with only 3 parameters. It has been declared as 'NOT FINAL', so it is a parent type.
Langkah 2) Buat tipe SUB di bawah tipe SUPER.
Tangkapan layar di bawah ini menunjukkan tipe turunan sub_emp_object dan isi (body) yang sedang dibuat.
CREATE OR REPLACE TYPE sub_emp_object UNDER emp_object (default_manager NUMBER,MEMBER PROCEDURE insert_default_mgr); / CREATE OR REPLACE TYPE BODY sub_emp_object AS MEMBER PROCEDURE insert_default_mgr IS BEGIN INSERT INTO emp VALUES(emp_no,emp_name,salary,manager); END; END; /
Code Penjelasan:
- Code baris 10-13: Creating 'sub_emp_object' as the inherited type with one additional attribute 'default_manager' and a member procedure declaration.
- Code baris 14: Membuat isi untuk tipe objek yang diwariskan.
- Code baris 16-21: Defining the member procedure that inserts records into the 'emp' table with the values from the SUPER object type, except for the manager value. For the manager value, it uses 'default_manager' from the SUB type.
Langkah 3) Membuat blok anonim untuk memanggil tipe SUB.
Tangkapan layar di bawah ini menunjukkan blok anonim yang memanggil tipe SUB dan menyisipkan ID manajer default.
DECLARE guru_emp_det sub_emp_object; BEGIN guru_emp_det:= sub_emp_object(1007,'RRR',20000,1000,1002); guru_emp_det.insert_default_mgr; COMMIT; END; /
Code Penjelasan:
- Code baris 25: Declaring 'guru_emp_det' as 'sub_emp_object' type.
- Code baris 27: Menginisialisasi objek dengan konstruktor implisit. Konstruktor memiliki 5 parameter (4 atribut dari tipe PARENT dan 1 atribut dari tipe SUB). Parameter terakhir (1002) mendefinisikan nilai untuk atribut default_manager.
- Code baris 28: Calling the member 'insert_default_mgr' to insert the record with the default manager id passed in the constructor.
Kesetaraan Objek PL/SQL
Instance objek yang termasuk dalam tipe objek yang sama dapat dibandingkan kesamaannya. Untuk melakukan ini, tipe objek tersebut memerlukan metode khusus yang disebut metode ORDER.
Metode ORDER ini harus berupa fungsi yang mengembalikan tipe numerik. Metode ini menerima dua parameter sebagai input (parameter pertama: id dari instance objek itu sendiri; parameter kedua: id dari instance objek lain).
ID dari kedua instance objek dibandingkan, dan hasilnya dikembalikan sebagai angka.
- Nilai positif menunjukkan bahwa instance objek SELF lebih besar daripada instance lainnya.
- Nilai negatif menunjukkan bahwa instance objek SELF lebih kecil daripada instance lainnya.
- Angka nol menunjukkan bahwa instance objek SELF sama dengan instance lainnya.
- Jika salah satu instance bernilai null, fungsi akan mengembalikan null.
Screenshot di bawah ini menunjukkan sintaks fungsi anggota ORDER yang harus disertakan dalam badan tipe untuk pemeriksaan kesamaan.
CREATE TYPE BODY<object_type_name_ 1>AS OBJECT ( ORDER MEMBER FUNCTION match(<parameter> object_type_name_ 1) RETURN INTEGER IS BEGIN IF <attribute_name>parameter <attribute_name>THEN RETURN -1; --any negative number will do ELSIF id>c.id THEN RETURN 1; --any positive number will do ELSE RETURN 0; END IF; END; . . ); /
Penjelasan Sintaks:
- Sintaks di atas menunjukkan fungsi ORDER yang harus disertakan dalam badan tipe untuk pemeriksaan kesamaan.
- Parameter untuk fungsi ini harus berupa turunan dari tipe objek yang sama.
- The function can be called as "obj_instance_1.match(obj_instance_2)", and this expression returns the numerical value shown, where obj_instance_1 and obj_instance_2 are instances of the object type.
Contoh 1: In the following example, we compare two objects. We create two instances and compare the 'salary' attribute between them. We do this in two steps.
- Langkah 1: Buat tipe objek dan isi objek.
- Langkah 2: Buat blok anonim untuk membandingkan instance objek.
Langkah 1) Membuat tipe dan isi objek.
Tangkapan layar di bawah ini menunjukkan spesifikasi emp_object_equality dan isinya, yang mendefinisikan fungsi ORDER yang membandingkan atribut gaji.
CREATE TYPE emp_object_equality AS OBJECT( salary NUMBER, ORDER MEMBER FUNCTION equals(c emp_object_equality)RETURN INTEGER); /
CREATE TYPE BODY emp_object_equality AS ORDER MEMBER FUNCTION equals(c emp_object_equality)RETURN INTEGER IS BEGIN IF salary<c.salary THEN RETURN -1; ELSIF salary>c.salary THEN RETURN 1; ELSE RETURN 0; END IF; END; END; /
Code Penjelasan:
- Code baris 1-4: Creating the 'emp_object_equality' object type with 1 attribute and 1 member.
- Code baris 6-16: Defining the ORDER function that compares the 'salary' attribute of the SELF instance and the parameter instance. It returns a negative value if the SELF salary is lesser, a positive value if the SELF salary is greater, and 0 if the salaries are equal.
Code Keluaran:
Ketik dibuat
Langkah 2) Membuat blok anonim untuk membandingkan instance objek.
Tangkapan layar di bawah ini menunjukkan blok anonim yang membandingkan dua instance emp_object_equality berdasarkan gaji.
DECLARE l_obj_1 emp_object_equality; l_obj_2 emp_object_equality; BEGIN l_obj_1:=emp_object_equality(15000); l_obj_2:=emp_object_equality(17000); IF l_obj_1.equals(l_obj_2)>0 THEN Dbms_output.put_line('Salary of first instance is greater'); ELSIF l_obj_1.equals(l_obj_2)<0 THEN Dbms_output.put_line('Salary of second instance is greater'); ELSE Dbms_output.put_line('Salaries are equal'); END IF; END; /
Keluaran
Salary of second instance is greater
Code Penjelasan:
- Code baris 20: Declaring 'l_obj_1' of 'emp_object_equality' type.
- Code baris 21: Declaring 'l_obj_2' of 'emp_object_equality' type.
- Code baris 23: Initializing 'l_obj_1' with the salary value '15000'.
- Code baris 24: Initializing 'l_obj_2' with the salary value '17000'.
- Code baris 25-33: Mencetak pesan berdasarkan angka yang dikembalikan oleh fungsi ORDER.


















