Oracle Database 10g The Complete Reference phần 6 pptx

135 346 0
Oracle Database 10g The Complete Reference phần 6 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

back to CUSTOMER—you cannot record a call for a customer who does not already have a record in CUSTOMER. A single nonkey attribute, Call_Date, is created within the CUSTOMER_CALL table. create table CUSTOMER_CALL (Customer_ID NUMBER, Call_Number NUMBER, Call_Date DATE, constraint CUSTOMER_CALL_PK primary key (Customer_ID, Call_Number), constraint CUSTOMER_CALL_FK foreign key (Customer_ID) references CUSTOMER(Customer_ID)); For example, you could have the following CUSTOMER and CUSTOMER_CALL entries: insert into CUSTOMER values (123,'SIGMUND','47 HAFFNER RD','LEWISTON','NJ',22222); insert into CUSTOMER values (234,'EVELYN','555 HIGH ST','LOWLANDS PARK','NE',33333); insert into CUSTOMER_CALL values (123,1,TRUNC(SysDate)-1); insert into CUSTOMER_CALL values (123,2,TRUNC(SysDate)); The foreign key from CUSTOMER_CALL to CUSTOMER defines the relationship between the tables. From an OOP point of view, the records in CUSTOMER_CALL reference the records in CUSTOMER. Therefore, we must find a way to assign OID values to the records in CUSTOMER and generate references in CUSTOMER_CALL. How to Generate OIDs First, use an object view to assign OIDs to the records in CUSTOMER. Remember that OIDs are assigned to records in an object table—and an object table, in turn, is based on an abstract datatype. Therefore, we first need to create an abstract datatype that has the same structure as the CUSTOMER table: create or replace type CUSTOMER_TY as object (Customer_ID NUMBER, Name VARCHAR2(25), Street VARCHAR2(50), City VARCHAR2(25), State CHAR(2), Zip NUMBER); / Now, create an object view based on the CUSTOMER_TY type, while assigning OID values to the records in CUSTOMER: create or replace view CUSTOMER_OV of CUSTOMER_TY with object identifier (Customer_ID) as 658 Part V: Object-Relational Databases ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36:658 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:25 PM Color profile: Generic CMYK printer profile Composite Default screen select Customer_ID, Name, Street, City, State, Zip from CUSTOMER; The first part of this create view command gives the view its name (CUSTOMER_OV) and tells Oracle that the view’s structure is based on the CUSTOMER_TY datatype: create or replace view CUSTOMER_OV of CUSTOMER_TY The next part of the create view command tells the database how to construct OID values for the rows in CUSTOMER. The with object identifier clause is followed by the column to use for the OID—in this case, the Customer_ID value. This will allow you to address the rows within the CUSTOMER table as if they were referenceable row objects within an object table. with object identifier (Customer_ID) as The final part of the create view command gives the query on which the view’s data access will be based. The columns in the query must match the columns in the view’s base datatype. select Customer_ID, Name, Street, City, State, Zip from CUSTOMER; The rows of CUSTOMER are now accessible as row objects via the CUSTOMER_OV view. The OID values generated for the CUSTOMER_OV rows are called pkOID s, because they are based on CUSTOMER’s primary key values. Relational tables can be accessed as row objects if you create object views for them. How to Generate References The rows of CUSTOMER_CALL reference rows in CUSTOMER. From a relational perspective, the relationship is determined by the foreign key pointing from the CUSTOMER_CALL.Customer_ID column to the CUSTOMER.Customer_ID column. Now that the CUSTOMER_OV object view has been created, and the rows in CUSTOMER can be accessed via OIDs, you need to create reference values in CUSTOMER_CALL that reference CUSTOMER. Once the REFs are in place, you will be able to use the DEREF function (shown earlier in this chapter) to access the CUSTOMER data from within CUSTOMER_CALL. The create view command for the object view of CUSTOMER_CALL is shown in the following listing. It uses a new function, MAKE_REF, which is described following the listing. create view CUSTOMER_CALL_OV as select MAKE_REF(CUSTOMER_OV, Customer_ID) Customer_ID, Call_Number, Call_Date from CUSTOMER_CALL; With the exception of the MAKE_REF operation, this create view command looks like a normal create view command. The MAKE_REF operation is shown in this line: select MAKE_REF(CUSTOMER_OV, Customer_ID) Customer_ID, Chapter 36: Advanced Object-Oriented Concepts 659 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36:659 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:26 PM Color profile: Generic CMYK printer profile Composite Default screen The MAKE_REF function takes as arguments the name of the object view being referenced and the name of the column (or columns) that form the foreign key in the local table. In this case, the Customer_ID column of the CUSTOMER_CALL table references the column that is used as the basis of OID generation in the CUSTOMER_OV object view. Therefore, two parameters are passed to MAKE_REF: CUSTOMER_OV and Customer_ID. The result of the MAKE_REF operation is given the column alias Customer_ID. Since this command creates a view, the result of an operation must be given a column alias. What does MAKE_REF do? It creates references (called pkREF s, since they are based on primary keys) from the CUSTOMER_CALL_OV view to the CUSTOMER_OV view. You can now query the two views as if CUSTOMER_OV were an object table and CUSTOMER_CALL_OV were a table that contains a REF datatype that references CUSTOMER_OV. Querying the Object Views The queries of the object views with REFs mirror the structure of the queries of table REFs. You use the DEREF function to select the value of the referenced data, as shown earlier in this chapter. Applied to the object views, the query will be select DEREF(CCOV.Customer_ID) from CUSTOMER_CALL_OV CCOV where Call_Date = TRUNC(SysDate); DEREF(CCOV.CUSTOMER_ID)(CUSTOMER_ID, NAME, STREET, CITY, STATE,ZIP) CUSTOMER_TY(123, 'SIGMUND', '47 HAFFNER RD', 'LEWISTON','NJ',22222) The query found the record in CUSTOMER_CALL for which the Call_Date value was the current system date. It then took the Customer_ID value from that record and evaluated its reference. That Customer_ID value, from the MAKE_REF function, pointed to a pkOID value in the CUSTOMER_ OV object view. The CUSTOMER_OV object view returned the record whose pkOID matched the referenced value. The DEREF function then returned the value of the referenced row. The query thus returned rows from CUSTOMER even though the user only queried CUSTOMER_CALL. Object views of column objects enable you to work with tables as if they were both relational tables and object-relational tables. When extended to row objects, object views enable you to generate OID values based on established foreign key/primary key relationships. Object views allow you to continue to use the existing constraints and standard insert, update, delete, and select commands. They also allow you to use OOP features such as references against the object tables. Object views thus provide an important technological bridge for migrating to an OOP database architecture. As described earlier in this chapter, Oracle performs joins that resolve the references defined in the database. When the referenced data is retrieved, it brings back the entire row object that was referenced. To reference the data, you need to establish pkOIDs in the table that is the “primary key” table in the relationship, and use MAKE_REF to generate references in the table that is the “foreign key” table in the relationship. You can then work with the data as if it were stored in object tables. 660 Part V: Object-Relational Databases ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36:660 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:26 PM Color profile: Generic CMYK printer profile Composite Default screen Object PL/SQL PL/SQL programs can use the abstract datatypes you have created. Whereas earlier versions of PL/SQL could only use the Oracle-provided datatypes (such as DATE, NUMBER, and VARCHAR2), you can now use your own user-defined datatypes as well. The result is the merging of SQL, procedural logic, and OOP extensions—a combination referred to as object PL/SQL. The following anonymous PL/SQL block uses object PL/SQL concepts. The CUSTOMER_TY abstract datatype is used as the datatype for the Cust1 variable, and its value is populated by a query of the CUSTOMER_OV object view. set serveroutput on declare Cust1 CUSTOMER_TY; begin select VALUE(COV) into Cust1 from CUSTOMER_OV COV where Customer_ID = 123; DBMS_OUTPUT.PUT_LINE(Cust1.Name); DBMS_OUTPUT.PUT_LINE(Cust1.Street); end; / The output of this PL/SQL block is SIGMUND 47 HAFFNER RD In the first part of the PL/SQL block, a variable is declared using the CUSTOMER_TY datatype: declare Cust1 CUSTOMER_TY; The Cust1 variable value is selected from the CUSTOMER_OV object view (created in the preceding section of this chapter). The VALUE function is used to retrieve the data in the structure of the abstract datatype. Since the data will be selected in the format of the abstract datatype, you need to use the CUSTOMER_OV object view created on the CUSTOMER table. begin select VALUE(COV) into Cust1 from CUSTOMER_OV COV where Customer_ID = 123; The Cust1.Name and Cust1.Street values are then retrieved from the attributes of the Cust1 variable and displayed. You cannot pass the entire Cust1 variable to the PUT_LINE procedure. DBMS_OUTPUT.PUT_LINE(Cust1.Name); DBMS_OUTPUT.PUT_LINE(Cust1.Street); end; Chapter 36: Advanced Object-Oriented Concepts 661 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36:661 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:27 PM Color profile: Generic CMYK printer profile Composite Default screen This is a deliberately simple example, but it shows the power of object PL/SQL. You can use object PL/SQL anywhere you use abstract datatypes. Your PL/SQL is thus no longer bound to the Oracle-provided datatypes, and may more accurately reflect the objects in your database. In this example, an object view was queried to illustrate that the queries can access either column objects or row objects. You can then select the attributes of the abstract datatype and manipulate or display them. If you have defined methods for the abstract datatype, you can apply them as well. For example, you can call the datatype’s constructor methods within your PL/SQL blocks. In the following example, a variable named NewCust is defined using the CUSTOMER_TY datatype. The NewCust variable is then set equal to a set of values using the CUSTOMER_TY constructor method. The NewCust variable’s set of values is then inserted via the CUSTOMER_OV object view. declare NewCust CUSTOMER_TY; begin NewCust := CUSTOMER_TY(345,'NewCust','StreetVal', 'City','ST',00000); insert into CUSTOMER_OV values (NewCust); end; / You can see the result of the insert by querying CUSTOMER_OV: select Customer_ID, Name from CUSTOMER_OV; CUSTOMER_ID NAME 123 SIGMUND 234 EVELYN 345 NewCust In addition to calling constructor methods, you can call the methods you have created on your abstract datatypes. If you will be comparing the values of variables that use the abstract datatypes, you will need to define map or order methods for the datatypes. This capability allows you to further extend object PL/SQL—you define the datatypes and the functions at the database level, and they are callable within any of your PL/SQL programs. Object PL/SQL represents a significant enhancement over traditional PL/SQL. Objects in the Database The features available in Oracle—column objects, row objects, and object extensions to PL/SQL— enable you to implement objects in your database without sacrificing the investment you have already made in analysis and design. You can continue to create systems based on relational design techniques and tune them based on relational access methods. The tools that Oracle provides allow you to create an OOP layer above your relational tables. Once you have that layer in place, you can access the relational data as if it were stored in a fully OOP database. Having an OOP layer allows you to realize some of the benefits of an OOP system, such as abstraction and encapsulation. You can apply the methods for each abstract datatype across a set 662 Part V: Object-Relational Databases ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36:662 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:28 PM Color profile: Generic CMYK printer profile Composite Default screen of consistently implemented objects, and benefit from object reuse and standards enforcement. At the same time, you can benefit from Oracle’s relational features. The ability to use both relational and object technology within an application lets you use the proper tool for the proper job within the database. Note that implementing object-relational features may force you to change your approach to data relationships and the tools you use to access the data. If you use row objects, the relationships among tables are among row objects, not their data—possibly resulting in dangling REFs. Additionally, unless you use object views you will need to use new SQL syntax for queries and DML operations. When implementing the object portion of an object-relational database, start by defining the abstract datatypes that are the core components of your business. Every object-relational feature, whether it relates to column objects or row objects, is based on an abstract datatype. The better you have defined your datatypes and their methods, the better you will be able to implement objects. If necessary, nest objects so that you can have multiple variations of the same core datatype. The result will be a database that is properly designed to take advantage of both relational and object- oriented features. Chapter 36: Advanced Object-Oriented Concepts 663 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36:663 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:28 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 37 Blind Folio 37:665 PART VI Java in Oracle P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:29 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 37 Blind Folio 37:666 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:29 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 37 Blind Folio 37:667 CHAPTER 37 An Introduction to Java P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:29 PM Color profile: Generic CMYK printer profile Composite Default screen [...]... Default screen 66 8 Part VI: ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 37 Blind Folio 37 :66 8 Java in Oracle n this chapter, you will see an overview of Java as it relates to Oracle database applications There are numerous uses of Java beyond Oracle applications, and there are many features of the language that will not be used by most Oracle developers The goal of this... install the JDK on the server on which you will be running the Java programs As of the time of this writing, JDK 1.4 is the most recent production release; most development efforts use JDK 1.3.1 At a minimum, you should use JDK 1.2.2 NOTE The JDK is installed as part of the Oracle Database 10g installation Be sure to install both the database kernel software and the software that comes on the Companion... included in the CLASSPATH environment variable The programs referenced here (javac and java) are located in the /bin subdirectory of the JDK software directory First, save the program as a plain text file called HelloOracle.java Next, compile it: javac HelloOracle.java A new file, called HelloOracle.class, will be created on your server You can then run the class: java HelloOracle Then the output will... example, the if clause is evaluated, followed by the switch clause Note that the aMonth variable is not declared until just before it is needed For this example, the aMonth variable is assigned a value of 2, so the program will display the word “February” and will then branch over to the section of code identified by the someotherlabel label If the aMonth value had been 1, the if clause at the beginning... } For performance reasons, the && and || operations are carried out only if required; the second expression is not evaluated if the first expression determines the result In the case of the AND operator, if the first value is false, the evaluation ends at that point and the Boolean value false is returned In the case of the OR operator, if the first expression is false, the second expression will still... Folio 37 :67 2 Java in Oracle In the following listing, the Character.isUpperCase method is used to evaluate the aCharVariable variable declared earlier: if (Character.isUpperCase(aCharVariable)) { some commands to execute } If the aCharVariable value is uppercase, the commands in the following block will be executed; otherwise, flow will continue to the next part of the program The following is the general... Chapter 38 Blind Folio 38 :68 5 CHAPTER 38 JDBC Programming P:\010Comp \Oracle8 \351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:51:44 PM Color profile: Generic CMYK printer profile Composite Default screen 68 6 Part VI: ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 38 Blind Folio 38 :68 6 Java in Oracle ava Database Connectivity (JDBC) builds on the Java and programming basics... Within the HelloOracle class, a public method called main is created The main method prints the word Oracle. ” This is significantly more complex than select 'Oracle' from dual; but the Java version has features lacking in the SQL version HelloOracle is a class, and as such its methods can be called from within other classes HelloOracle could have multiple methods; in this example, it has only the main... Default screen 68 0 Part VI: ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 37 Blind Folio 37 :68 0 Java in Oracle NOTE To compile and execute a Java class, you must have the JDK on your server The following examples assume that the binaries that are part of that kit are located in a directory that is automatically searched via the PATH environment variable and that the directory... variable’s value does not exceed 7, the block will be executed For each pass through the loop, the variable’s value is incremented by 1 via the ++ unary operator Within a loop, you can use the continue clause to jump to another statement within the loop If you just use the continue clause by itself, the process flow will jump to the end of the loop body and evaluate the loop’s termination test If you . procedure. DBMS_OUTPUT.PUT_LINE(Cust1.Name); DBMS_OUTPUT.PUT_LINE(Cust1.Street); end; Chapter 36: Advanced Object-Oriented Concepts 66 1 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36: 661 P:10Comp Oracle8 351-7CDVenturaook.vp Friday,. datatype across a set 66 2 Part V: Object-Relational Databases ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36: 662 P:10Comp Oracle8 351-7CDVenturaook.vp Friday,. Customer_ID, Chapter 36: Advanced Object-Oriented Concepts 65 9 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 36 Blind Folio 36: 659 P:10Comp Oracle8 351-7CDVenturaook.vp Friday,

Ngày đăng: 08/08/2014, 20:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan