010 Nested Tables

Embed Size (px)

Citation preview

  • 7/31/2019 010 Nested Tables

    1/12

    Oracle 9i

    Nested Tables

  • 7/31/2019 010 Nested Tables

    2/12

    Creating an Object

    Create an object which will store the data that is requiredas part of nested table.

    Create or Replace Type MultiAddress

    asobject

    (

    PermanentAddress Varchar2(100),

    CurrentAddress Varchar2(100)

    );

  • 7/31/2019 010 Nested Tables

    3/12

    Creating Table of Object

    Create a Table from the object created for the nestedtable to be used as data type specification for the nestedtable in the container tables definition.

    Create Type StudentsMultiAddressas

    Table of MultiAddress;

  • 7/31/2019 010 Nested Tables

    4/12

    Create Nested Table

    A nested table is stored in the container table as acolumn definition.

    The actual data for the nested table is stored separatefrom the container table.

    We cannot directly query on the nested table.

    Create Table Students(

    RollNo char(4) Primary Key Check (RollNo like 'S%'),Name varchar2(100) Not Null,

    Address StudentsMultiAddress) Nested Table Address Store As StudentsAddress;

  • 7/31/2019 010 Nested Tables

    5/12

    Viewing the structure of Table

    To view the complete structural details of the containertable the user must issue the following commands.

    Set Describe Depth All;

    Describe Students;

  • 7/31/2019 010 Nested Tables

    6/12

    Inserting Data Data can be inserted in container table as:

    Insert into Students Values('&RollNo',

    '&Name',StudentsMultiAddress(

    MultiAddress('PAddress 1 Tuple 1', 'CAddress 1 Tuple1'),MultiAddress(PAddress 2 Tuple 1', 'CAddress 2 Tuple1'),MultiAddress(PAddress 3 Tuple 1', 'CAddress 3 Tuple1'),MultiAddress(PAddress 4 Tuple 1', 'CAddress 4 Tuple1'),

    MultiAddress(PAddress 5 Tuple 1', 'CAddress 5 Tuple 1'))

  • 7/31/2019 010 Nested Tables

    7/12

    Viewing Data

    The data stored in the table containing nested table canbe viewed by the simple select statement.

    Select * from Students;

    The following query is not allowed.

    Select * from StudentsAddress;

  • 7/31/2019 010 Nested Tables

    8/12

    Querying Data of Nested Table

    The data of the nested table is queried by using thetable function of oracle and an alias name can be usedfor the nested table.

    Select RollNo,SA.PermanentAddress,SA.CurrentAddressfrom students, Table (Students.Address) SA

    where SA.CurrentAddress = 'CAddress 5 Tuple 1';

  • 7/31/2019 010 Nested Tables

    9/12

    Inserting Data in Nested Table Data can be added to the nested table column of the

    container table by using the table function of oracle likethe following command.

    Insert into Table

    (

    Select Address From Students where RollNo='S001

    )

    Values

    (MultiAddress (PAddress 6 Tuple 1','CAddress 6 Tuple 1

    )

    );

  • 7/31/2019 010 Nested Tables

    10/12

    Updating Nested Table Contents

    The data of the Nested Table can be updated by usingthe table function of oracle alias name can be used torefer the nested table.

    Update Table(

    Select Address From Students

    Where RollNo=S001

    ) SA

    Set SA.CurrentAddress = 'UCAddress 6 Tuple 1'

    Where SA.CurrentAddress = 'CAddress 6 Tuple 1';

  • 7/31/2019 010 Nested Tables

    11/12

    Deleting Data from Nested Table

    The data of the Nested Table can be updated by usingthe table function of oracle alias name can be used torefer the nested table.

    Delete Table(

    Select Address From Students

    Where RollNo='S001

    ) SA

    Where SA.CurrentAddress = UCAddress 6 Tuple 1';

  • 7/31/2019 010 Nested Tables

    12/12