Delete records based on another table
Hi allI have two tables called table1 and table2. The two tables are joined with fk. tab2_id I need now to remove all records of two table where lic_no is null in table2.
Table structure:
Table1:
tab1_Id
tab2_id
CNAME
address
contact
Table2
tab2_id
lic_no
issue_date
fees
Comments
I'm trying like this -
delete from table1 as some tab2_id of t table2 where tab2_id! = t.tab2_idbut I get the error message.
I also tried to delete table2 first records and then delete an array like this -
delete from table1 where tab2_id! = (select tab2_id from table2)get the error "subquery returns multiple rows.
Hope in this sense.
I don't know what I'm doing wrong. Please suggest.
Thanks in advance.
Kind regards
Pascal M
It will work
DELETE from table1
WHERE tab2_id in ( select tab2_id
from table2
where lic_no is null ) ;
DELETE from table2 where lic_no is null ;
Note: Disable constraints between the tables if you have a
I think this is what your need.
SS
Tags: Database
Similar Questions
-
best way to create a table based on another table
Hello
I am trying to create a table based on another table with all the data in it. It contains important data.
create table < tablename > select * from table1.
Is the best way to do it, or is there another way. Please advice.
Thank youInsert / * + append * / in
as select * from ; It should be->
insert /*+ append */ into
select * from ; Kind regards.
LOULOU.
-
Delete records based on the foreign keys of oracle 11 g
I have a requirement to remove the records from the tables in the order according to
the existing foreign keys.
I for example, the following tables and the pk, fk constraints:
create table one
(aa number (1),)
descr varchar2 (20));
ALTER table one
Add constraint a_pk key (aa) primary;
create table b
(aa number (1),)
descr varchar2 (20));
ALTER table b
Add constraint b_pk key (aa) primary;
create table c
(aa number (1),)
descr varchar2 (20));
ALTER table c
Add constraint c_pk key (aa) primary;
create table a2
(aa number (2),)
id_aa number (1).
descr varchar2 (20));
ALTER table a2
Add constraint a2_pk key (aa) primary;
ALTER table a2
Add constraint a2_fk foreign key (id_aa)
references a (aa);
create table b2
(aa number (2),)
id_aa number (1).
descr varchar2 (20));
ALTER table b2
Add constraint b2_pk key (aa) primary;
ALTER table b2
Add constraint b2_fk foreign key (id_aa)
references b (aa);
create table z
(aa number (3),)
id_aa number (1).
id_bb number (1).
descr varchar (20));
ALTER table z
Add constraint z_pk key (aa) primary;
ALTER table z
Add constraint z_fk1 foreign key (id_aa)
references a (aa);
ALTER table z
Add constraint z_fk2 foreign key (id_bb)
references b (aa);
So, I want to choose the names of the tables in such an order so as
deleting records will succeed...
I built the following sql query (using the recursive subquery factoring):
[p]
with q (r_constraint_name, table_name, constraint_name, lvl) as
(select table_name, constraint_name, r_constraint_name 1 lvl
from user_constraints one
where a.constraint_type = 'P '.
Union of all the
Select b.table_name b.constraint_name, b.r_constraint_name, q.lvl + 1 lvl
from user_constraints b
Join q
on (q.constraint_name = b.r_constraint_name)
where b.constraint_type = 'R '.
)
Select f.table_name, f.constraint_name, f.r_constraint_name, f.lvl
q f
[/ p]
I want the results as the following list:
Table-name
----------------
B2
A2
Z
A
B
C
The table - B2, A2, Z - (in any order) must first referred in the list
because they are based on the other three tables - A, B, C. Thus, in order to remove the
A, B, C table records the B2, A2, table Z records must be beleted first.
The query I posted above has the problem that it displays tables A2, B2 twice
(1 because they have a pk and 2 because they have fk referring A, B relatively tables).
Is there a solution for this problem?
Note: I use db11g v2
I wrote not all relationships of tables user_constraints (only argument constraint_name = r_constraint_name)
Thank you
SIM
I have a requirement to remove the records from the tables in the order according to
the existing foreign keys.
. . .
The table - B2, A2, Z - (in any order) must first referred in the list
because they are based on the other three tables - A, B, C. Thus, in order to remove the
A, B, C table records the B2, A2, table Z records must be beleted first.
Is there a solution for this problem?
Yes - the 'solution' is to use ON DELETE CASCADE, as appropriate or write a procedure that removes tables in the proper order.
The solution is NOT to try to use dynamic sql statements to do so.
You already know the good parent/child relationships. Simply create a procedure that uses the correct order.
Analyses are necessary in any case to determine the proper order AND press the appropriate values to use to remove the appropriate lines
Your DDL for tables and constraints must be in a version control system
Update the procedure when / if new constraints or tables are added to the application
IMHO, you must use dynamic SQL NOT to try to adjust automatically if a new constraint appears. New constraints should not appear by accident - they appear ONLY as part of a well planned release.
-
delete records based on sysdate
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE Production 11.1.0.7.0
AMT for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
Table:
SQL > incoming_orders desc;
Name Null? Type
----------------------------------------- -------- -----------------
NOT COUNT NULL VARCHAR2 (5)
SUBACCOUNT NOT NULL VARCHAR2 (1)
NOMEN NOT NULL VARCHAR2 (28)
CHG_NBR NOT NULL VARCHAR2 (3)
ORDER_QTY NOT NULL NUMBER (38)
UI VARCHAR2 (5)
ZIP VARCHAR2 (5)
ORDER_DATE DATE
W2056 widget1 0 35 006 EA 46124 4 May 10
W2530 widget2 0 000 3000 TG 73503 4 May 10
My goal on the sql and pl/sql is to delete records more of 9 days. I just wanted to start by deleting records with today's date. But it does not work with the code I used:
SQL > select * from incoming_orders where to_char (order_date) = sysdate;
no selected line
or
DECLARE
v_orders_rec incoming_orders % ROWTYPE;
BEGIN
REMOVE FROM INCOMING_ORDERS
WHERE ORDER_DATE = SYSDATE
END;
My goal is to try to delete records of todays on the road anything over 9 days. Appreciate the assistance.
It's fun... but what I will meet these dam little... :) Thank you.Select * from incoming_orders where to_char (order_date)< trunc(sysdate="" -="" 9="">
not above but below
Select * from incoming_orders where order_date< trunc(sysdate="" -="" 9=""> -
How can I delete records of a 3D table based on values in a field?
In the attached table 3D I want to delete all the records that have no 3A 900000 / 3 has 900001/or 3A 900002 in field 2. Is there an easy way to do this?
Thank you.
Instead of delete why didn't you find and replace rather--much more effective anyway due to no resizing of the table. Consider this in your subsequent treatment table.
-
Deleting record based on the ID of the main table
Hello
I am trying to remove the news that are published before 2012, but the tables in detail consists the notebook of the child based on the news_id has been. Request certainly simple deletion will not workSQL> desc news Name Null? Type ----------------------------------------- -------- ---------------------------- NEWS_ID NOT NULL NUMBER(14) NEWS_DATE TIMESTAMP(0) SL_ID NUMBER(2) HEADING VARCHAR2(3120) DESCRIPTION VARCHAR2(3900) SQL> desc news_location Name Null? Type ----------------------------------------- -------- ---------------------------- NEWS_ID NUMBER(14) COUNTRY VARCHAR2(32) REGION NUMBER(2) SQL> desc news_product Name Null? Type ----------------------------------------- -------- ---------------------------- NEWS_ID NUMBER(14) PRODUCT_CATEGORY_ID NOT NULL NUMBER(14) PRODUCT VARCHAR2(27) SQL> desc news_service Name Null? Type ----------------------------------------- -------- ---------------------------- NEWS_ID NUMBER(14) SERVICE_ID NOT NULL NUMBER(14) SRVIS VARCHAR2(16) SQL> desc news_info Name Null? Type ----------------------------------------- -------- ---------------------------- NEWS_ID NUMBER(14) SOURCE VARCHAR2(203) ORIGIONAL_NEWS VARCHAR2(3900) HEADING VARCHAR2(3110) SQL> select count(*) from news where TO_CHAR(news_date,'YYYY') < 2012; COUNT(*) ---------- 8759
Please advise and thank you in advanceOnce again, I tell you, do not need ON DELETE CASCADE in the Delete query.
Just use the Delete query like: -.
delete from news where TO_CHAR(news_date,'YYYY') < 2008;
And the child tables are deleted as a result.
-
How to identify records in a table for which no record exists in another table?
Hello
I have a table named CARE:
Name of Type NULL
---------------------------- -------- --------------
FICHE_ID NOT NULL NUMBER
AGENT_ID NUMBER
FICHE_ID is the main KEY to the PLUG
There is a table ACTIVITE_FAITE:
Name NULL Type
----------------- -------- -----------
FICHE_ID NOT NULL NUMBER
ACTIVITES_ID NOT NULL NUMBER
NUMBER OF SECTEURS_ID
NOT NULL NUMBER DURATION (8.2)
ACTIVITE_FAITE_ID NOT NULL NUMBER
NUMBER OF TYPE_ACTIVITE_ID
FICHE_ID is a key for both tables dower (it references FICHE_ID. of the table INSERT)
I just want to display the records of CARE for which there is no reference in the ACTIVITE_FAITE table. In other words, all the documents of record for records that have been created in ACTIVITE_FAITE.
I tried without success:
Select * from sheet f
If f.fiche_id not in
(by selecting af.fiche_id in Activite_faite af)
Thank you very much for your help!
Select * from sheet f
where fiche_id not in (select fiche_id from activite_faite)
is a way (this works because fiche_id is not nullable)
where there is not (select null from activite_faite where f.fiche_id = a.fiche_id)
is another way.
-
Deleting records in an Oracle table
I have a table of RESULTS and want to delete the records does not correspond to the account and IDS on the table 2.
This is the request I have formed-
delete results p
where (p.account, p.match_value) not in
(select the id from 2 account)
However, NOT IN the clause seems to take too long to run, so I can change the query like this?
delete results p
Left outer join the 2 pd
on p.account = pd.account
and p.match_value = pd.id;
Please suggest if it seems good.
NOT IN often has performance issues. Also if there is no line in table 2 with null or id account it will not work as it should in any case. Try this. "
delete from results where (account, match_value) in
(select the account, the results match_value
less
Select id from 2 account);
-
How one table based on another table on average?
Example:
I have the ARRAY1 array with the following:
1
1
1
2
2
3
Array2 contains:
1
2
3
4
5
6
In the end, ARRAY3 would contain:
2 (1+2+3)/3
4.5 (4+5)/2
6 (6)/1
This is my first go at it. There are a lot of improvements that can be made (conditional index is one) but it seems to do what you want. Table 1 and 2 must be the same size to make it work.
-
Possibility to delete records in two different table in the same DB
Regarding the title of the Message, if it is possible, please show me the SQL (using MS SQL 2000) command to perform this operation. Thank you very much.clixmax wrote:
> Regarding the title of the Message, if it is possible, please show me the SQL code
> command (using MS SQL 2000) to perform this operation. Thank you very much.SET NOCOUNT ON
REMOVE tableOne
WHERE someCondition = #form.someCondition #.REMOVE tableTwo
WHERE someCondition = #form.someCondition #.SET NOCOUNT OFF
-
Delete based on another column dublicate records
How can I remove duplicate records, based on another column.
the table structure
CREATE TABLE IA_EXPORT_LEVEL
(
A_NO VARCHAR2 (22 BYTE),
IM VARCHAR2 (18 BYTE),
ctn_level NUMBER (2)
)
reviews
A_NO IM ctn_level
S1 S1 1
S1 M1 2
S1 h1 3
H2 S2 1
S2 h2 2
S3 h1 2
S3 h1 3
S4 h6 4
S4 h6 5
I want to delete all records other than the minimum value of the ctn_level
output should like below
S1 S1 1
H2 S2 1
S3 h1 2
S4 h6 4
Published by: OraFighter on December 16, 2011 13:44DELETE FROM IA_EXPORT_LEVEL where rowid IN (select rid from (select rowid rid, row_number() over (partition by A_NO,IM order by ctn_level) rn from IA_EXPORT_LEVEL ) where rn > 1 ) /
Hope your question.
-
How to restore deleted records in another table in the oracle 10g database...
Hi all
I want to restore deleted records in a particular table in the other table
Suppose that:
I make a request
delete from emp
where deptno = 30;
now, I won't restore deptno = 30 records in the other table, let's say table emp1
can someone let me know how to do it?
Thank you...
This is what flashback query is for:
orclz > conn scott/tiger
Connected.
orclz > select count (*) emp;
COUNT (*)
----------
14
orclz > delete from emp where deptno = 30;
6 deleted rows.
orclz > commit;
Validation complete.
orclz > create table deleted30 in select * from emp to the timestamp (systimestamp - 5/1440) where deptno = 30;
Table created.
orclz > select count (*) in the deleted30;
COUNT (*)
----------
6
orclz >
-
Update of the table based on another
Hello
I'm trying to update a date column in a table based on another table. There is a unique ID in each table that identifies and connects each person:
This is the error:update patient_bu a set a.entry_date = (select b.entry_date from CLIENT_MED_DT b where a.id = b.id)
ORA-01427: einreihig subquery returns multiple rows
The problem (I think) is that the select statement back several dates for records people. There are some people / records that have multiple entry dates, but I'm only interested in the most recent date.
How can I update this table with only the most recent date?
Thanks for any help,
MattOh well, I forgot the UPDATE clause:
SQL> create table patient_bu 2 (id number 3 ,entry_date date); Table created. SQL> create table client_met_dt 2 (id number 3 ,entry_date date); Table created. SQL> merge into patient_bu a 2 using (select id 3 ,max(entry_date) max_date 4 from client_met_dt 5 group by id 6 ) b 7 on (a.id = b.id) 8 when matched then update 9 set a.entry_date = b.max_date; 0 rows merged.
-
The use of FORMS_DDL to upadate another table
Hi all
I want to update a record in a table by using the statement of forms_ddl against a button to a form that is based on another table. I used the following to do this but no updates or error free code
occered-
COMMIT_FORM;
IF: CASE_STATUS. WRIT_OFF = 1 THEN
FORMS_DDL ('UPDATE CASE_INFO SET WRIT_OFF = 1 WHERE CASE_NO =' |: CASE_STATUS.) CASE_NO);
ON THE OTHER
FORMS_DDL ('UPDATE CASE_INFO SET WRIT_OFF = 0 WHEN CASE_NO =' |: CASE_STATUS.) CASE_NO);
END IF;
COMMIT_FORM;
What is wrong with my code. Please help me find the problem. I use form 10g.
ArifCOMMIT_FORM; IF :CASE_STATUS.WRIT_OFF=1 THEN UPDATE CASE_INFO SET WRIT_OFF=1 where CASE_NO= :CASE_STATUS.CASE_NO; ELSE UPDATE CASE_INFO SET WRIT_OFF=0 where CASE_NO=:CASE_STATUS.CASE_NO; END IF; COMMIT;
Forms_ddl builtin is used to launch the ddl through forms. No need to use it for dml.
-
change the order of a table with another table
I want to change the order of a 1 d table based on another table 1 d that specifies the order. For example, I have a table including the following items: 12, 13, 14, 15, 16. And I have another 1 d array that specifies the order of 3, 4, 0, 1, 2. I want the table resulting in 15, 16, 12, 13, 14. How can I do this? It must be really easy. Another example, I give myself a table in the order following 1,2,3,4,5,6,7 and I an order specifying the array containing the 0,6,1,5,2,3,4. Therefore, the resulting table must be 1,7,2,6,3,4,5. ???
It is, in fact, quite easy. Put an "Array Index" inside a loop and thread the two tables in it. Disabled automatic indexing on the input array. Wire of the array element located on the border of the for loop, and you'll have the desired table.
Maybe you are looking for
-
make a backup of recovery on external hard drive
Hi allI want to restore my hp envy 15j063 and I want to do it on a flash drive. My flash drive is a sandisk who is diagnosed as a hard disk partition. Is it possible that I can use this disc as a destination of recovery?Thank youAli
-
I got the blue screen after update
Hello. I got a bluescreen error after update my entire system today. He wrote: Signature of the problem:Problem event name: BlueScreenThe system version: 6.0.6002.2.2.0.768.3Locale ID: 1030 More information about the problem:BCCode: 116BCP1: 85F5C510
-
Canon provides a driver to use for the installation of an ink jet Canon i560 printer under windows 7 x 64. Windows7 has however no provision for detecting or using this driver. Even when the location of the inf file is entered according to the sequ
-
Poblem with Windows Anytime Upgade. Windows 7 Starter to Home Premium
Hello I have problems with the installation of the Windows Anytime Upgrade on my Acer Netbook. Windows 7 Starter to Home Premium. I put in my product key, and it seems to have been accepted. After about 10 minutes of downloading, the message reads
-
Where is generator of folio in adobe indesign cc2015?
I just downloaded the application InDesign CC2015 to a new PC. I worked on a portfolio in Adobe Publishing Suite that I went to day today - I can open the file in InDesign, but I can't find the Folio Builder option in the window menu. Does anyone kno