Even with complete and readable statements, an error occurs: SQL Error [900] [42000]: ORA-00900: invalid SQL statement - sql

I have two tables created in my Oracle database: the table 'responsible' (which has the id and description of the responsible) and the table 'ident' (which has id's of three types of expenses).
Below, I am trying to create a script in which:
Add values to the columns of the 'responsible';
create a 'type_responsible' table, and insert values into it;
create a relationship table called 'responsible_by_type', which, using the data from the table 'responsible', 'ident' and 'responsible_type' establishes one relationship between them.
These changes in data type to primary key or foreign key are attempts to get the bank to recognize the fields as relatable.
I get the error: 'SQL Error [900] [42000]: ORA-00900: invalid SQL statement'. Even if I execute one instruction at a time, which makes no sense.
I really don't know where my mistake is. Since I can't run the script, I can't know if the script allows me to reach my goal. Could someone help me?
<--Table RESPONSIBLE-->
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (1, 'payer1');
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (2, 'payer2');
ALTER TABLE RESPONSIBLE ADD CONSTRAINT PK_RESPONSIBLE_ID PRIMARY KEY (RESPONSIBLE_ID);
ALTER TABLE RESPONSIBLE ADD CONSTRAINT UNIQUE_RESPONSIBLE_DESC UNIQUE (RESPONSIBLE_DESC);
<--Table IDENT-->
<--OPTION ONE-->
ALTER TABLE IDENT ADD CONSTRAINT pk_RESPONSIBLES_TYPE_IDS primary key(RESPONSIBLE_DEBTS_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_EXPENSES_ID);
<--OPTION TWO-->
ALTER TABLE IDENT ADD CONSTRAINT FK_RESPONSIBLE_DEBTS_ID FOREIGN KEY (RESPONSIBLE_DEBTS_ID);
ALTER TABLE IDENT ADD CONSTRAINT FK_RESPONSIBLE_ASSETS_ID FOREIGN KEY (RESPONSIBLE_ASSETS_ID);
ALTER TABLE IDENT ADD CONSTRAINT FK_RESPONSIBLE_EXPENSES_ID FOREIGN KEY (RESPONSIBLE_EXPENSES_ID);
<--Table RESPONSIBLE_TYPE-->
CREATE TABLE RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID NUMBER(10), RESPONSIBLE_TYPE_DESC VARCHAR(100));
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (1, 'Assets');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (2, 'Expenses');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (3, 'Debts');
ALTER TABLE RESPONSIBLE_TYPE ADD CONSTRAINT PK_RESPONSIBLE_TYPE_ID PRIMARY KEY (RESPONSIBLE_TYPE_ID);
<--Table RESPONSIBLE_BY_TYPE-->
CREATE TABLE RESPONSIBLE_BY_TYPE(RESPONSIBLE_ID NUMBER(10) NOT NULL, RESPONSIBLE_EXPENSES_ID NUMBER(10) NOT NULL,
RESPONSIBLE_ASSETS_ID NUMBER(10) NOT NULL, RESPONSIBLE_DEBTS_ID NUMBER(10) NOT NULL, RESPONSIBLE_DESC VARCHAR(100), RESPONSIBLE_TYPE VARCHAR(100),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_ID) REFERENCES RESPONSIBLE(RESPONSIBLE_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_EXPENSES_ID) REFERENCES IDENT(RESPONSIBLE_EXPENSES_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_ASSETS_ID) REFERENCES IDENT(RESPONSIBLE_ASSETS_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_DEBTS_ID) REFERENCES IDENT(RESPONSIBLE_DEBTS_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_TYPE) REFERENCES RESPONSIBLE_TYPE(RESPONSIBLE_TYPE),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_DESC) REFERENCES RESPONSIBLE(RESPONSIBLE_DESC);
In the first steps it was running. From the moment I created the table 'responsible_by_type' and started changing the data types of the other tables, I couldn't do anything anymore.
Now, even when I execute a simple insert instruction, I get the error I mentioned above.

This works from Oracle SQL Developer
-- ******************* Existing Table RESPONSIBLE - you don't need to create it ***************************
--CREATE TABLE RESPONSIBLE
-- (
-- RESPONSIBLE_ID NUMBER(10),
-- RESPONSIBLE_DESC VARCHAR2(100)
-- );
-- -------------- Alter table --------------------
ALTER TABLE RESPONSIBLE ADD CONSTRAINT pk_responsible_id PRIMARY KEY (RESPONSIBLE_ID);
ALTER TABLE RESPONSIBLE ADD CONSTRAINT unique_responsible_desc UNIQUE (RESPONSIBLE_DESC);
-- ---------- I n s e r t s ----------------
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (1, 'payer1');
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (2, 'payer2');
-- ******************* Existing Table IDENT - you don't need to create it ***************************
-- CREATE TABLE IDENT
-- (
-- RESPONSIBLE_DEBTS_ID NUMBER,
-- RESPONSIBLE_ASSETS_ID NUMBER,
-- RESPONSIBLE_EXPENSES_ID NUMBER
-- );
-- -------------- Alter table --------------------
ALTER TABLE IDENT ADD CONSTRAINT pk_responsibles_type_ids
PRIMARY KEY(RESPONSIBLE_DEBTS_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_EXPENSES_ID);
-- *************** New Table RESPONSIBLE_TYPE ***************************
CREATE TABLE RESPONSIBLE_TYPE
(
RESPONSIBLE_TYPE_ID NUMBER(10),
RESPONSIBLE_TYPE_DESC VARCHAR(100),
CONSTRAINT pk_responsible_type_id PRIMARY KEY (RESPONSIBLE_TYPE_ID)
-- option 2 -- PK(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC)
);
-- ---------- I n s e r t s ----------------
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (1, 'Assets');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (2, 'Expenses');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (3, 'Debts');
-- *************** New Table RESPONSIBLE_BY_TYPE ***************************
CREATE TABLE RESPONSIBLE_BY_TYPE
(
RESPONSIBLE_ID NUMBER(10) NOT NULL,
RESPONSIBLE_EXPENSES_ID NUMBER(10) NOT NULL,
RESPONSIBLE_ASSETS_ID NUMBER(10) NOT NULL,
RESPONSIBLE_DEBTS_ID NUMBER(10) NOT NULL,
RESPONSIBLE_DESC VARCHAR(100),
RESPONSIBLE_TYPE_ID NUMBER(10),
-- NOTE *** FK Constraints References PKs in referenced tables *** ----------
CONSTRAINT fk_resp_by_type_resp
FOREIGN KEY(RESPONSIBLE_ID) REFERENCES RESPONSIBLE(RESPONSIBLE_ID),
CONSTRAINT fk_resp_by_type_idnt
FOREIGN KEY(RESPONSIBLE_EXPENSES_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_DEBTS_ID)
REFERENCES IDENT(RESPONSIBLE_EXPENSES_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_DEBTS_ID),
CONSTRAINT fk_resp_by_type_resptype
FOREIGN KEY(RESPONSIBLE_TYPE_ID) REFERENCES RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID)
-- option 2 -- FK(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) references RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC)
);
-- R e s u l t
table RESPONSIBLE created.
table RESPONSIBLE altered.
table RESPONSIBLE altered.
1 rows inserted.
1 rows inserted.
table IDENT created.
table IDENT altered.
table RESPONSIBLE_TYPE created.
1 rows inserted.
1 rows inserted.
1 rows inserted.
table RESPONSIBLE_BY_TYPE created

Related

SQL ORA-02291: integrity constraint violated - parent key not found

I have encountered some problem with SQL foreign key.
Here are my table and insert SQL.
create table passenger_card2
(
phone char(20) primary key,
name char(20)
);
create table card
(
card_num char(20) primary key,
balance number(10,2),
cvn char(20)
);
create table passenger_card1
(
sin integer primary key,
user_id char(20) not null unique,
phone char(20),
card_num char(20) unique,
foreign key(phone) references passenger_card2,
foreign key (card_num) references card
);
And here are my INSERT statements:
INSERT INTO PASSENGER_CARD2 VALUES ( '111222333' , 'Ace');
INSERT INTO CARD VALUES ( '1000' , '100.1' , '110');
INSERT INTO PASSENGER_CARD1 VALUES ('100', 'aaaa', '111222333', '1000');
However, I get an error when I tried to insert PASSENGER_CARD1 data:
SQL ORA-02291: integrity constraint violated - parent key not found
I am not sure why my foreign key is wrong?
I am not sure if this is going to be right but you should make the table 2 first before you create the first table. The database is confused because it wouldnt make sense telling them that theres gonna be a foreign key in the second table but the table isnt created. Run the code for the second table first and then run the code for the first table.

Constraints interdependent tables [duplicate]

This question already has answers here:
Are circular references acceptable in database?
(8 answers)
Closed 5 years ago.
I created 2 tables which are dependent on each other like this.
CREATE TABLE A(NO1 NUMBER(2) PRIMARY KEY,NO2 NUMBER(2));
Table created.
CREATE TABLE B(NO1 NUMBER(2) PRIMARY KEY,NO2 NUMBER(2));
Table created.
ALTER TABLE A ADD CONSTRAINT AA FOREIGN KEY(NO2) REFERENCES B(NO1);
Table altered.
ALTER TABLE B ADD CONSTRAINT BB FOREIGN KEY(NO2) REFERENCES B(NO1);
Table altered.
INSERT INTO A VALUES(10,20);
INSERT INTO A VALUES(10,20);
ERROR at line 1:
ORA-02291: integrity constraint (SUBK.AA) violated - parent key not found
INSERT INTO B VALUES(10,20);
INSERT INTO B VALUES(10,20);
ERROR at line 1:
ORA-02291: integrity constraint (SUBK.BB) violated - parent key not found
How to insert data in table a and b
You are asking for troubles ;-), but You can do this in two steps:
insert into A values(10, null);
insert into B values(20, null);
update A set no2 = 20 where no1 = 10;
update B set no2 = 10 where no1 = 20;
You create an invalid constraints:
ALTER TABLE B ADD CONSTRAINT BB FOREIGN KEY(NO2) REFERENCES B(NO1);
Foreign key should be to different table, not to column in the same table.
If you want circular constraint A -> B and B ->A you can insert first data and then add constraints. your foreign key should direct to primary key, so new SQL will be :
CREATE TABLE A(NO1 NUMBER(2) PRIMARY KEY,NO2 NUMBER(2));
CREATE TABLE B(NO1 NUMBER(2) PRIMARY KEY,NO2 NUMBER(2));
INSERT INTO A VALUES(10,20);
INSERT INTO B VALUES(10,20);
ALTER TABLE A ADD CONSTRAINT AA FOREIGN KEY(NO1) REFERENCES B(NO1);
ALTER TABLE B ADD CONSTRAINT BB FOREIGN KEY(NO1) REFERENCES A(NO1);
Make your constraints deferred. Doing so will cause them to not be enforced until the transaction commits.
Under this setup, you can insert records into both tables without getting an error, as long as both inserts happen in the same transaction.
CREATE TABLE A(NO1 NUMBER(2) PRIMARY KEY,NO2 NUMBER(2));
CREATE TABLE B(NO1 NUMBER(2) PRIMARY KEY,NO2 NUMBER(2));
ALTER TABLE A ADD CONSTRAINT
AA FOREIGN KEY(NO2) REFERENCES B(NO1) INITIALLY DEFERRED DEFERRABLE;
ALTER TABLE B ADD CONSTRAINT
BB FOREIGN KEY(NO2) REFERENCES A(NO1) INITIALLY DEFERRED DEFERRABLE;
INSERT INTO A VALUES(10,20);
INSERT INTO B VALUES(20,10);
COMMIT;
NOTE: I assumed you wanted the BB constraint to refer to table A, so I changed it.

Unique constraint on two database columns with reverse direction

I'm stuck upon a following problem: Let's say I have a table with relations to itself:
CREATE TABLE ITEM (
ID NUMBER(18) PRIMARY KEY,
NAME VARCHAR2(100 CHAR) NOT NULL
);
ALTER TABLE ITEM ADD CONSTRAINT PK_ITEM PRIMARY KEY (ID);
CREATE TABLE ITEM_RELATION (
FIRST_ITEM_ID NUMBER(18) NOT NULL,
SECOND_ITEM_ID NUMBER(18) NOT NULL,
RELATION_TYPE VARCHAR2(1) NOT NULL
);
ALTER TABLE ITEM_RELATION ADD CONSTRAINT PK_ITEM_RELATION PRIMARY KEY (FIRST_ITEM_ID, SECOND_ITEM_ID, RELATION_TYPE);
--ALTER TABLE ITEM_RELATION ADD CONSTRAINT UK_ITEMS UNIQUE (FIRST_ITEM_ID, SECOND_ITEM_ID, RELATION_TYPE);
ALTER TABLE ITEM_RELATION ADD CONSTRAINT FK_FIRST_ITEM FOREIGN KEY (FIRST_ITEM_ID) REFERENCES ITEM(ID);
ALTER TABLE ITEM_RELATION ADD CONSTRAINT FK_SECOND_ITEM FOREIGN KEY (SECOND_ITEM_ID) REFERENCES ITEM(ID);
Now let's say, I wan't the relation to be directional, that is if item 1 has relation to item 2 of certain type, the item 2 shouldn't have the same relation to item 1.
That is, the following should not be permitted:
INSERT INTO ITEM (ID, NAME) VALUES (1, 'Item 1');
INSERT INTO ITEM (ID, NAME) VALUES (2, 'Item 2');
INSERT INTO ITEM_RELATION(FIRST_ITEM_ID, SECOND_ITEM_ID, RELATION_TYPE) VALUES (1, 2, 'R');
INSERT INTO ITEM_RELATION(FIRST_ITEM_ID, SECOND_ITEM_ID, RELATION_TYPE) VALUES (2, 1, 'R');
It means that the table ITEM_RELATION defines a direction of this connection, and it shouldn't be allowed to add a reversed relation.
Is it possible with Oracle DB?
You can do this with a unique index. In Oracle, you can use functions in indexes, so this will work:
create unique index unq_item_relation_3 on
item_relation(RELATION_TYPE ,
least(FIRST_ITEM_ID, SECOND_ITEM_ID),
greatest(FIRST_ITEM_ID, SECOND_ITEM_ID)
);
You could get the same effect with a check constraint, by requiring that FIRST_ITEM_ID be less than SECOND_ITEM_ID:
alter table item_relation add constraint chk_item_relation_2
check ((FIRST_ITEM_ID < SECOND_ITEM_ID);
However, this requires that the items be inserted in a particular order.

Oracle SQL, trigger inserting row into table X as FK for currently inserted row in table Y

So in Oracle 11g I have the following:
CREATE TABLE OBJECT(
ID NUMBER(8) NOT NULL,
CATEGORY_ENUM_ID NUMBER(8) NOT NULL
);
CREATE SEQUENCE OBJECT_SEQ
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
/
CREATE TABLE TREE(
ID NUMBER(8) NOT NULL,
OBJECT_ID NUMBER(8) NOT NULL,
NAME NVARCHAR2(128)
);
CREATE TABLE CATEGORY_ENUM(
ID NUMBER(8) NOT NULL,
NAME NVARCHAR2(64)
);
-- PK's
ALTER TABLE TREE
ADD CONSTRAINT TREE_PK PRIMARY KEY (ID);
ALTER TABLE OBJECT
ADD CONSTRAINT OBJECT_PK PRIMARY KEY (ID);
ALTER TABLE CATEGORY_ENUM
ADD CONSTRAINT CATEGORY_ENUM_PK PRIMARY KEY (ID)
--- FK's
ALTER TABLE TREE
ADD CONSTRAINT TREE_OBJECT_FK FOREIGN KEY (OBJECT_ID)
REFERENCES OBJECT (ID);
ALTER TABLE OBJECT
ADD CONSTRAINT OBJECT_CATEGORY_FK FOREIGN KEY (CATEGORY_ENUM_ID)
REFERENCES CATEGORY_ENUM (ID);
-- Closed dictionary sample data
INSERT INTO CATEGORY_ENUM (ID, NAME) VALUES (1, 'TREE');
INSERT INTO CATEGORY_ENUM (ID, NAME) VALUES (2, 'HERB');
INSERT INTO CATEGORY_ENUM (ID, NAME) VALUES (3, 'SHROOM');
-- Triggers
CREATE OR REPLACE TRIGGER TREE_before_insert
BEFORE INSERT
ON TREE
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
CATEGORY_ID NUMBER;
BEGIN
SELECT ID INTO CATEGORY_ID FROM CATEGORY_ENUM WHERE NAME = 'TREE' AND ROWNUM <= 1;
if :NEW.OBJECT_ID is null then
:NEW.OBJECT_ID := OBJECT_SEQ.nextval;
INSERT INTO OBJECT (ID, CATEGORY_ENUM_ID) VALUES (:NEW.OBJECT_ID, CATEGORY_ID);
end if;
END;
/
If next I run:
INSERT INTO TREE (ID, NAME) VALUES (1, 'Tree1');
INSERT INTO TREE (ID, NAME) VALUES (2, 'Tree2');
I get an error:
...
TRIGGER TREE_BEFORE_INSERT compiled
Error starting at line 91 in command:
INSERT INTO TREE (ID, NAME) VALUES (1, 'Tree1')
Error report:
SQL Error: ORA-02291: integrity constraint (HR.TREE_OBJECT_FK) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
1 rows inserted.
But if before the above, I insert anything into FEATURES table (like below), it works fine.
INSERT INTO OBJECT (ID, CATEGORY_ENUM_ID) VALUES (0, 1);
INSERT INTO TREE (ID, NAME) VALUES (1, 'Tree1');
INSERT INTO TREE (ID, NAME) VALUES (2, 'Tree2');
So the problem occurs only for the very first insert, the others work fine, IDs assigned from OBJECT_SEQ.nextval are proper.
What am I doing wrong ?
Jah bless ya for help.
EDIT I have removed some unnecessary code, so now it's more clear and shorter.
The problem here is that you have a primary key constraint placed on the "TREE" table and a foreign key constraint placed on the "OBJECT" table. So any new record that you try inserting into the table with foreign key constraint, make sure it is already available in the table that has the primary key mapping.
Example shown below:
**Tables:**
create table tab1
(id number,
name varchar2(100)
)
create table tab2
(id number,
name varchar2(100)
)
**constraints added:**
alter table tab1 add constraint pk_tab1_id primary key (id)
alter table tab2 add constraint fk_tab2_id foreign key(id) references tab1(id)
**Insert statement on the second table:**
insert into tab2(values(1,'abc')
**Error:**
SQL Error: ORA-02291: integrity constraint (PRAVE.fk_tab2_id) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
**Solution:**
**Insert into the table with PK first as below:**
insert into tab1 values(1,'abc')
1 rows inserted.
insert into tab2 values(1,'abc')
1 rows inserted.

using self referencing in sql server

create table EMP(Eid int primary key)
insert into EMP values(11e3)
--self referencing
alter table EMP
add constraint fk_EMP_Eid
foreign key (Eid) references EMP(Eid)
--now insert
insert into EMP values(12e2)
But, this insert should fail, because there is no previous value of Eid=1200 in the EMP table, so when the foreign key will reference this column , then it would not find the value, hence should fail the insert .
but why does it succeeds?
The column references itself.
So the addition of the row itself guarantees that there is a matching row. This constraint can never fail.
In fact looking at the execution plan SQL Server realises this and doesn't even bother checking it. There is no assert operator present.
If we create a more typical Employee table there are different plans for the inserts that can violate the constraint as below.
create table EMP2(Eid int primary key, boss_id int null);
alter table EMP2 add constraint fk_EMP2_Eid
foreign key (boss_id) references EMP2(Eid)
insert into EMP2 values(1,null) /*Can't violate constraint as NULL*/
insert into EMP2 values(2,1) /*Can violate constraint as NOT NULL*/
If you try multiple rows a blocking spool is added to the plan so the constraints aren't checked until all rows are inserted.
insert into EMP2 values (3,2),(4,3) /*Can violate constraint - multiple rows*/
And just for completeness as it was raised in the comments, looking at the case when the insert is to a table with a FK referencing a different one...
CREATE TABLE EmpSalaryHistory
(
Eid INT NOT NULL REFERENCES EMP(Eid),
EffectiveDate DATETIME NOT NULL,
Salary INT,
PRIMARY KEY (Eid,EffectiveDate)
)
INSERT INTO EmpSalaryHistory
VALUES (1,GETDATE(),50000),
(2,GETDATE(),50000)
In this instance no spool is added to the plan it can check as it inserts each row rather than all at the end so it can rollback earlier in the event that a row fails (the end result will be the same)
Your FK column fk_EMP_Eid probably allows nulls, therefore the relationship isn't required to exist, but if you do try to put a value in that column, then SQL Server will verify that the FK is valid or else it will error.
I created this example for self refernce key for ms sql server
CREATE TABLE Category (
CategoryId int IDENTITY(1,1) not null,
ParentId int null,
CONSTRAINT PK_CategoryId PRIMARY KEY CLUSTERED (CategoryId),
CONSTRAINT FK_ParentId FOREIGN KEY (ParentId) REFERENCES Category(CategoryId),
Title nvarchar(255) NOT NULL
);
insert into category(title)
values
('category1');
insert into category(title,parentid)
values
('category2',1);