Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

SQL*Plus: Release 9.2.0.1.0 - Production on Sat Sep 18 08:58:25 2004 Copyright (c) 1982, 2002, Oracle Corporation.

All rights reserved. Connected to: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production With the Partitioning, OLAP and Oracle Data Mining options JServer Release 9.2.0.1.0 - Production SQL> create or replace type name as object(name1 char(10),name2 char(10)); 2 / Type created. SQL> create table o(id number(10), 2 cname char(10)); Table created. SQL> insert into o values(9,'priya'); 1 row created. SQL> ed Wrote file afiedt.buf 1* insert into o values(9,'priya') SQL> SQL> create table o(id number(10) 2 cname char(10));/ 3 / create table o(id number(10) * ERROR at line 1: ORA-00922: missing or invalid option SQL> ed Wrote file afiedt.buf 1 create or replace table o(id number(10) 2* cname ) 3 ; create or replace table o(id number(10)

* ERROR at line 1: ORA-00922: missing or invalid option SQL> ed Wrote file afiedt.buf 1 create table o1(id number(10) 2* cname ) 3 ; create table o1(id number(10) * ERROR at line 1: ORA-00922: missing or invalid option SQL> ed Wrote file afiedt.buf 1 create table o1(id number(10) 2* cname name ) 3 ; create table o1(id number(10) * ERROR at line 1: ORA-00922: missing or invalid option SQL> ed Wrote file afiedt.buf 1 create table o1(id number(10), 2* cname name ) 3 ; Table created. SQL> insert into o1 values(01,name('p','p')); 1 row created. SQL> declare 2 a1 name; 3 begin 4 select s.cname into a1 from o1 where id=01;

5 dbms_output.put_line(a1.name1); 6 end; 7 / select s.cname into a1 from o1 where id=01; * ERROR at line 4: ORA-06550: line 4, column 8: PL/SQL: ORA-00904: "S"."CNAME": invalid identifier ORA-06550: line 4, column 1: PL/SQL: SQL Statement ignored SQL> ed Wrote file afiedt.buf 1 declare 2 a1 name; 3 begin 4 select s.cname into a1 from o1 s where id=01; 5 dbms_output.put_line(a1.name1); 6* end; SQL> / PL/SQL procedure successfully completed. SQL> set serveroutput on SQL> ed Wrote file afiedt.buf 1 declare 2 a1 name; 3 begin 4 select s.cname into a1 from o1 s where id=01; 5 dbms_output.put_line(a1.name1); 6* end; SQL> SQL> / p PL/SQL procedure successfully completed. SQL> desc o1; Name Null? Type ----------------------------------------- -------- ---------------------ID NUMBER(10) CNAME NAME

SQL> ed Wrote file afiedt.buf 1 declare 2 a1 name; 3 begin 4 select s.cname.name1 into a1 from o1 s where id=01; 5 dbms_output.put_line(a1.name1); 6* end; SQL> / select s.cname.name1 into a1 from o1 s where id=01; * ERROR at line 4: ORA-06550: line 4, column 8: PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHAR ORA-06550: line 4, column 1: PL/SQL: SQL Statement ignored SQL> ed Wrote file afiedt.buf 1 declare 2 a1 name; 3 begin 4 select s.cname.name1 into a1 from o1 s where id=01; 5 dbms_output.put_line(a1.name1); 6* end; 7 / select s.cname.name1 into a1 from o1 s where id=01; * ERROR at line 4: ORA-06550: line 4, column 8: PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHAR ORA-06550: line 4, column 1: PL/SQL: SQL Statement ignored SQL> ed Wrote file afiedt.buf 1 2 3 4 declare a1 name; begin select s.cname into a1 from o1 s where id=01;

5 dbms_output.put_line(a1.name1); 6* end; 7 / p PL/SQL procedure successfully completed. SQL>

rn Legitimate scalar column types and database types used in the same, in addition to some expansion of it. It is divided into seven groups: the number of characters, line, date, line marking, and the credibility of Boolean. rn Figures, it has three basic types - NUMBER, PLS_INTEGER, and BINARY_INTENER. Can be described NUMBER integer or real number, and PLS_INTEGER and can only be described BINARY_INTENER integer. rn NUMBER, is stored in decimal format, it is easy to store, but in the calculation, the system will automatically convert it to be a binary operation. It is the definition of NUMBER (P, S), P is the precision, the largest 38, S is the calibration range can be -84 ... 127 value. For example: NUMBER (5,2) that can be used to store -999.99 ... 999.99 value. P, S can be omitted in the definition, such as: NUMBER (5), NUMBER, etc.; rn Used to describe BINARY_INTENER not stored in the database, but with symbols used to calculate the integer value. It's up to 2 forms of expression binary code. Cycle counters often use this type. rn PLS_INTEGER and BINARY_INTENER The only difference is that in the calculation of overflow which occurred when, BINARY_INTENER type of variable will be automatically assigned to a NUMBER and not go wrong type, PLS_INTEGER variable type error will occur.

rn Characters, including the CHAR, VARCHAR2 (VARCHAR), LONG, NCHAR and NVARCHAR2 types. rn CHAR, description of fixed-length string, if not the actual value of the length of the definition, the system will be filled spaces. It's a statement as follows CHAR (L), L is the length of the string, the default is 1, as the largest variables 32,767 characters, as the data is stored on the largest ORACLE8 for 2000. rn VARCHAR2 (VARCHAR), description of variable-length string. It means the following statement VARCHAR2 (L), L is the length of the string, there is no default value, as the largest 32,767-byte variable, as data is stored on the largest ORACLE8 for 4000. Byte in a multi-language environment, the actual number of characters stored value may be smaller than L, for example: when the language environment in Chinese (SIMPLIFIED CHINESE_CHINA.ZHS16GBK), a VARCHAR2 (200) out of the data can be saved, or 200 English characters 100 Chinese characters. rn LONG, store in the database can be used to save up to 2G of data as a variable, can be expressed as a 32,760 byte maximum length of string variable. rn NCHAR, NVARCHAR2, the national character set specified with the NLS environment variable set of language is closely related to the use of methods and CHAR, VARCHAR2 same. rn Lines, including RAW and LONG RAW types. Used to store binary data, and not in the character set conversion between. rn RAW, similar to CHAR, a statement the way RAW (L), L for length, in bytes, as the largest database in 2000 listed as the biggest variable 32767 bytes. rn LONG RAW, similar to LONG, listed as the largest database of 2G bytes of data storage, as a maximum 32,760-byte variable. rn Date, only one type - DATE, the time used to store information, station 7 bytes (from the second century), there is no "millennium bug" problem. rn

Line marking, only one type - ROWID, used to store "line identifier" ROWIDTOCHAR function can be used to convert the line to become the characters logo. rn Boolean, only one type - BOOLEAN, can be expressed only TRUE, FALSE or NULL. rn Credible, there is only one type - MLSLABEL, can be used to save TRUSTED ORACLE binary variable-length tags. ORACLE in the standard, the only store NULL values. rn Third, composite (COMPOSITE) rn Scalar type is predefined, the use of these types can be derived from a number of complex types. There are records in table. rn Records, can be seen as a combination of scalar structure, it is a statement as follows: rn TYPE record_type_name IS RECORD rn (Filed1 type1 [NOT NULL] [: = expr1] rn ....... rn filedn typen [NOT NULL] [: = exprn]) rn Which, record_type_name is the record type name. (Is not watched as CREATE TABLE ?......) reference variables must be related to the definition, recording only a TYPE, not VARIABLE. rn Table, not the physical storage of data tables, here is a variable type, also known as PL / SQL table, it is similar to C language array, also in a similar way. It's a statement as follows: rn TYPE table_type_name IS TABLE OF scalar_type INDEX BY BINARY_INTENER; rn

Which, table_type_name is the type of name, scalar_type scalar type is a type of statement. Reference must also be related to the definition of variables. Table and the array is different from table 2, KEY and VALUE, KEY statement is the definition of BINARY_INTENER, VALUE statement is the definition of scalar_type. rn In addition to records and tables, there are object types, collections (nested tables and VARRAYS) type, which will be devoted to explain. rn Fourth, reference (REFERENCE) rn PL/SQL8.0 before in only one type - REF CURSOR, the cursor is. The definition of its relatively simple, rn CURSOR cursor_name IS select ..... from .....; rn PL/SQL8.0 after the introduction of the REF type, which point to an object. This type will be explained separately. (Such as I find out again, so in the face of an expert ......) rn 5, LOB type rn LOB variable is mainly used to store large amounts of data fields of the database, the largest 4G byte can store the content (as much, and keep VCD?, A waste!). Are: rn CLOB: The LONG and ORACLE7 type is similar to single-byte character data storage. (Oh do not be used to keep the Chinese...) rn NCLOB: used to store multi-byte character set data width. (For example: keep " Soaring Lake " on very good) rn BLOB: and LONG RAW is similar to the structure used to store non-binary data. rn BFILE: It ORACLE database used to allow outside storage of large binary form of the text read-only access. rn

Other rn In fact, to complete the understanding of scalar variables basically to meet the C / S mode of conventional database applications, and the type of follow-up to tell the truth, I play a 5-year ORACLE, are rarely used. rn Attachment: reference "ORACLE8 PL / SQL Programming" ( "ORACLE8 PL / SQL PROGRAMMING") ORACLE Series Mechanical Technology Industry Press rn Note: This article is cited by PL / SQL which can be defined data types, not all types can be specified in the building when the data type for the column.

Tags: oracle, expression, variables, varchar2, oracle data, data types, variable length, column types, binary operation, variable type, decimal format, database types, binary code, type error, integer value, date line, calibration, overflow, credibility

Pre-relational databases such as IMS and IDMS supported ADTs, but strong typing features were not introduced until about 1990, with the first commercial object-oriented databases. Actually, other database management systems have offered many of Oracle9is new features for years. For example, Dr. Wong Kim developed UniSQL, a relational/object-oriented database that supports the concept of nested tables. In UniSQL a data field in a table can be a range of values or an entire table. In this way, the domain of values can be defined for a specific field in a relational database can be. Relationship data can be incorporated directly into a table structure using nested data tables.

Having considered the basic idea behind abstract data types, we can investigate some of the compelling benefits to this approach. ADTs are useful within an Oracle9i design for several reasons:

Encapsulation - Abstract data types insure uniformity and consistency since each type exists as a complete unit. Each type includes the data definitions, default values, and value constraints. Moreover, an abstract data type can interact with other abstract data types. Regardless of where it appears in a database, the same logical data type always has the same definition, default values, and value constraints. Reusability - Common data structures can be reused within many definitions, insuring uniformity and saving coding time Flexibility - The database object designer is able to model the real world by creating real-world representations of data.

If the data types are properly analyzed and incorporated into the database object model, abstract data typing is a powerful tool. Next, well consider implementing abstract data typing in Oracle 9i. The requirement to model all types at their lowest level was one of the shortcomings of Oracle 7. For example, the address information of a customer could be accessed only by manipulating street_address, city_address, and zip_code as three separate statements.. Oracle9i makes it possible to create an abstract data type called full_address and manipulate it as if it were a single data type. This is a huge improvement for Oracle, but as already mentioned, pre-relational databases supported this construct. A data type composed of subtypes has always been available to COBOL users. For example, a full address can be defined in COBOL as follows:
05 CUSTOMER-ADDRESS. 07 STREET-ADDRESS 07 CITY-ADDRESS 07 ZIP-CODE PIC X(80). PIC X(80). PIC X(5).

The CUSTOMER-ADDRESS can then be treated as if it were a single entity, like this:
MOVE CUSTOMER-ADDRESS TO PRINT-REC. MOVE SPACES TO CUSTOMER-ADDRESS.

A customer_address data type can be defined in an object database as follows:


CREATE TYPE customer_address ( street_address CHAR(20),

Object and User-Defined Data Types


Object and User-Defined Data Types You are already familiar with the concept of complex data types (date and time), but their complexity is hidden from the users. Now, we are going to talk about some complex data types that do require more user involvement. Understanding these data types requires some background in object-oriented programming and design. SQL99 In general, SQL99 defines two new groups of complex data types: Abstract types (ADT) and collections.

ADT
ADT provides means to explicitly define a structural type within the larger context of the database structure. It is a series of attribute names paired with system data types plus optional methods to manipulate those properties. ADT can virtually be anything one can imagine and describe by the means of the language a car, a person, an invoice. Note We already mentioned in Chapter 1 that object-oriented design and principles are very popular today. All modern computer languages (Java, C#, VB.NET) are strictly based on those principles. Nevertheless, we would like to remind you that object-oriented databases are hardly mainstream; their market share is marginal at best, and their future existence is still questionable. Major database vendors seem to prefer embedding some object-oriented features into their existing RDBMS engines (OORDBMS approach). We are not going to list all pros and contrast once again, but in our opinion the major advantage of traditional relational databases is they are based on strong mathematical and theoretical platforms and time-proven standards.

Collections
Collections can be of reference type, set type, and list type. Reference type Reference type is a special type through which an instance of another type can be referenced. The reference values are read-only and unique in database system catalog. Set and list types

These types extend a field's capabilities by storing a collection of homogeneous data. For instance, we could make a good use of array data type for a phone number field. Note Until recently, phone numbers didn't cause database programmers and designers any troubles. It was conventional for a person to have only one home number and a work number. The situation has dramatically changed in recent years. Cell phones are now as popular as TVs, VCRs, and watches. Many people even have more than one cell phone! That is not such a big deal for a correctly designed relational database we simply can store phone numbers in a separate table with pointers to their parent. But using an array data type would be another elegant solution in this situation. Sets and lists can consist of built-in data types (for example, array of strings), abstract data types, reference data types, or named row data types. The only difference between the two collection types is that the list has to be ordered. Oracle 9i Oracle supports ADT in the form of object types. Collections are supported as nested tables, varying arrays, and ref types. Objects Objects in Oracle can either simply describe an entity or have some methods (functions) to manipulate it in addition to that. We can create an object in Oracle using this syntax: CREATE TYPE addr_type AS OBJECT ( street VARCHAR(30), city VARCHAR(30), state CHAR(2), zip VARCHAR(10), country VARCHAR(30) type CHAR(1)); We can now create table CUST1 with address field of type ADDR_TYPE: CREATE TABLE cust1 ( id NUMBER, name VARCHAR(30), address addr_type); Figure 3-5 is the illustration of what we've just created, assuming some records have been populated. Now we can access the attributes of address field using dot notation:

declared as an object type

Figure 3-5: Table with column

address.street address.city address.state address.zip address.country address.type Not much benefit so far basically we just store multiple address fields in one column but we'll take advantage of our new custom type in later examples. Nested tables A nested table in Oracle corresponds to SQL99 set type. It can be defined as a table that is a column of another table with unlimited number of rows. The actual data for a nested table is physically stored in another table, but from a user's viewpoint it looks like an integral part of the main table. For example, we have a parent-child relationship between tables CUST2 and ADDRESS and want to eliminate it using a nested column instead. Assuming we already have ADDR_TYPE created, what we do next is CREATE TYPE addr_type_tab AS TABLE OF addr_type This creates a new user-defined type ADDR_TYPE_TAB of nested table type. Now, we can create the actual table CUST2 so that each row may contain a table of addresses: CREATE TABLE cust2 ( id NUMBER, name VARCHAR(30), address addr_type_tab) NESTED TABLE address STORE AS addr_storage; The last line of code specifies the name of the storage table where the rows of all the values of the nested table reside. Figure 3-6 shows how addresses for CUST2 are stored in a nested table.

Figure 3-6: Nested tables in Oracle Varying arrays VARRAY data type is very similar to NESTED TABLE. The main difference is that VARRAY has to be ordered. Also, you have to specify the maximum number of elements for VARRAY and don't have to indicate the name for the storage: CREATE TYPE addr_type_varray AS VARRAY(50) OF addr_type;

create table cust3 ( id NUMBER, name VARCHAR(30), address addr_type_varray); DB2 UDB 8.1 DB2 supports user-defined structured types, reference types, and user-defined distinct types.

User-defined types
User-defined types are very similar to Oracle's object types. This example shows how to create an address structure: CREATE TYPE addr_type AS ( street VARCHAR(30), city VARCHAR(30), state CHAR(2), zip VARCHAR(10), country VARCHAR(30) type CHAR(1)) MODE DB2SQL Now we can create a table that uses the new user-defined data type ADDR_TYPE: CREATE TABLE cust1 ( id

INTEGER, name VARCHAR(30), address addr_type)

Reference types
Reference types are used to define references to a row in another table (or userstructured type). The references are similar to referential integrity constraints but do not enforce relationships between the tables: CREATE TABLE cust2 ( id REF(addr_type)) INTEGER, name VARCHAR(30), address

The address column of the CUST2 table references the user-defined ADDR_TYPE.

Distinct types
Distinct types are defined using existing data types and have the same features of the built-in type. For example, we can create a new data type usd for U.S. dollars: CREATE DISTINCT TYPE usd AS DECIMAL(12,2) WITH COMPARISONS Now we can use it throughout the whole database in a way similar to the way MS SQL Server uses its MONEY data type. For example, we can create tables with column of a type USD: CREATE TABLE employee ( id INTEGER, same VARCHAR(30), salary USD);

You might also like