user1744285
user1744285

Reputation: 11

Creating (NonFirst Normal Form) object table using rows of another ordinary (2NF) table

The challenge:

From one table named CARS_TABLE with two columns, ID and Model with information like below: 
1    Ferrari
2    Audi
3    Mustang
After creating another table (in this case, named CARS_OBJECTS_TABLE) that only has 
only one column which is an object type which I\'ve done with the code below...
/
CREATE TYPE CAR_OBJECT AS OBJECT
    (ID number,
    Model varchar2(30))
/
CREATE TABLE Cars_Object_Table(
    Car_Column CAR_OBJECT)

-- now the challenge is to get the two columns, ID and Model, 
of the first table CARS_TABLE,  into the object column Car_column 
of the second table CARS_OBJECT_TABLE. 

So far, I have come up with the below, which does not produce an error, 
but does not populate the object column of the second table either.

CREATE TYPE CAR2 UNDER CAR (
    IDs number,
    Models varchar2(30),
DECLARE
    Car_Object CAR2;
    cursor importData is select * from cars_table;
BEGIN
    open importData;
    loop
    fetch importData into CAR2 (IDs, Models)
    if importData%found
        then
        insert into Cars_Object_Table    
        values CAR2 (IDs, Models);
    else
        exit;
    end if;
    end loop;
    close importData;
END;
/

Any ideas? 


Upvotes: 0

Views: 66

Answers (1)

WoMo
WoMo

Reputation: 7246

You can do this in just SQL with an insert .. select statement. However you must first select your cars_table data into car_objects and then insert.

insert into cars_object_table
select car_object(id, model) from cars_table;

Upvotes: 1

Related Questions