create table mbastudent
(
semester varchar2(20) ,
stud_info student
);
this is object table i have created
now iserting value--
insert into mbastudent
(semester,stud_info )
values
('1st' ,student(1100 ,'KUMAR', '07-OCT-80', '04-MAR-14', 15000))
insert into mbastudent
(semester,stud_info )
values ('2nd',student(1101,'SESHU', '07-OCT-81', '04-MAR-14', 15000));
select ref(a) from mbastudent a;
--how to get ref value of object
You may find it in
Oracle docs from site
I dont now structure of student. My sugesstion is
student (
id number,
name varchar2(4000),
birthday date,
begin_date date,
cost number
)
And you want to get a name, cost and semester
SELECT semester, stud_info.name, stud_info.cost
FROM mbastudent s
Related
I need to insert a row to a table in oracle.
insert into policy_tab values ('4325','29-APR-98','29-APR-2007',32424,(select ref(a) from agent_tab a where a.nic='242424v'),claim_ntty(
claim_t('25-APR-2005','25-JUN-2005'),
claim_t('26-APR-2005','26-JUN-2005')
));
But when I executed it will show this error. "ORA-01722: invalid number ORA-06512: at "SYS.DBMS_SQL", line 1721"
create type policy_ty as object(
pid char(5),
sDate date,
eDate date,
premium number(10,2),
agent ref agent_ty,
claims claim_ntty
);
create table policy_tab of policy_ty(
pid primary key,
agent SCOPE IS agent_tab
)
nested table claims store as claim_nttab;
create type claim_t AS OBJECT(
eDate date,
amount number(10,2)
);
create type claim_ntty as table of claim_t;
create type agent_ty as object(
nic char(10),
name varchar(50),
address varchar(50),
contactNo contactNo_vaty
) NOT FINAL;
create table agent_tab of agent_ty(
nic primary key
);
So how to resolve it?
From the first look you need to use like below. However the information provided is not sufficient to resolve the issue. Related Objects defintion as well needed.
This has to be changed - '29-APR-98' to '29-APR-1998'
INSERT INTO policy_tab
VALUES (
'4325',
'29-APR-1998',
'29-APR-2007',
32424,
(SELECT REF (a)
FROM agent_tab a
WHERE a.nic = '242424v'),
claim_ntty (claim_t ('25-APR-2005', '25-JUN-2005'),
claim_t ('26-APR-2005', '26-JUN-2005')));
Edit:
Second observation. You created the below object:
create type claim_t AS OBJECT(
eDate date,
amount number(10,2)
);
And using it like:
claim_ntty (claim_t ('25-APR-2005', '25-JUN-2005'),
claim_t ('26-APR-2005', '26-JUN-2005')));
Second Argument should be number not date.
So your insert should be:
INSERT INTO policy_tab
VALUES (
'4325',
'29-APR-1998',
'29-APR-2007',
32424,
(SELECT REF (a)
FROM agent_tab a
WHERE a.nic = '242424v'),
claim_ntty (claim_t ('25-APR-2005', 123), claim_t ('26-APR-2005', 456)));
Below, I have defined some tables, for the relevant ones I have typed in the definitions. Pretty simple, though now I'm trying to raise the salaries for those two employees in the view, and I can't complete the update having that error message you will see below. Anyone could guide me a bit, please??
Definitions for tables employees, projects and employees_projects:
create table employee
(
id number,
name varchar2(20),
mobile varchar2(10),
address varchar2(30),
salary number(6,2),
hire_date date,
department_id number
);
create table project
(
id number,
name varchar2(20),
budget number(10,2),
start_date date,
finish_date date
);
create table employee_projects
(
id number,
employee_id number,
project_id number
);
View definition is:
create view lucky_employees as
select e.name,e.salary from employees e, project p, employee_projects ep
where e.project_id=p.id and e.id=ep.employee_id and p.budget > 1000000.00 ;
SQL> select * from lucky_employees;
ID NAME SALARY
1 Maria 1365.28
2 Sonja 1365.28
Then, I try to update the view by 10%, which is something I know it's possible to do under certain conditions:
SQL>
update lucky_employees set salary = salary * 1.1;
update lucky_employees set salary = salary * 1.1
*
ERROR at line 1:
ORA-01779: cannot modify a column which maps to a non key-preserved table
How would it be to succesfully update it??
Thanks very much, sorry for the inconveniences!!
create view lucky_employees as
select e.name from employees e, project p, employee_projects ep
where e.project_id=p.id and e.id=ep.employee_id and p.budget > 1000000.00 ;
This error is occurring because each salary of your view doesnot uniquely map to a salary of your employees table.
More information from oracle docs is here.
Anoter explanation from Burleson here.
I have following object tables in oracle DB.
create type deposit_ty as object
(
depno number(6),
depcategory ref depcategory_ty,
amount number(6),
period number(2)
);
create type deposit_ntty as table of deposit_ty;
create type address_ty as object
(
homeno number,
street varchar(30),
city varchar(30)
);
create type customer_ty as object
(
cusid char(4),
custname varchar(40),
address address_ty,
dob DATE,
deposits deposit_ntty
);
can any one tell what is the difference between column address and deposits in customer_ty object table?
An object type/abstract data type/record is like a row or tuple: it contains an ordered set of attributes. To populate address you must set one and only one value for each of homeno, street, and city.
A nested table is like a table: it contains an unordered set of rows. Usually a nested table only contains a set of simple values, like a number. In your example, it is a set of object types. To populate deposits you can create any number of deposit_ty.
For example:
declare
customer customer_ty :=
customer_ty(
'ABC',
'Name',
address_ty('123', 'fake street', 'Springfield'),
sysdate,
deposit_ntty(
deposit_ty(1, null, 100, 1),
deposit_ty(2, null, 200, 2)
)
);
begin
null;
end;
Also, you probably want to use a VARCHAR2 instead of VARCHAR or CHAR. And if it's not too late, throw out all this object stuff and use tables like everyone else.
I have a series of tables and objects I have defined. I have an object nested table that I am trying to insert values into. The values are in the form of a variable array but I don't know how to insert them. my tables and code are as follows.
Table wu.classes
crn number(5)
department varchar2(8)
title carchar2(25)
Table wu.students
student_id char(11)
name varchar2(10)
dept varchar2(8)
advisor varchar(10)
classes wu.classes_va
wu.classes_va varray(5) of number (5)
create type classes_ty as object(crn varchar2(5),department varchar2(8), coursetitle varchar2(25)
create table classes_ot of classes_ty;
insert into classes_ot select crn,department,title from wu.classes;
create or replace type classes_ref_ty as table of ref classes_ty;
create table student_plus(student# varchar2(11),student_name varchar2(10),major varchar2(8), advisor (10), enrolled classes_ref_ty) nested table enrolled store as classes_ref_ty_tab;
Problem here (I need to loop through to fill the table but I just need to know how to do it for one values and i can figure the rest out):
begin
insert into student_plus values('700-123-948','Hooker','CS','VanScoy',classes_ref_ty();
insert into table(select enrolled from student_plus where student#='700-123-948')
select ref(c) from classes_ot c where ???
end;
/
I don't know how to access the variable array and use it with the classes_ref_ty.
I've created the following two object types :
create or replace type person_typ as object (
person# varchar(10)
) not final;
create or replace type salesperson_typ under person_typ (
salesperson# varchar(10),
sSurname varchar(10),
sForename varchar(10),
dateOfBirth date
);
create table person_tab of person_typ (
person# primary key
);
And I've inserted a row using :
insert into person_tab
values (salesperson_typ('p1','s1', 'Jones', 'John', sysdate));
Which I can retrieve using the following :
select
treat(value(s) as salesperson_typ).person# as person_number,
treat(value(s) as salesperson_typ).sSurname as sSurname
from
person_tab s
;
However, if I look at person_tab I only see the following :
SQL> select * from person_tab;
PERSON#
----------
p1
I'm curious, where does the salesperson specific data get stored? I was almost expecting to find a salesperson table, but I can't find anything obvious.
Your object is stored invisibly in the same table.
You can check columns by querying USER_TAB_COLS:
SELECT *
FROM user_tab_cols
WHERE table_name = 'PERSON_TAB';
Then you can then use the column names* you just discovered in a query (except SYS_NC_ROWINFO$, that throws an error for me).
SELECT SYS_NC_OID$
,SYS_NC_TYPEID$
--,SYS_NC_ROWINFO$
,PERSON#
,SYS_NC00005$
,SYS_NC00006$
,SYS_NC00007$
,SYS_NC00008$
FROM PERSON_TAB;
Note*
You should not use these column names in any application because they are internal and subject to change in future patches/releases.