How to pass values to object columns from one table to another? - oracle

I have my main table like this
create table final(
Province varchar2(50),
Country varchar2(100),
Latitude Number(10,0),
Longitude Number(10,0),
Cdate varchar2(20),
Confirmed int,
killed int,
Recover int
)
Then I have created Table with nested table like this
create type virus_Statistic_t as object(
vDate varchar2(20),
infection int,
dead int,
recovered int
)
/
create type virus_Statistic_tlb as table of virus_Statistic_t
/
create type countries_t as object(
Province_or_State varchar2(50),
Country_or_Region varchar2(100),
Lat Number(10,0),
Longt Number(10,0),
virus virus_Statistic_tlb
)
/
create table countries of countries_t (
Lat not null,
Longt not null
) nested table virus store as virus_ntb;
Now I am trying to pass all columns values from final to countries table.
This is I have tried
INSERT INTO countries(Province_or_State, Country_or_Region, Lat, Longt, vDate, infection, dead, recovered)
SELECT Province, Country, Latitude, Longitude, Cdate, Confirmed, killed, Recover
FROM final
/
It gives this error
ERROR at line 1:
ORA-00904: "RECOVERED": invalid identifier
How can I pass all values from final to countries table?

You need to use type constructors. The correct syntax is this:
INSERT INTO countries(Province_or_State, Country_or_Region, Lat, Longt, virus)
SELECT Province, Country, Latitude, Longitude,
virus_Statistic_tlb (virus_Statistic_t(Cdate, Confirmed, killed, Recover))
FROM final
/
Though note that this is only inserting one virus row per country, is that what you meant? To insert multiple virus rows per country do this:
INSERT INTO countries(Province_or_State, Country_or_Region, Lat, Longt, virus)
SELECT Province, Country, Latitude, Longitude,
CAST(MULTISET(SELECT virus_Statistic_t(Cdate, Confirmed, killed, Recover)
FROM final f2
WHERE f2.Province = f1.Province
AND ...etc.
) AS virus_Statistic_tlb
)
FROM final f1
GROUP BY Province, Country, Latitude, Longitude;
Opinion
I can never respond to a question about using nested tables without saying that the correct way to use them is not at all! In a real database you should have a separate database table for virus_statistics with a foreign key to the countries table. I realise you are probably doing this for educational purposes, but you should also be aware that no one should ever use nested tables in real life. No doubt you'll soon realise why, when you try to use this data :-)

Related

Oracle Object Type and Object Table

I have created an object type and a table. I would like to know how select, insert, update and delete operation on it.
create table employee_info (
empid number,
emp_name varchar2(50),
department varchar2(20),
designation varchar2(50),
salary number
);
create type employee_info_obj is object (
empid number,
department varchar2(50),
designation varchar2(50),
salary number
);
create type employee_info_obj_t is
table of employee_info_obj ;
You have only created an object type and an unrelated database table. If you want a database table based on the type, you need to create one:
create table employee_info of employee_info_obj;
While it can be nice in certain programming scenarios to have a type synced to a table, there are some downsides such as it being harder to add columns later, and third party tool support since the object table will not be listed in user_tables but only in user_object_tables and user_all_tables, so I would question the usefulness of this approach.
dbFiddle

oracle sql developer after creating table , column name came as column1,column2 .... columnN

i am using sql developer Version 21.2.1.204 and when i create table by using new table function in IDE , and entered name and select type for each field
after clicking on ok button table is created but fields name are different,
COLUMN2,
COLUMN3,
COLUMN4,
came
i entered column name properly but still this is happening
you can see in images
If the wizard is not working for you, you can write out the DDL statement in a worksheet:
CREATE TABLE table1 (
id VARCHAR2(20),
name VARCHAR2(20),
address VARCHAR2(20),
age VARCHAR2(20) -- Why not use a number?
);
And run it.
As an aside, you probably should use date_of_birth and then calculate the age rather than using an age column that will go out of date as soon as the next person reaches their birthday.
CREATE TABLE table1 (
id VARCHAR2(20),
name VARCHAR2(20),
address VARCHAR2(20),
date_of_birth DATE
);

clickhouse MATERIALIZED VIEW issues

I created MATERIALIZED VIEW like this :
create target table:
CREATE TABLE user_deatils_daily (
day date,
hour UInt8 ,
appid UInt32,
isp String,
city String,
country String,
session_count UInt64,
avg_score AggregateFunction(avg, Float32),
min_revenue AggregateFunction(min, Float32),
max_load_time AggregateFunction(max, Int32)
)
ENGINE = SummingMergeTree()
PARTITION BY toRelativeWeekNum(day)
ORDER BY (day,hour)
create mv:
CREATE MATERIALIZED VIEW user_deatils_daily_mv
TO user_deatils_daily as
select toDate(session_ts) as day, toHour(toDateTime(session_ts)) as hour,appid,isp,city,country,
count(session_uuid) as session_count,avgState() as avg_score,
minState(revenue) as min_revenue,
maxState(perf_page_load_time) as max_load_time
from user_deatils where toDate(session_ts)>='2020-08-26' group by session_ts,appid,isp,city,country
the data in the target table starting to fill with data.
after some times the target table is getting fill with new data and doesn't' save the old one.
why is that?
SummingMergeTree() PARTITION BY toRelativeWeekNum(day) ORDER BY (day,hour)
means calculate sums groupby toRelativeWeekNum(day), day,hour)
user_deatils_daily knows nothing about user_deatils_daily_mv. They are not related.
user_deatils_daily_mv just does inserts into user_deatils_daily
SummingMergeTree knows nothing about group by session_ts,appid,isp,city,country
I would expect to see ORDER BY (ts,appid,isp,city,country);
I would do:
CREATE TABLE user_details_daily
( ts DateTime,
appid UInt32,
isp String,
city String,
country String,
session_count SimpleAggregateFunction(sum,UInt64),
avg_score AggregateFunction(avg, Float32),
min_revenue SimpleAggregateFunction(min, Float32),
max_load_time SimpleAggregateFunction(max, Int32) )
ENGINE = AggregatingMergeTree()
PARTITION BY toStartOfWeek(ts)
ORDER BY (ts,appid,isp,city,country);
CREATE MATERIALIZED VIEW user_deatils_daily_mv TO user_details_daily
as select
toStartOfHour(toDateTime(session_ts)) ts,
appid,
isp,
city,
country,
count(session_uuid) as session_count ,
avgState() as avg_score,
min(revenue) as min_revenue,
max(perf_page_load_time) as max_load_time
from user_details
where toDate(session_ts)>='2020-08-26' group by ts,appid,isp,city,country;

Oracle Insert all data from another table contain duplicates how to avoid

have two tables A and B both same structure except B has one addition extra column inserting as "null". I need to Retain all data from A in B when I insert like below query it is inserting duplicate values because of that getting "primary Key violation error" when I try to create the "CONSTRAINT PK_Details_A PRIMARY KEY" Please help on this to avoid duplicate values while inserting the records.
Thanks in advance.
Insert into tableB(
id, effectiveDate, endDate
,startDate, Type, salary
,baseSalary, Amount, Amount1
,currency, Percentage, Salary
,Notional
)
select id, effectiveDate, endDate
,startDate, Type, salary
,baseSalary, Amount, Amount1
,currency, Percentage, Salary,null
from tableA;
EDIT
Primary key definition for B copied from comment below:
ALTER TABLE B
ADD CONSTRAINT PK_B
PRIMARY KEY ( oid)
USING INDEX ( CREATE UNIQUE INDEX PK_B ON B ( oid )

what is the difference between nested table and object type in oracle?

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.

Resources