DECLARE A FOREIGN KEY CONSTRAINT causes ORA-00907 - oracle

I have a create table statement, but it's not compile,
The fk_myFirstTable CONSTRAINT cause the problem.
Someone know what wrong in the CONSTRAINT ?
I get : ORA-00907: - "missing right parenthesis"
CREATE TABLE "mySchema"."mySecondTable "
(
idNumber NUMBER(10,0) NOT NULL ENABLE,
SystemId NUMBER(10,0) NOT NULL ENABLE,
CONSTRAINT "mySecondTable _PK" PRIMARY KEY (idNumber ),
CONSTRAINT "fk_myFirstTable" FOREIGN KEY (SystemId) REFERENCES myFirstTable(SystemId)
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MYTBS" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 0 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MYTBS" ;
thanks for help!

The USING INDEX clause is part of the primary key constraint. I moved the foreign key constraint to after the ENABLE,.
Try this:
CREATE TABLE "mySchema"."mySecondTable "
(
idNumber NUMBER(10,0) NOT NULL ENABLE,
SystemId NUMBER(10,0) NOT NULL ENABLE,
CONSTRAINT "mySecondTable _PK" PRIMARY KEY (idNumber )
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MYTBS" ENABLE,
CONSTRAINT "fk_myFirstTable" FOREIGN KEY (SystemId) REFERENCES myFirstTable(SystemId)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 0 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MYTBS" ;

I have a stupid space in the declare line:
CREATE TABLE "mySchema"."mySecondTable "
instead of:
CREATE TABLE "mySchema"."mySecondTable"
Sorry for the lack of attention!

A few objections, if I may: you didn't write that code, did you? It suspiciously looks like a copy/paste from some GUI which - true - fetches all that information from dictionary and creates a jungle, when all you need is a bush.
Don't enclose Oracle objects' names into double quotes and - even worse - use mixed case within those double quotes. Otherwise, you'll have to reference them using double quotes, the same mixed case, ALWAYS. A true nightmare. By default, Oracle will create them using uppercase, but you can reference them using any case (lower, mixed, upper - just don't store them as such!).
NO: "mySecondTable " - note a trailing space - horror!
YES (as their name will be stored in UPPERCASE anyway):
mysecondtable
MYSECONDTABLE
mySecondTable
There's no need to specify NOT NULL for column(s) that make(s) primary key constraint; by default, they can't be NULL.
All that storage mumbo-jumbo ... phew, if this is an ordinary table we use for daily purposes, you really shouldn't take care about it and let Oracle handle those information. I agree - no problem in specifying all that if you know what they are. Reading your question, I think you should rely on Oracle.
Shortened, your query might/should look as follows (I'm creating the my_first_table, just to make the foreign key constraint work):
SQL> create table my_first_table
2 (system_id number constraint pk_mft primary key);
Table created.
SQL> create table my_second_table
2 (id_number number constraint pk_mst primary key,
3 system_id number constraint fk_my_first_table references my_first_table (system_id)
4 not null
5 );
Table created.
SQL>

Related

Materialized View has stop refreshing after index creation in oracle. Is recompile required post index creation?

CREATE MATERIALIZED VIEW "FCSMARC"."CS_LOAN_RECEIPT_DTL" ("ACCOUNTNO", "AGREEMENTID", "CUSTOMERNAME", "RECEIPTNO", "CHEQUENUM", "RECEIPT_AMOUNT", "PAYMENT_TYPE", "INSTRUMENT_TYPE", "DRAWNON", "CITY", "CHEQUEDATE")
ORGANIZATION HEAP
PCTFREE 10
PCTUSED 40
INITRANS 1
MAXTRANS 255
NOCOMPRESS
LOGGING
STORAGE(
INITIAL 1048576
NEXT 1048576
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT
)
TABLESPACE "MAIN_TBL" BUILD IMMEDIATE USING INDEX REFRESH START WITH SYSDATE NEXT SYSDATE + 2/24
USING DEFAULT LOCAL ROLLBACK SEGMENT USING ENFORCED CONSTRAINTS DISABLE QUERY REWRITE
AS
SELECT accountno,agreementid,customername,receiptno,chequenum,receipt_amount,payment_type, instrument_type,drawnon,city,chequedate FROM cs_loan_receipt_dtl_rl#fcsmarc_to_collstage UNION ALL
SELECT accountno,agreementid,customername,receiptno,chequenum,receipt_amount,payment_type, instrument_type,drawnon,city,chequedate

oracle saving non english characters not working via script

I have c# program that fills me database with values, but when I try to export them to import.sql and then run import.sql script it always fill database with special characters and not czech letters.
Via example
I have this table
CREATE TABLE Document
(
"Id" NUMBER(19,0) GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE ,
"name" NVARCHAR2(200),
"fullname" NVARCHAR2(300)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ;
and when I export data for example 1, "Velmi přívětivý dokument", "Dokument který byl velmi přívětivý do doby než SQL přeházelo písmenka"
and insert inside import.sql looks like this
CREATE TABLE Document
(
"Id" NUMBER(19,0) GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE ,
"name" NVARCHAR2(200),
"fullname" NVARCHAR2(300)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ;
INSERT INTO Document ("Id","name","fullname") values (1, "Velmi přívětivý dokument", "Dokument který byl velmi přívětivý do doby než SQL přeházelo písmenka")
and run script (Using dockerd to host local db) I get special characters like this 'p��v�iv�' instead 'Přívětivý'
but when I run that INSERT inside SQL Developer it insert all czech characters inside db without any problem.
I am not really sure where the problem is.
You must set encoding, a.k.a. "code page" of your terminal (I assume cmd.exe, but I don't know docker) to the encoding of your export file. The default is neither UTF-8, nor CP1250, nor CP1252.
Most like it is CP437, CP850 or CP852 depending on your Windows language, see https://web.archive.org/web/20170916200715/http://www.microsoft.com/resources/msdn/goglobal/default.mspx
You can interrogate and change it with command chcp
Code Page Identifiers you can find here

Oracle update throws timeout

I am having a very simple oracle update:
update inv_li_pck_inst set mig_li_pck_inst_id = 9377 where id = 9384
Both records exist in the table inv_li_pck_inst: id=9377 and id=9384
Record with id=9377 is migration record.
Problem is that this very simple update query takes ages to run - and at the end throws timeout exception. What could possibly be wrong here? IDs in the table inv_li_pck_inst are unique.
Table DDL:
CREATE TABLE "TESTING_INV"."INV_LI_PCK_INST"
( "ID" NUMBER NOT NULL ENABLE,
"LI_PCK_ID" NUMBER NOT NULL ENABLE,
"WORKFLOW_ID" NUMBER,
"INSERTED" DATE NOT NULL ENABLE,
"INSERTED_BY" NUMBER(9,0) NOT NULL ENABLE,
"UPDATED" DATE,
"UPDATED_BY" NUMBER(9,0),
"DELETED" DATE,
"DELETED_BY" NUMBER(9,0),
"MIG_LI_PCK_INST_ID" NUMBER,
"STATUS_ID" NUMBER,
"WFI_ID" NUMBER,
"TOF_WFI_ID" NUMBER,
CONSTRAINT "PK_INV_LI_PCK_INST" PRIMARY KEY ("ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TESTING_INV_DATA" ENABLE,
CONSTRAINT "FK_LPI_WORKFLOW" FOREIGN KEY ("WORKFLOW_ID")
REFERENCES "TESTING_INV"."WORKFLOW" ("WORKFLOW_ID") ENABLE,
CONSTRAINT "FK_LIPI_STATUS" FOREIGN KEY ("STATUS_ID")
REFERENCES "TESTING_INV"."INV_LI_PCK_INST_STATUS" ("ID") ENABLE,
CONSTRAINT "FK_LIPI_MIG_PCK_INST" FOREIGN KEY ("MIG_LI_PCK_INST_ID")
REFERENCES "TESTING_INV"."INV_LI_PCK_INST" ("ID") ENABLE,
CONSTRAINT "FK_LI_PCK_INST_WFI" FOREIGN KEY ("WFI_ID")
REFERENCES "TESTING_INV"."WFI" ("WFI_ID") ENABLE,
CONSTRAINT "FK_INV_LI_PCK_INST5" FOREIGN KEY ("TOF_WFI_ID")
REFERENCES "TESTING_INV"."WFI" ("WFI_ID") ENABLE,
CONSTRAINT "FK_LPI_LI_PCK" FOREIGN KEY ("LI_PCK_ID")
REFERENCES "TESTING_INV"."INV_LI_PCK" ("ID") ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TESTING_INV_DATA" ;
CREATE INDEX "TESTING_INV"."IDX_LI_PCK_INST_WFI" ON "TESTING_INV"."INV_LI_PCK_INST" ("WFI_ID")
PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TESTING_INV_DATA" ;
CREATE UNIQUE INDEX "TESTING_INV"."UN_LI_PCK_INST" ON "TESTING_INV"."INV_LI_PCK_INST" (NVL2("DELETED","ID",NULL), "WORKFLOW_ID")
PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TESTING_INV_DATA" ;
CREATE OR REPLACE EDITIONABLE TRIGGER "TESTING_INV"."TRG_INV_LI_PCK_INST"
before insert on INV_LI_PCK_INST
for each row
begin
select SEQ_INV_LI_PCK_INST.nextval into :new.ID from dual;
end;
/
ALTER TRIGGER "TESTING_INV"."TRG_INV_LI_PCK_INST" ENABLE;
Most probable scenario is that there is an other migration session (or more of them) and it blocks your session.
Simple setup
create table testing
(ID NUMBER primary key,
MIG_ID NUMBER );
alter table testing add (
constraint mig foreign key(MIG_ID) references testing(id));
insert into testing (id, mig_id) values(9384, null);
insert into testing (id, mig_id) values(9377, null);
commit;
If you now performs the UPDATE is goes perfectly smooth:
update testing set mig_id = 9377 where id = 9384;
But if you before the update performs a delete of the migrated ID form an other session and do not commit it, your update will "hang" forever.
-- perform from other session and do not commit
delete from testing where id = 9377;
Why, because if the update would be done and the delete session would be commited - the referential integrity will be violeted. The UPDATE must wait until the deleted is commited or rollbacked to see if the reference ID is there or not.
How to dignose?
Simple check the v$session (or GV of RAC) and see the blocking_status and event
select SID, SERIAL#,STATUS,SQL_ID, BLOCKING_SESSION_STATUS, BLOCKING_SESSION,EVENT
from v$session
where USERNAME = your_user
You will see (most probably) your session with BLOCKING_SESSION_STATUS = VALID and
EVENT = enq: TX - row lock contention. In BLOCKING_SESSION you can find the session ID of your interacting session.
The SQL_ID will tell you what the other session is doing.

Get complete ddl for index in oracle

I am using oracle 11g/12c. I want to get ddl of indexes in my database. For this I used the query -
SELECT DBMS_METADATA.GET_DDL('INDEX','SYS_IL0000091971C00001$$','CCEEXPERTS') FROM dual
Here 'SYS_IL0000091971C00001$$' is my index name and 'CCEEXPERTS' is my owner name.
From this I get the ddl -
CREATE UNIQUE INDEX "CCEEXPERTS"."SYS_IL0000091971C00001$$" ON "CCEEXPERTS"."DATABLOB" (
And my actual ddl is -
CREATE UNIQUE INDEX "CCEEXPERTS"."SYS_IL0000091971C00001$$" ON "CCEEXPERTS"."DATABLOB" (
PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
PARALLEL (DEGREE 0 INSTANCES 0) ;
In actual ddl after "CCEEXPERTS"."DATABLOB" ( , next line character and from their the ddl is truncted.
How can I get the complete ddl? Please help me...
Thanks in advance.
In SQLplus, set these before running the procedure.
set long 100000
set longchunksize 100000
Oracle has a DBMS_METADATA package to retrieve and customize the DDL returned. Default settings for all indexes SQL:
select DBMS_METADATA.GET_DDL('INDEX', index_name)
from all_indexes
where owner in (USER, 'USER_OTHER_THAN_LOGGED_IN_USER');
A pl/sql block example:
set serveroutput on
DECLARE
V_DDL CLOB;
BEGIN
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);
DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM, 'PRETTY', true);
dbms_metadata.set_transform_param(dbms_metadata.session_transform,'TABLESPACE',false);
V_DDL := DBMS_METADATA.GET_DDL('VIEW', 'A_VIEW_NAME');
DBMS_OUTPUT.PUT_LINE(V_DDL);
END;

Oracle: very slow query example. How to speed it up by looking at session browser?

I'm going to detail in every aspect a query which is really slow. Can you give me some advice to understand what should I consider to speed it up?
It involves just two tables.
SELECT /*+ leading(tb) */![enter image description here][1]
DISTINCT COUNT (huB.FLH_ID_MESSAGGIO), hub.flh_STATO, TB.LOTTO
FROM ENI_FLUSSI_HUB HUB, NETATEMP.TMP_GAB_RECOVERY_SCARTI tb
WHERE HUB.FLH_ID_MESSAGGIO = tb.FLH_ID_MESSAGGIO
AND hub.FLH_TIPO_PROCESSO_COD = tb.FLH_TIPO_PROCESSO_COD
AND TB.LOTTO IN (:"SYS_B_0", :"SYS_B_1", :"SYS_B_2", :"SYS_B_3")
AND HUB.FLH_FLAG_ANN = :"SYS_B_4"
GROUP BY hub.flh_STATO, TB.LOTTO
ORDER BY HUB.FLH_STATO
The first index script:
CREATE INDEX NETATEMP.ENI_ETAI_IDX2 ON NETATEMP.TMP_GAB_RECOVERY_SCARTI
(LOTTO, OPERATORE)
NOLOGGING
TABLESPACE NETATEMP_IDXTI
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;

Resources