Deletion of Parent Record
Hi!
I want to delete parent record, but error comes that Child record present.
Is any way to delete Parent Record.
Regards!
Maybe you might want to do away with the Foreign Key constraint altogether? What is the purpose of having a parent-child relationship if the child row can exist without any changes even if the parent row is deleted? The closest option available with ON DELETE is SET NULL, where the child rows will be set to NULL if the parent row is deleted, however I don't think this is what you are trying to achieve. If you want a child row to exist even if the parent is deleted, either disable or drop the constraint, for it makes no real sense to have it anyway.
Similar Messages
-
Delete the parent records and child table records at a time
hi all;
I am facing the pbm like to delete the all records in child table and corresponding records in parent table at a time. so I want to delete the all the records in child table and corresponding parent records in parent table by using single SQL query. plz help me
Thanks in advanceYou want to use one single SQL statement to delete the child records in a table and the corresponding master records in the master table??
That's not quite possible with a single SQL, of course unless you are talking about Oracle Forms, where you have a relation and set the delete behavior to Cascading, like said in the above posts.
Tony -
hi all,
based on the thread i got the list of child tables.
Re: to find the tables which is having link with particular table.
no i have doubt in deletion process.
My ultimate aim is to delete the bl_det records.
let me explain with ex.
i have table
BL_DET --parent table with comp and ref as primary key
which is having child rec.
BL_CON --child of BL_DET(comp,ref) primary key comp,ref and serial
BL_CON_PCK --is child of BL_CON(comp,ref,serial) primary key comp,ref,serial,pck_gp_srl,gp_srl.
DELETE BL_CON_PCK
WHERE BCK_COMP = :GLOBAL.comp
AND BCK_REF = :parameter;
DELETE FROM BL_CON
WHERE BCR_COMP = :GLOBAL.comp
AND BCR_REF = :paramter
DELETE FROM BL_DET
WHERE BL_COMP = :GLOBAL.comp
AND BL_REF - :parameterwill delete the correct set of records.(my doubt area is delete statement written for the table BL_CON_PCK).
Please tell me if i am wrong.
Thanks..(my doubt area is delete statement written for the table BL_CON_PCK).Cannot understand your doubt. What is your doubt, are you getting any error while executing your code?
To delete a record from parent table all child references must be deleted. So your delete statement appears right
-- How delete should progress
BL_CON_PCK >> BL_CON >> BL_DET
{code}
If you have created foreign constraint using ON DELETE CASCADE or ON DELETE SET NULL then you can straight away delete from parent table and all child references are deleted/set as null respectively. Read Oracle Documentation to see details
Short demonstration
{code:sql}
SQL> CREATE TABLE A(a1 NUMBER PRIMARY KEY , a2 NUMBER);
Table created
SQL> CREATE TABLE B(b1 NUMBER PRIMARY KEY , a1 NUMBER);
Table created
SQL> CREATE TABLE C(c1 NUMBER, b1 NUMBER);
Table created
SQL> ALTER TABLE B ADD CONSTRAINT FK_B FOREIGN KEY (a1) REFERENCES A ON DELETE CASCADE;
Table altered
SQL> ALTER TABLE C ADD CONSTRAINT FK_C FOREIGN KEY (b1) REFERENCES B ON DELETE CASCADE;
Table altered
SQL> INSERT INTO A VALUES (1,2);
1 row inserted
SQL> INSERT INTO B VALUES (3,1);
1 row inserted
SQL> INSERT INTO C VALUES (4,3);
1 row inserted
SQL> SELECT * FROM A;
A1 A2
1 2
SQL> SELECT * FROM B;
B1 A1
3 1
SQL> SELECT * FROM C;
C1 B1
4 3
SQL> DELETE FROM A WHERE A1=1;
1 row deleted
SQL> SELECT * FROM A;
A1 A2
SQL> SELECT * FROM B;
B1 A1
SQL> SELECT * FROM C;
C1 B1
{code} -
Can't Delete Parent Record - On Delete Cascade is set
Greetings all,
I have a parent and child table and when I attempt to delete the parent record, I get the following error:
ORA-02292: integrity constraint (WFSTEADMAN.INCIDENT_NOTES_TBL_FK) violated - child record found
Error Unable to process row of table MYTEST_INCIDENTS.
I have the the FK set to ON DELETE CASCADE so I am not sure what the issue might be.
Below are the structures of my parent and child tables:
I am using apex.oracle.com so the version is: Application Express 4.0.2.00.06
Any assistance would be appreciated.
Wally
PARENT TABLE:
=============================================================
CREATE TABLE "MYTEST_INCIDENTS"
( "INCIDENTID" NUMBER NOT NULL ENABLE,
"INCIDENTCREATED" DATE NOT NULL ENABLE,
"INCIDENT_OWNER" VARCHAR2(200) NOT NULL ENABLE,
"DTGSTART" DATE NOT NULL ENABLE,
"DTGEND" DATE,
"INCIDENT_SERVICES" VARCHAR2(4000),
"INCIDENT_CUSTOMERS" VARCHAR2(4000),
"INCIDENT_DESCRIPTION" VARCHAR2(4000),
"INCIDENT_TIMELINE" VARCHAR2(4000),
"INCIDENT_TRACKING" VARCHAR2(500),
"INCIDENT_RESTORE" VARCHAR2(4000),
"INCIDENT_FOLLOWUP" VARCHAR2(4000),
"INCIDENT_LEVEL" NUMBER,
"INCIDENT_TITLE" VARCHAR2(500),
"EMAIL_NOTIFY" VARCHAR2(4000),
"TEXT_NOTIFY" VARCHAR2(4000),
"LEVEL_TYPE" VARCHAR2(25),
CONSTRAINT "MYTEST_INCIDENTS_PK" PRIMARY KEY ("INCIDENTID") ENABLE
CREATE OR REPLACE TRIGGER "BI_MYTEST_INCIDENTS"
before insert on "MYTEST_INCIDENTS"
for each row
begin
if :NEW."INCIDENTID" is null then
select "MYTEST_INCIDENTS_SEQ".nextval into :NEW."INCIDENTID" from dual;
end if;
end;
ALTER TRIGGER "BI_MYTEST_INCIDENTS" ENABLE;
CHILD TABLE
=============================================================
CREATE TABLE "MYTEST_INC_NOTES"
( "NOTEID" NUMBER NOT NULL ENABLE,
"INCIDENT_ID" NUMBER NOT NULL ENABLE,
"NOTE_DTG" DATE,
"NOTE_OWNER" VARCHAR2(200),
"NOTE_COMMENTS" VARCHAR2(4000),
CONSTRAINT "MYTEST_INC_NOTES_PK" PRIMARY KEY ("NOTEID") ENABLE
) ;ALTER TABLE "MYTEST_INC_NOTES" ADD CONSTRAINT "MYTEST_INC_NOTES_CON" FOREIGN KEY ("INCIDENT_ID")
REFERENCES "MYTEST_INCIDENTS" ("INCIDENTID") ON DELETE CASCADE ENABLE;
CREATE OR REPLACE TRIGGER "BI_MYTEST_INC_NOTES"
before insert on "MYTEST_INC_NOTES"
for each row
begin
if :NEW."NOTEID" is null then
select "MYTEST_INC_NOTES_SEQ".nextval into :NEW."NOTEID" from dual;
end if;
end;
ALTER TRIGGER "BI_MYTEST_INC_NOTES" ENABLE;
CREATE OR REPLACE TRIGGER "MYTEST_INC_NOTES_T1"
BEFORE
insert or update on "MYTEST_INC_NOTES" REFERENCING NEW AS NEW OLD AS OLD
for each row
begin
:NEW.NOTE_DTG := SYSDATE;
end;
ALTER TRIGGER "MYTEST_INC_NOTES_T1" ENABLE;Yeah there was a rampant child table still left out there. Noticed the issue and deleted the child and all is well now. :). Should have looked for a couple more minutes before posting.
Thanks
Wally -
Hi,
Is it possible to delete parent records from parent table without deleting from child tables ?
I can delete the records after disabling the constraints . But I couldn't enable the constraint on child table after deletion of parent records from parent table.
Please Help.Two Alternatives
1. What is the need of child records if no parent record ? No use keeping the child records without parent records.
For e.g., If we have Invoice_Master containing the Distributor and total amount and Other Transport details and Invoice_Details containing the list of Items the Distributor has purchased. In this case if we delete the Invoice_Master then the Invoice_Details is meaning less.
2. Another case If really need to maintain the Child records then create one dummy record in the Parent and link the Child records to the dummy Parent by changing the Foreign Key value to the dummy Parent record Primary Key Value. -
ADF,how to delete parent record and related child record without manual cod
Hi All,
I'm using 11g adf.
I have one parent table PAR and two child table CHD1 , CHD2 respectively.
I'm inserting values in three tables , making a form having add , delete and edit buttons.
Issue when i want to delete a record from PAR table , it gives child table record exists . i have did manual coding to delete the child records with related to the selected parent table PAR.
Is there any process in ADF to delete the child records with respective selected parent record with out manual coding.
thanks in advance.http://download.oracle.com/docs/cd/E14571_01/web.1111/b31974/bcentities.htm#BABHFJFJ
John -
I want to delete parent record so that its child is automatically deleted
Hi Gurus , i was trying to delete the child record as soon as parent is deleted.
How do we do that?
I can delete child 1st but it seems that there are lots of child data in different table.
I have heard 'on delete cascade ' does that.
Can you please help me out how to use that in delete statement???You should change your relationship to on delete = cascade. For example:
alter table tb2 drop constraint fk;
alter table tb2 add constraint fk foreign key (n) references tb1(n) on delete cascade;
When you delete father table, will be deleted all children that have relationship like delete cascade. -
Delete child records before I can delete the parent
Hello,I'm a newbie to SQL and if it is not to difficult I can succeed to write querie's. I don't much know about PL/SQL.
We are working with Oracle.The parent ERD is this.
Table 1 --> linking Table --> Table 2 --> Table 3(detail)
1 | 1 1 1 1 1 n
|--> Table 1 detail
1
Now I want to delete for every record of table 1 who doesn't have a record in the table 1 detail.
But before I can do this:
- I have to delete the one or more records in table 3,
- then in table 2 and then the records in the linking table
- and at last the records from Table 1, which is the parent.
Can anybody help or put me in a direction how to solve this?
Cascade doesn't work I think.
I mean by 'Linking table' a table with only three UID's(Table 1, Table 3 and his self)
I mean by the numbers the relation between the tables.
I'm working with Toad
Thanks in advance,
Nicoif I am understanding you correctly you have 3 tables kind of like this
create table table1 as (select 1 t1_id from dual union
select 2 from dual union
select 3 from dual);
create table table2 as (select 1 t2_id, 1 t1_id from dual union
select 2 t2_id, 1 t1_id from dual union
select 3 t2_id, 2 t1_id from dual union
select 3 t2_id, 2 t1_id from dual);
create table table3 as (select 1 t3_id, 1 t2_id from dual union
select 2 t3_id, 1 t2_id from dual union
select 3 t3_id, 2 t2_id from dual);
select t1_id, t2_id, t3_id
from table1 left outer join table2 using (t1_id) left outer join table3 using (t2_id)
order by t1_id;
T1_ID T2_ID T3_ID
1 1 1
1 1 2
1 2 3
2 3
3 so you would like to delete table 1 t1_id 2 and 3 because there are no corresponding entris in table 3 (the detail table)
delete from table1 where t1_id in
select t1_id
from table1 left outer join table2 using (t1_id) left outer join table3 using (t2_id)
where t3_id is null
select * from table1;
t1_id
1of course that leaves you with orphans so you will need to clean up table 2
delete from table2 where t1_id not in
(select t1_id from table1);why won't a cascade delete work?
Edited by: pollywog on Nov 18, 2010 12:03 PM -
How to find child records is exists before to delete parent records.
Dear Everyone,
I would like to ask you, is there any simple way to find child records is exists before to delete parent records.
I have a master table defined primary key and I have referenced that as foreign key in many tables.
I have used Delete_Record to delete but it isn't display the message 'child record exist' but it cleared the record from the form. So I need to know how to find child records is exists or not before to do delete_record so that I can display message in-case it exists.
I do know we can able to check in every table matching the column manually, but what if we use 100 of tables then it is not possible to check manually.
I am sure there must be simple way to find as how the oracle identifies when we execute delete query.
Please help me on this.
Thanks in advance.Simple solution is to run the query I have given for all child tables or to try a single query for all child tables withj something like:
select <parent_key>
from <parent_table>
where <parent_key> in
(select <foreign_key_1> from <child_table_1> union
select <foreign_key_2> from <child_table_2> union
... );More complicate solution could be to change the foreign key constraints to cascade DELETE on parent table to child tables:
SQL>
SQL> create table p(x int primary key);
Table created.
SQL> create table c1(x1 int);
Table created.
SQL> create table c2(x2 int);
Table created.
SQL>
SQL> alter table c1 add constraint fk1 foreign key(x1) references p;
Table altered.
SQL> alter table c2 add constraint fk2 foreign key (x2) references p;
Table altered.
SQL>
SQL>
SQL> insert into p values(1);
1 row created.
SQL> insert into c1 values(1);
1 row created.
SQL> insert into c2 values(1);
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> alter table c1 drop constraint fk1;
Table altered.
SQL> alter table c2 drop constraint fk2;
Table altered.
SQL>
SQL> alter table c1 add constraint fk1 foreign key(x1) references p on delete cascade;
Table altered.
SQL> alter table c2 add constraint fk2 foreign key (x2) references p on delete cascade;
Table altered.
SQL>
SQL> delete p where x=1;
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from p;
no rows selected
SQL> select * from c1;
no rows selected
SQL> select * from c2;
no rows selected
SQL>Edited by: P. Forstmann on 27 oct. 2011 14:01 -
Query to delete both parent and child record....
hai.........
I tried to delete a record from the table.... but i get a error saying 'child record' exist cannot delete record'.... can u plz tell me the query to delete both parent and child record....
plz help me.....
anoo...Hello,
Is already answered in {thread:id=824593}. Please mark the question as answered.
Greetings,
Roel
http://roelhartman.blogspot.com/
http://www.bloggingaboutoracle.org/
http://www.logica.com/ -
Cdc- deletion of parent- child records
Hi,
I am working with CDC-consistent feature in odi.
Here my scenario is, I have a record say 120 (primary key) in table A(parent source table) and it is used as a foreign key in Table B.
both child and parent are inserted into the concerned tables of target.
Now i want to delete this 120 record from target parent and child tables.
IN the pkg i arranged the pkg scenarios as follows
odiwaitforlogdata----->source model(with extenwindow and lock subscriber option selected)------>parent pkg scenario------>child pkg scenario----->source mode(with unlock subscriber and purge journal options). ------------> This works fine for insert and update.
odiwaitforlogdata----->source model(with extenwindow and lock subscriber option selected)------>child pkg scenario--------->parent pkg scenario----->source mode(with unlock subscriber and purge journal options). ------------> This works fine for delete.
Can't I achieve these two in one pkg
Please Guide.
Regards,
Chaitanya.Hi,
kev374 wrote:
Thanks, one question...
I did a test and it seems the child rows have to also satisfy the parent row's where clause, take this example:
EVENT_ID|PARENT_EVENT_ID|CREATED_DATE
2438 | (null) | April 9 2013
2439 | 2438 | April 11 2013
2440 | 2438 | April 11 2013
select * from EVENTS where CREATED_DATE < sysdate - 9
start with EVENT_ID = 2438
connect by PARENT_EVENT_ID = prior EVENT_IDSo you've changed the condition about only wanting roots and their children, and now you want descendants at all levels.
This pulls in record #2438 (per the sysdate - 9 condition) but 2439 and 2440 are not connected. Is there a way to supress the where clause evaluation for the child records? I just want to pull ALL child records associated with the parent and only want to do the date check on the parent.Since the roots (the only rows you want to exclude) have LEVEL=1, you can get the results you requested like this:
WHERE created_date < SYSDATE - 9
OR LEVEL > 1However, since you're not ruling out the grandchildren and great-grandchildren any more, why wouldn't you just say:
SELECT *
FROM events
WHERE created_date < SYSDATE - 9
OR parent_event_id IS NOT NULL;?
CONNECT BY is slow. Don't use it if you don't need it.
If you x-reference my original query:
select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null -- All parents
union
select * from EVENTS where PARENT_EVENT_ID in (select EVENT_ID from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null) -- include any children of parents selected from above
The 2nd select does not apply the created_date < sysdate - 90 on the children but rather pulls in all related children :)Sorry; my mistake. That's what happens when you don't post sample data, and desired results; people can't test their solutions and find mistakes like that. -
Deletion of child records linked to parent records to be deleted etc..
'Combined with WHEN OTHERS, SQLCODE provides a way for you to handle different, specific exceptions without having to use EXCEPTION_INIT pragma. In the next example, I trap two parent child exceptions, -2292 and -2291, and, then take an action appropriate to each situation:
PROCEDURE delete_company (company_id_in IN NUMBER)
IS
BEGIN
DELETE FROM company where company_id = company_id_in;
EXCEPTION
WHEN OTHERS THEN
DECLARE
error_code NUMBER := SQLCODE;
error_msg VARCHAR2(512) := SQLERM;
BEGIN
IF error_code = -2292 THEN
/*Child records found. Delete these too */
DELETE FROM employee WHERE company_id = company_id_in;
/* Now delete parent again */
DELETE FROM company WHERE company_id = company_id_in;
ELSIF error_code = -2291 THEN
/* Parent key not found */
DBMS_OUTPUT_PUT_LINE('Invalid company id' || TO_CHAR(company_id_in));
ELSE
/* This is like a WHEN OTHERS inside a WHEN OTHERS */
DBMS_OUTPUT_PUT_LINE('Error deleting company, error: ' || error_msg
END IF;
END;
END delete_company;
Sourced from Oracle PL/SQL Programming (3rd edition) P.143
1. Are error_code and error_msg assigned automatically or do I have to include assignments? If yes, how?
2. Where do I get a list of commonly caught Oracle error codes and similar code snippets used for business logic?
3. How would you deal with the situation where you detect a parent while you are deleting a child record? Just warn the user and log it hoping the DBA will review the logs?
4. I work within a project where I could not query any parent-child relationships, using, amongst other queries the following query, and, this means that the programming team might not be creating these relationships within the database (Oracle 11g).
What are the technical risks?
Could I be omitting something, because it seems quite incredible to me that parent-child relationships are not in place?
SELECT a.table_name,
a.column_name,
a.constraint_name,
c.owner
FROM ALL_CONS_COLUMNS A, ALL_CONSTRAINTS C
where A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
and a.table_name=:TableName
and C.CONSTRAINT_TYPE = 'R'
Sourced from http://stackoverflow.com/questions/1729996/list-of-foreign-keys-and-the-tables-they-referenceThis is bad code, very very bad code. Throw it away and start with a clear idea of what you want to do.
Why is this code very very bad? Because it uses WHEN OTHERS not followed by a RAISE.
Why is this code bad code? Because it uses one WHEN clause instead of several.
Here is an example for handling multiple exceptions:
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#BABFBHGA
If your error code does not have a predefined name, define the name yourself:
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#BABIIEFC
If you keep the WHEN OTHERS at the end, be sure to add RAISE so the exception still exists. If you don't, Oracle will not do the automatic rollback it should be doing.
To answer your question about parent-child relationships, it is very important to have them. They stop you deleting a parent that has children, but they do not stop you from deleting children. -
How to delete the child record from the database
how to delete a parent and child record from the database can we do it in the servlet and my database is oracle
I'm not sure I understand the question but you could certainly use the JDBC API from within your servlet to access and modify a DB. You could also use an EJB layer to access your DB and accomplish the same tasks.
-
DOE KEYMAP_SERVICE_AV Unable to find parent record for record of node
I HAVE A DO OBJECT WITH 2 LEVELS (HEADER AND 1 CHILD) WHEN I DELETED THE HEADER AND SYNCRONIZE THE ERRO BELOW OCCURS
BACKEND KEYMAP_SERVICE_AV Successful execution CL_ORDEM_INS~DELETE
BACKEND KEYMAP_SERVICE_AV Successful execution
DOE KEYMAP_SERVICE_AV Unable to find parent record for record of node 'OPERACAO_INS'
DOE KEYMAP_SERVICE_AV SYNCKEY_MMW = BE8C84A1DEE142C497BB215A6BF4DE5E MOBILE_ID = COLETOR_ERICO TRANS_ITEM_ID = BE8C84A1DEE142C497BB215A6BF4DE5E IDORDEM_INS = 2C539E9D0848470099033B873FFE0AD3 OPERACAO = 0010 SUBOPERACAO =
WHAT'S WRONG, I SEARCH IN DOE TABLES AND THE PARENT RECORD EXISTS ?
THANKSWhat's the value of :NEW variables after deletion? NULL! What gets inserted into your child table? You guess it... :)
-
Help, I accidentally deleted the parent tab set.
I accidentally deleted the parent tab set from my application, and cannot find anyway to create a new one. Has anyone had this problem?
Oracle - 9.04R2
ApEx - 2.2.0
Browser - Firefox
Thanks,
MichelleHi Scott,
unfortunately I made a stupid thing, being too smart and updating the FLOWS_020200.wwv_SOMETHING_tabs record, modifying parent tab of the problematic tab to main. Doing that caused the complete application to disappear from the Application builder. I guess some checksums in APEX internal objects were modified and that is the result.
Fortunately I had a recent export of an application, so I didn't loose too much. That also means that I can not help myself with "as of" export.
Could you please just tell if recreating parent tabs is possible by "clicking through apex screens" or should there be some hacking used.
Zober
Maybe you are looking for
-
I want to run hardware test on my Power Mac G5 but it isn't working.
What I tried: I have the original install disc 1 which came with the machine in the optical drive (which is the usual one). I printed the hardware test guide which is on the disc, disconnected everything but mouse, keyboard and monitor. I tried to bo
-
My wife and I each have an iPhone 4S. Ever since the upgrade to ios6, text messages sent from my iPhone also appear on hers? All response to those text also show up on both devices. Likewise, text sent by her appear to others as having been sent from
-
1. I have a basic MacBook. 2. I'm running Mac OS X Version 10.6.8. 3. Under Firefox, I have my preference settings set on "When Firefox starts 'show my windows and tabs from last time'." 4. I have privacy settings set to "Tell websites I do not want
-
macbook pro mid 2009 model a1286 bootcamp winxphome sp2 4 gb RAM 2 mb initial size 4008 mb current size 2 mb minimum allowed 4185 mb recommended 2790 mb currently allocated what should i set the virtualmemory / paging file size min/max to? i read on
-
How To add postion specific email address to this query in oracle hrms
Hi friends, I'm using the below query to send an email to the supervisor,employee and the HR manager I was able to add the first two mail address.. for the third one,I mean HR manager... how do I get the email address I want to add the email address