record and table

1.1 PL/SQL record

    Records are composed of a group of fields, similar to the columns in a row. The %ROWTYPE attribute lets you declare a PL/SQL record that represents a row in a database table, without listing all the columns. Your code keeps working even after columns are added to the table. If you want to represent a subset of columns in a table, or columns from different tables, you can define a view or declare a cursor to select the right columns and do any necessary joins, and then apply %ROWTYPE to the view or cursor.

  记录类型的定义语法:

  TYPE record_name IS RECORD(

     field_name1 datatype1 [NOT NULL][DEFAULT | := expression1],

     field_namen datatype2 [NOT NULL][DEFAULT | := expressionn]

   );

  记录声明

  v_record1 record_name;

  v_record2 record_name;

  记录的引用

  record_name.field_name

  记录赋值
  v_record1:=v_record2;     --赋值的两个record必须是相同类型的记录

  使用%ROWTYPE

  v_recordnamen table_name%ROWTYPE;

  将定义一个记录v_recordnamen,记录中的各字段类型,将与表table_name的各列类型相对应。

  而%TYPE只会返回某一列的类型,使用%TYPE时,在列上定义的所有NOT NULL约束都不会被包含进来。

 

 

1.2 PL/SQL table

      PL/SQL提供一种复合类型 TABLE(即PL/SQL表)。PL/SQL表有两列KEY列和VALUE列。PL/SQL tables have only one column and use a primary key to give you array-like access to rows.  这个列可以是标量类型(scalar type)如CHAR、DATE、NUMBER。但是primary key必须是BINARY_INTEGER、PLS_INTEGER或者VARCHAR2中的一种。

      可以在块、过程、函数或者包的声明部分声明 PL/SQL table type。

  ...

  DECLARE

     TYPE tabletype_name IS TABLE OF type

       INDEX BY BINARY_INTEGER;

  ...

  BEGIN

     ...

  END;

 

  Once you declare type tabletype_name ,you can declare PL/SQL tables of that type, as the next example shows:

 

   v_tabletype tabletype_name;

 

   声明了类型和变量以后,就可以引用了。you can reference rows in PL/SQL table using array-like syntax to specify the primary key value. For example, you reference index(第index行) row in the PL/SQL table named v_tabletype as follows:

 

  v_tabletype(index);

 

 

1.2.1 PL/SQL版本2.3允许PL/SQL存储记录的表。

    DECLARE

      TYPE t_StudentTable IS TABLE OF students%ROWTYPE

         INDEX BY BINARY_INTEGER

      v_Students t_StudentTable;

    BEGIN

      SELECT *

   INTO v_Students(10001)

         FROM students

         WHERE id=10001;

    END;

 

   you can reference rows in the PL/SQL table as follows:

   table(index).field

   如:

       v_Students(10001).first_name:='Larry';

 

 

1.3PL/SQL table  的 attribute

        属性                                    返回类型                                     说明

     COUNT                                  NUMBER                                返回表中行的数目

     DELETE                                 N/A                                        删除表中的行,如果没有参数会全部删除

     EXESTS                                 BOOLEAN                              如果指定的表项存在,返回TRUE

     FIRST                                    BINARY_INTEGER                  返回表中第一行的索引

     LAST                                     BINARY_INTEGER                  返回表中最后一行的索引

     NEXT                                     BINARY_INTEGER                 返回表中指定行的下一行的索引

     PRIOR                                   BINARY_INTEGER                 返回表中指定行的上一行的索引

 

    一个表在第一次被创建的时候,它是不包含任何行的,这样,在不使用DELETE的情况下,如果要删除整个PL/SQL表,可以将一个空表赋值给它。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值