I am trying to check if "visitors" are inside a "village building". But everytime i execute my SELECT statement i get the error that "position" in the table "visitors" is an invalid identifier.
DROP TABLE village CASCADE CONSTRAINTS;
CREATE TABLE village (
building_id integer PRIMARY KEY,
name VARCHAR2(30),
visitors integer,
building SDO_GEOMETRY
);
/*****************index********************/
delete from user_sdo_geom_metadata where table_name = 'VILLAGE';
INSERT INTO user_sdo_geom_metadata
( TABLE_NAME,
COLUMN_NAME,
DIMINFO,
SRID
)
VALUES
( 'village',
'building',
SDO_DIM_ARRAY( -- 20X20 grid
SDO_DIM_ELEMENT('X', 0, 1000, 0.5),
SDO_DIM_ELEMENT('Y', 0, 1000, 0.5)
),
NULL -- SRID
);
-- inserts
INSERT INTO village VALUES(1,'Kirche', 4,
SDO_GEOMETRY(
2003,
NULL,
NULL,
SDO_ELEM_INFO_ARRAY(1,1003,1),
SDO_ORDINATE_ARRAY(100,100, 100,120, 80,120, 80,150 ,100,200, 150,200, 150,150, 200,150, 200,120, 150,120, 150,100)
)
);
drop table visitors;
create table visitors(
id integer,
position SDO_GEOMETRY
);
drop sequence visitors_seq;
create sequence visitors_seq;
INSERT INTO visitors VALUES (visitors_seq.nextval,
SDO_GEOMETRY(
2001,
NULL,
SDO_POINT_TYPE(160, 100, NULL),
NULL,
NULL
)
);
commit;
SELECT * FROM village WHERE SDO_INSIDE(village.building, visitors.position) = 'TRUE';
Can it be that i have to use SDO_GEOMETRY data as a parameter instead of "visitors.position"?
Your query FROM clause does not reference the table visitors. You'll need to include it.
Related
Object type
create or replace TYPE "TYPE_FE_FEE_DETAIL" AS OBJECT
(
FE_AMOUNT VARCHAR2(25),
CURR_ID VARCHAR2(5),
PROFILE_TYPE VARCHAR2(1),
ISO_CODE VARCHAR2(25)
);
create or replace TYPE "TYPE_TB_FE_FEE_DETAIL" AS TABLE OF type_fe_fee_detail;
We have a view with two columns where the second column is of OBJECT TYPE and has data like
EPAYPROD_M3.TYPE_TB_FE_FEE_DETAIL(EPAYPROD_M3.TYPE_FE_FEE_DETAIL('10', '1', '1', '818'))
I need to insert these four values into a table which has four separate columns. I am having in trouble doing so.
TYPE_TB_FE_FEE_DETAIL is a nested table. To get the attribute values, unnest it.
You can do this with the table operator:
create or replace TYPE "TYPE_FE_FEE_DETAIL" AS OBJECT
(
FE_AMOUNT VARCHAR2(25),
CURR_ID VARCHAR2(5),
PROFILE_TYPE VARCHAR2(1),
ISO_CODE VARCHAR2(25)
);
/
create or replace TYPE "TYPE_TB_FE_FEE_DETAIL" AS TABLE OF type_fe_fee_detail;
/
with rws as (
select type_tb_fe_fee_detail(type_fe_fee_detail('10', '1', '1', '818')) obj
from dual
)
select t.*
from rws r, table ( r.obj ) t;
FE_AMOUNT CURR_ID PROFILE_TYPE ISO_CODE
10 1 1 818
Create table A_15006977.vehicle. (
Vin varchar(20) primary key,
Vehicle_type char(20) not null,
Mileage number(20) not null,
Manufacturer char(20) not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values ('tf1bb2ve533093891','panel van',18 325,'man')
A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values
('tf1bb2ve533093822','standard van',79 885,'ford')
Select * from dual;
Create table A_15006977.vehicle (
Vin varchar(20) CONSTRAINT vehicle__vin__pk PRIMARY KEY,
Vehicle_type char(20) CONSTRAINT vehicle__vehicle_type__nn not null,
Mileage number(20) CONSTRAINT vehicle__mileage__nn not null,
Manufacturer char(20) CONSTRAINT vehicle__manufacturer__nn not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
VALUES ( 'tf1bb2ve533093891', 'panel van', 18325, 'man' )
INTO A_15006977.vehicle (vin,vehicle_type,mileage,manufacturer)
values ( 'tf1bb2ve533093822', 'standard van', 79885, 'ford' )
SELECT 1 FROM DUAL;
Or:
Insert Into A_15006977.vehicle( vin,vehicle_type,mileage,manufacturer )
SELECT 'tf1bb2ve533093891','panel van', 18325, 'man' FROM DUAL UNION ALL
SELECT 'tf1bb2ve533093822','standard van', 79885, 'ford' FROM DUAL;
Note:
You had an extra . after the table name in the DDL statement and spaces in the mileage (18 325 and 79 885) which need removing and you needed an INTO keyword before the second insert.
It is also useful to name your constraints (then you can easily determine which constraint has been violated in later statements).
I'm stuck and can't figure out how to add the condition to the CHECK constraint in oracle...
Basically I have a table with the following structure:
CREATE TABLE TEST_TBL
(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y',
CONSTRAINT def_check_const
CHECK (???????)
);
There may be multiple values in this table, however I require that only 'Y' or 'N' can be entered in the DEF_COL column. Also i want to add a constraint to restrict the number of 'Y' in the DEF_COL column to only one. There may be multiple 'N' entries allowed for DEF_COL column. Please could someone guide me as to what must be done to add the constraint in the check section of the create table query?
Thanks in advance....
Oracle Setup:
CREATE TABLE TEST_TBL(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y'
CONSTRAINT def_check_const
CHECK ( DEF_COL IN ('Y', 'N' ) )
);
CREATE UNIQUE INDEX def_col_only_one_y__U
ON TEST_TBL( CASE DEF_COL WHEN 'Y' THEN 'Y' END );
Insert a row:
INSERT INTO TEST_TBL VALUES ( 'A1', 'A2', 'A3', 'A4', 'Y' );
1 rows inserted.
Insert a second column with DEF_COL = 'Y':
INSERT INTO TEST_TBL VALUES ( 'B1', 'B2', 'B3', 'B4', 'Y' )
Error report -
SQL Error: ORA-00001: unique constraint (TEST.DEF_COL_ONLY_ONE_Y__U) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
Update:
To have only one Y for each unique combination of 3 columns then try:
CREATE UNIQUE INDEX C1_2_3__def_col_only_one_y__U
ON TEST_TBL( COL1, COL2, COL3, CASE DEF_COL WHEN 'Y' THEN 'Y' END );
I'm doing an evaluation of FLYWAY and running a very simple script that creates a new table and insert a row to that table. The table creates successfully but I get an ORA-942 error on the INSERT. I have tried every permutation I can think of using upper/lower case and quoted and unquoted schema and table names. I have also tried separating out the INSERT statement into different script from the CREATE statement - all to no avail. Can anyone help explain the format flyway needs for the INSERT statements?
Here are the statements from my most recent attempt. I have tried the same statements with quotes around the table_name, with all lower case, with no quotes around the schema - nothing works. Logged directly into the database as SYS, I can describe the table with this command:
desc "FLYWAY_USER".department
CREATE TABLE "FLYWAY_USER".DEPARTMENT
( DEPARTMENT_id NUMBER(3) NOT NULL,
DEPARTMENT VARCHAR2(64) NOT NULL,
display_name VARCHAR2(64) NOT NULL,
description VARCHAR2(400),
create_date DATE NOT NULL,
update_date DATE NOT NULL,
created_by VARCHAR2(80) NOT NULL,
updated_by VARCHAR2(80) NOT NULL )
TABLESPACE users;
INSERT INTO "FLYWAY_USER".DEPARTMENT
( DEPARTMENT_id,
DEPARTMENT,
display_name,
description,
create_date,
update_date,
created_by,
updated_by )
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM DEPARTMENT ),
'HUMAN_RESOURCES',
'Human Resources',
'The best place to eat smores or get a raise.',
Sysdate,
Sysdate,
'AT09001',
'AT09001' );
Thanks
Logged directly into the database as SYS
Use better a dedicated application user.
Anyway if you use a different user that the schema owner of your objects (FLYWAY_USER) you must qualify ALL your references.
especially
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM DEPARTMENT ),
should be
VALUES
( ( SELECT Nvl( Max(DEPARTMENT_id), 0) + 1 FROM FLYWAY_USER.DEPARTMENT ),
I have this table below in the picture that i want to create as an object-oriented table. I dont want the usual create table with relationships.... I just want to learn how to turn this table into an object-oriented table. Below is a picture of my tables and how are they connected:
CREATE TYPE A_TYPE AS OBJECT(
id INT,
col1 INT
);
/
CREATE TYPE A_REF_TABLE_TYPE AS TABLE OF REF A_TYPE;
/
CREATE TYPE B_TYPE AS OBJECT(
id INT,
col1 INT
);
/
CREATE TYPE B_REF_TABLE_TYPE AS TABLE OF REF B_TYPE;
/
CREATE TYPE C_TYPE AS OBJECT(
id INT,
a_list A_REF_TABLE_TYPE,
b_list B_REF_TABLE_TYPE,
col1 INT
);
/
CREATE TABLE A_TAB OF A_TYPE(
ID PRIMARY KEY
);
CREATE TABLE B_TAB OF B_TYPE(
ID PRIMARY KEY
);
CREATE TABLE C_TAB OF C_TYPE(
ID PRIMARY KEY
)
NESTED TABLE a_list STORE AS c_a_lists
NESTED TABLE b_list STORE AS c_b_lists;
INSERT INTO A_TAB VALUES( A_TYPE( 1, 3 ) );
INSERT INTO A_TAB VALUES( 2, 4 );
INSERT INTO B_TAB VALUES ( B_TYPE( 1, 7 ) );
INSERT INTO B_TAB VALUES ( 2, 2 );
INSERT INTO B_TAB VALUES ( 3, 10 );
INSERT INTO C_TAB VALUES (
1,
A_REF_TABLE_TYPE(
( SELECT REF(a) FROM A_TAB a WHERE ID = 2 ) -- Single value
),
( -- Multiple values
SELECT CAST( COLLECT( REF(b) ) AS B_REF_TABLE_TYPE )
FROM TAB_B b
WHERE ID IN ( 1, 3 )
),
42
);
INSERT INTO C_TAB VALUES (
2,
NULL, -- Unknown
B_REF_TABLE_TYPE(), -- No values
54
);
Output:
SELECT * FROM C_TAB;
ID A_LIST B_LIST COL1
-- ------------------------------- --------------------------------------------- ----
1 A_REF_TABLE_TYPE( A_TYPE(2,4) ) B_REF_TABLE_TYPE( B_TYPE(1,7), B_TYPE(3,10) ) 42
2 (null) B_REF_TABLE_TYPE() 54