Trouble adding metadata to a table in Oracle - oracle

I created a table in Oracle with this code:
CREATE TABLE Roads (
TYPE VARCHAR2(40),
ADMN_CLASS VARCHAR2(20),
TOLL_RD VARCHAR2(10),
RTE_NUM1 VARCHAR2(3),
RTE_NUM2 VARCHAR2(3),
ROUTE VARCHAR2(40),
LOCATION MDSYS.SDO_GEOMETRY);
Then I wanted to add metadata to the LOCATION column which will hold the geometry:
INSERT INTO USER_SDO_GEOM_METADATA
(TABLE_NAME, COLUMN_NAME, DIMINFO, SRID)
VALUES
('ROADS', 'LOCATION',
MDSYS.SDO_DIM_ARRAY
( MDSYS.SDO_DIM_ELEMENT('X', -180, 180, 0.5),
MDSYS.SDO_DIM_ELEMENT('Y', -90, 90, 0.5)
),
8256
);
However, I get an error:
Error report -
SQL Error: ORA-13223: duplicate entry for ROADS.LOCATION in SDO_GEOM_METADATA
ORA-06512: at "MDSYS.MD", line 1723
ORA-06512: at "MDSYS.MDERR", line 17
ORA-06512: at "MDSYS.SDO_GEOM_TRIG_INS1", line 48
ORA-04088: error during execution of trigger 'MDSYS.SDO_GEOM_TRIG_INS1'
13223. 00000 - "duplicate entry for %s in SDO_GEOM_METADATA"
*Cause: There are duplicate entries for the given table and column
value pair in the USER_SDO_GEOM_METADATA view.
*Action: Check that the specified table and geometry column names
are correct. There should be only one entry per table, geometry
column pair in the USER_SDO_GEOM_METADATA view.
I am not sure why it says there are duplicate values because I have not created this table before. It also tells me to make sure that table and column name is correct and they are.

I've created your table and executed your insert statement OK.
Once executed again, I was able to reproduce your problem.
The error "SQL Error: ORA-13223: duplicate entry for ROADS.LOCATION in SDO_GEOM_METADATA" definitely points to duplicate data.
This trigger MDSYS.SDO_GEOM_TRIG_INS1 just performs a count(*) validation on the following MDSYS table and raise the -13223 exception when rowcount is != 0 .
SQL> desc mdsys.SDO_GEOM_METADATA_TABLE
Name Null? Type
----------------------------------------- -------- ----------------------------
SDO_OWNER NOT NULL VARCHAR2(32)
SDO_TABLE_NAME NOT NULL VARCHAR2(32)
SDO_COLUMN_NAME NOT NULL VARCHAR2(1024)
SDO_DIMINFO MDSYS.SDO_DIM_ARRAY
SDO_SRID NUMBER
Query mdsys.SDO_GEOM_METADATA_TABLE in order to validate its contents. You should find the duplicate record:
SQL> select * from mdsys.SDO_GEOM_METADATA_TABLE ;
USER1
ROADS
LOCATION
SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -180, 180, .5), SDO_DIM_ELEMENT('Y', -90, 90,
.5))
8256
To workaround this issue just delete that record from mdsys.SDO_GEOM_METADATA_TABLE.

I think the problem is because some space characters on the column_name.
COR_NB_SC_LON,COR_CD_EW_ LON
I have fixed it and it works.
insert into user_sdo_geom_metadata (table_name, column_name, diminfo, srid)
values (
'COORD_REF',
'myuser.CONVERT_DMS_TO_GEOM(COR_NB_DG_LON,COR_NB_MN_LON,COR_NB_SC_LON,COR_ CD_EW_LON,COR_NB_DG_LAT,COR_NB_MN_LAT,COR_NB_SC_LAT,COR_CD_NS_LAT)',
sdo_dim_array (
sdo_dim_element('long', -180.0, 180.0, 0.5),
sdo_dim_element('lat', -90.0, 90.0, 0.5)
),
4326
);
commit;

Related

ORA-04088: error during execution of trigger

I am facing following problem. I created a table with following sql in Oracle 11g release 2:
create table loc_aud
(
username varchar2(20),
audittime date,
IP VARCHAR2(30),
locno number(4),
old_city number(4),
new_city number(4)
);
This table is in sys schema. Then I created a trigger for value base auditing using following command in sys schema
CREATE OR REPLACE TRIGGER location_audit
AFTER UPDATE OF city
ON hr.locations
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF :old.city != :new.city THEN
INSERT INTO loc_aud
VALUES (user, sysdate, UTL_INADDR.get_host_address,
:new.location_id,:old.city,:new.city);
END IF;
END;
After that I connected with hr schema and tried to update the city column with following command:
update locations set city = 'Dhaka' where location_id = 2100;
But it is giving me following errors
update locations set city = 'Dubai' where location_id = 2100
*
ERROR at line 1:
ORA-01722: invalid number
ORA-06512: at "SYS.LOCATION_AUDIT", line 3
ORA-04088: error during execution of trigger 'SYS.LOCATION_AUDIT'
What am I doing wrong?
The table I created named loc_aud had a wrong datatype. The column city was varchar2 and what I tried to save in it was a number datatype. I altered the table and it worked.

can not update user_sdo_geom_metadata when creating spatial database in oracle

i was using the oracle 11g and try to create a spatial database, i just copied the sample code from oracle document
but when it comes to update the metadata part, it gave a duplicate entry error, and i tried delete from user_sdo_geom_metadata and it didnot give any error, and then tried to insert again, still got the duplicate entry error. I also tried select * from user_sdo_geom_metadata but got nothing.
any one have idea why? thanks
THE sample code:
CREATE TABLE cola_markets (
mkt_id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape SDO_GEOMETRY);
INSERT INTO cola_markets VALUES(
1,
'cola_a',
SDO_GEOMETRY(
2003, -- two-dimensional polygon
NULL,
NULL,
SDO_ELEM_INFO_ARRAY(1,1003,3), -- one rectangle (1003 = exterior)
SDO_ORDINATE_ARRAY(1,1, 5,7) -- only 2 points needed to
-- define rectangle (lower left and upper right) with
-- Cartesian-coordinate data
));
INSERT INTO user_sdo_geom_metadata
(TABLE_NAME,
COLUMN_NAME,
DIMINFO,
SRID)
VALUES (
'cola_markets',
'shape',
SDO_DIM_ARRAY( -- 20X20 grid
SDO_DIM_ELEMENT('X', 0, 20, 0.005),
SDO_DIM_ELEMENT('Y', 0, 20, 0.005)
),
NULL -- SRID
);
error cause: there are duplicate entries for the give table and column value pair in the user_sdo_geom_metadata view.
Just to be sure, what does the following show ?
SELECT * FROM USER_SDO_GEOM_METADATA;
Object names in dictionary views are stored in uppercase. So if you want to remove existing entries from the spatial metadata you need to do this (notice the uppper case table name):
DELETE FROM USER_SDO_GEOM_METADATA WHERE TABLE_NAME = 'COLA_MARKETS';
COMMIT;

Log all errors for a value into error log if a single row fails

I am trying to maintain data integrity and logging errors to an error table. I've got 3 tables with unique constraints and 1 error table:
create table tbl_one (
pass_no number,
constraint tbl_one_u01 unique (pass_no) );
insert into tbl_one values(10);
insert into tbl_one values(20);
create table tbl_two (
cus_no number,
cus_name varchar2(50),
pass_no number,
constraint tbl_two_u01 unique (cus_no) );
insert into tbl_two values( 101, 'NameX',10);
insert into tbl_two values( 102, 'NameY',10);
insert into tbl_two values( 103, 'NameZ',20);
create table tbl_target (
cus_no number,
pass_no number,
constraint tbl_target_u01 unique (cus_no),
constraint tbl_target_u02 unique (pass_no));
exec dbms_errlog.create_error_log('tbl_target','tbl_target_err');
I am trying to log all ORA-00001 errors to the error table tbl_target_err like this:
begin
insert into tbl_target
select a.pass_no, b.cus_no
from tbl_one a
inner join tbl_two b on b.pass_no = a.pass_no
log errors into tbl_target_err reject limit 10;
end;
The result is:
select * from tbl_target;
-------------------
CUS_NO PASS_NO
101 10
103 20
and the error table:
CUS_NO PASS_NO
102 10
I need all the violated errors to go into the error table. If the value of pass_no 10 is violated then all 10 values should inserted into the error table; not, one into target and one to error table. I don't want to use exists statements because I won't able to log all violated values.
How could I go about doing this?
You can't use the error logging mechanism for this as it isn't designed to support it. It errors at the point it tries to create the duplicate in the table - the first value it tries to insert for pass_no 10 is valid on its own - so it would have to distinguish between data that already existed and multiple values coming from the insert, to start with. So you'd need to roll your own.
One option is to create your own table to hold the duplicates, and use an insert all to decide which values belong in each table:
create table tbl_target_dup (
cus_no number,
pass_no number
);
insert all
when cus_count = 1 and pass_count = 1 then
into tbl_target values (cus_no, pass_no)
else
into tbl_target_dup values (cus_no, pass_no)
select a.pass_no, b.cus_no,
count(*) over (partition by a.pass_no) as pass_count,
count(*) over (partition by b.cus_no) as cus_count
from tbl_one a
join tbl_two b on b.pass_no = a.pass_no;
This allows you to have more columns than those affected by the PK/UK, and insert them only into the real table if you prefer, or a subset into the 'error' table. With just those two columns in each table you get:
select * from tbl_target;
CUS_NO PASS_NO
---------- ----------
103 20
select * from tbl_target_dup;
CUS_NO PASS_NO
---------- ----------
101 10
102 10
SQL Fiddle demo.
You could do the same thing with two inserts based on the same select, one with a subquery checking that both counts are 1, other checking that at least one is not, but this might perform better.

varrays oracle ORA-02330

I'm develop a simple app for learn about oracle and database object-relational with objects and varrays... I did the next code:
this is my varrays
SQL> create or replace type software_va as varray(3) of varchar2(30);
2 /
here is an object that I created:
SQL> create or replace type cargo1 as object(
2 id_cargo number,
3 nom_cargo varchar2(20),
4 suc ref sucursal);
5 /
when I try to create the table at this way:
SQL> create table cargos of cargo1(
2  primary key(id_cargo),
3  manejosoft software_va);
I got this error:
ERROR en line 3:
ORA-02330: datatype specification not allowed
I don't understand why I got this error and don't know if I have something wrong
If you want a relational table with both object and varray columns, this should work, and still has a primary key based on the object's ID:
create table cargos
(
cargo cargo1,
manejosoft software_va,
constraint cargos_pk primary key (cargo.id_cargo)
);
Table created.
insert into cargos values (cargo1(1, 'test'), software_va('a', 'b', 'c'));
1 row created.
insert into cargos values (cargo1(1, 'dup test'), software_va('d', 'e', 'f'));
insert into cargos values (cargo1(1, 'dup test'), software_va('d', 'e', 'f'))
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CARGOS_PK) violated
select * from cargos;
CARGO(ID_CARGO, NOM_CARGO)
--------------------------------------------------------------------------------
MANEJOSOFT
--------------------------------------------------------------------------------
CARGO1(1, 'test')
SOFTWARE_VA('a', 'b', 'c')
select c.cargo.nom_cargo
from cargos c
where c.cargo.id_cargo = 1;
CARGO.NOM_CARGO
--------------------
test
If you wanted an object table then you couldn't have the varray column as mentioned in comments:
create table cargos of cargo1
(
primary key(id_cargo)
);

Can not delete Metadata entry in Spatial DB

I am trying to a spatial data table in my db using :
CREATE TABLE building (buildid VARCHAR(15) PRIMARY KEY, buildname VARCHAR(50),numpoint NUMBER,points SDO_GEOMETRY);
CREATE INDEX building_spatial_idx ON building(points) INDEXTYPE IS MDSYS.SPATIAL_INDEX;
INSERT INTO USER_SDO_GEOM_METADATA(TABLE_NAME,COLUMN_NAME,DIMINFO,SRID)
VALUES (
'building',
'points',
SDO_DIM_ARRAY( --820*580 grid
SDO_DIM_ELEMENT('X', 0, 820, 1),
SDO_DIM_ELEMENT('Y', 0, 580, 1)
),
NULL --SRID
);
When I executed it for the first time it did not give any errors but afterwords it is giving error
insert into user_sdo_geom_metadata values
*
ERROR at line 1:
ORA-00001: unique constraint (MDSYS.UNIQUE_LAYERS) violated
ORA-06512: at "MDSYS.SDO_GEOM_TRIG_INS1", line 27
ORA-04088: error during execution of trigger 'MDSYS.SDO_GEOM_TRIG_INS1'
Is it happening because am trying to give meta-data for same table again.
Or is there any other reason. How can i delete all the index,metadata,table at a single go and remove this error.
A new Schema creation solved the problem,

Resources