ORACLE PL/SQL ¶ÔÏó(object)ѧϰ±Ê¼Ç(Ò»)
1¡¢¶ÔÏóÀàÐ͹淶
´´½¨¶ÔÏóÀàÐ͹淶µÄÓï·¨ÈçÏÂ
CREATE [OR REPLACE] TYPE [schema.] type_name
[AUTHID {CURRENT_USER|DEFINER}] AS OBJECT (
Attribute1 datatype,
[attribute2 datatype,…]
[method 1]
[method 2]);
/
ÆäÖÐAUTHIDָʾ½«À´Ö´Ðи÷½·¨Ê±£¬±ØÐëʹÓÃÔÚ´´½¨Ê±¶¨ÒåµÄCURRENT_USER»òDEFINERµÄȨÏÞ¼¯ºÏ¡£CURRENT_USERÊǵ÷Óø÷½·¨µÄÓû§£¬DEFINERÊǸöÔÏóÀàÐ͵ÄËùÓÐÕß¡£
1£©ÊôÐÔ
ÊôÐÔµÄÉùÃ÷ÓÐһЩÏÞÖÆ£¬°üÀ¨£º
ÊôÐÔµÄÉùÃ÷±ØÐë³öÏÖÔÚ·½·¨µÄÉùÃ÷ÒÔÇ°¡£
Êý¾ÝÀàÐÍ¿ÉÒÔÊÇÈκÎÊý¾Ý¿âÊý¾ÝÀàÐÍ£¬µ«ÊDz»ÄÜ°üÀ¨ROWID,UROWID,LONG,LONG RAW,NCHAR,NCLOB,NVARCHAR2ÀàÐÍ£¬ÒÔ¼°PL/SQLµÄרÓÃÀàÐÍ»òÔÚPL/SQL°üÖж¨ÒåµÄÀàÐÍ¡£
²»ÄÜʹÓÃÄÇЩֻÄÜÔÚPL/SQLÖÐʹÓöø²»ÄÜÔÚÊý¾Ý¿âÖÐʹÓõÄÊý¾ÝÀàÐÍ¡£ÕâЩÀàÐÍ°üÀ¨BINARY_INTEGER,BOOLEAN,PLS_INTEGER,RECORDºÍREF CURSOR¡£
²»ÄÜʹÓÃNOT NULLÔ¼Êø£¬µ«ÊÇ¿ÉÒÔͨ¹ýÔÚ¶ÔÏóʵÀýÉ϶¨ÒåÒ»¸öÊý¾Ý¿â´¥·¢Æ÷À´´ïµ½ÏàͬЧ¹û¡£
ÊôÐÔÁбíÖÐÖÁÉÙ±ØÐëÓÐÒ»¸öÊôÐÔ¡£
²»ÄÜʹÓÃĬÈÏÖµ¡£
×¢Ò⣺²»ÄÜÖ±½ÓÔÚÊôÐÔ»ò¶ÔÏóÀàÐÍÉÏʹÓÃ%TYPEºÍ%ROWTYPE¡£µ«ÊÇ¿ÉÒÔÔÚ¶ÔÏóʵÀýµÄÊôÐÔÉÏʹÓÃËüÃÇ¡£
-- This example works fine. %TYPE is applied
-- to the variable, not the object type.
DECLARE
v_discount_price discount_price_obj;
v_price v_discount_price.price%TYPE;
BEGIN
NULL;
END;
/
-- This example throws an exception. %TYPE is applied
-- directly to the object type.
DECLARE
v_price discount_price_obj.price%TYPE;
BEGIN
NULL;
END;
/
¸´ºÏ¶ÔÏóÀàÐÍ
¸´ºÏ¶ÔÏóÀàÐÍ¿ÉÒÔÒÔij¸öÊôÐÔµÄÊý¾ÝÀàÐ͵ÄÐÎʽ£¬°üº¬ÁíÒ»¸ö¶ÔÏóÀàÐÍ¡£
CREATE OR REPLACE TYPE discount_price_obj AS OBJECT (
discount_rate NUMBER(10,4),
price NUMBER(10,2),
MEMBER FUNCTION discount_price RETURN NUMBER)
INSTANTIABLE
FINAL;
/
CREATE OR REPLACE TYPE inventory_obj AS OBJECT (
item_id NUMBER(10),
num_in_stock NUMBER(10),
reorder_status VARCHAR2(20),
price REF discount_price_obj);
/
2£©·½·¨
·½·¨¾ÍÊǹ
Ïà¹ØÎĵµ£º
1. µ±Ç°ÏµÍ³ÈÕÆÚ¡¢Ê±¼ä
select getdate()
2. dateadd ÔÚÏòÖ¸¶¨ÈÕÆÚ¼ÓÉÏÒ»¶Îʱ¼äµÄ»ù´¡ÉÏ£¬·µ»ØÐ嵀 datetime Öµ
ÀýÈ磺ÏòÈÕÆÚ¼ÓÉÏ2Ìì
&nbs ......
ÎÊÌ⣺¼ÙÉèÓÐÕÅѧÉú³É¼¨±í(tb)ÈçÏÂ:
ÐÕÃû ¿Î³Ì ·ÖÊý
ÕÅÈý ÓïÎÄ 74
ÕÅÈý Êýѧ 83
ÕÅÈý ÎïÀí 93
ÀîËÄ ÓïÎÄ 74
ÀîËÄ Êýѧ 84
ÀîËÄ ÎïÀí 94
Ïë±ä³É(µÃµ½ÈçϽá¹û)£º
ÐÕÃû ÓïÎÄ Êýѧ ÎïÀí
---- ---- --- ......
µÚʮһÌ⣺
Óбístudents(name,class,grade),ÇëÓñê×¼sqlÓï¾äÍê³É
name class grade
ÕÅÈý Êýѧ 81
ÀîËÄ ÓïÎÄ 70
ÍõÎå Êýѧ 90
ÕÅÈý ÓïÎÄ 60
ÀîËÄ Êýѧ 100
ÍõÎå ÓïÎÄ 90
ÍõÎå Ó¢Óï 81
ÒªÇó: ÓÃsqlÓï¾äÊä³ö¸÷ÃŹ¦¿Î¶¼´óÓÚ80·ÖµÄͬѧÐÕÃû?
create table students (
name varchar(25),
class varchar(25),
grad ......