Inner Join Delete in SQL Server 2008? - sql

I am trying to join 2 tables together and do a delete on it.
DELETE TableA
FROM TableA a
INNER JOIN
TableB b on b.Id = a.Id
where title like 'test'
The above is what I come up with however I keep getting
The DELETE statement conflicted with the REFERENCE constraint
I thought if I merge the 2 tables together then I will delete both at the same time and no constraints would be conflicted.
Am I missing something in my query?

try this:
DELETE TableA
FROM TableA
INNER JOIN
TableB b on b.Id = TableA.Id
where TableA.title like 'test'

First try to delete TableB with that title condition
Then delete those records in TableA
DELETE FROM TableB
WHERE Id IN
( SELECT Id FROM TableA WHERE title = 'test')
DELETE FROM TableA
WHERE title = 'test'
Referential Constraints blocks you from deleting rows in TableA when you still have reference in TableB

I would delete one after the other with cascade constraint.

Related

Replace NOT IN with LEFT JOIN to obtain the same result

I've a doubt about the second query in PL/SQL. I've this query:
DELETE FROM tableA
WHERE id NOT IN (SELECT c_id FROM tableB WHERE tableB.c_id = tableA.id)
Is it possible or correct change for something like this?:
DELETE FROM tableA
LEFT JOIN tableB ON tableA.id = tableB.c_id
WHERE id IS NULL
But this show an error: syntax error near at or near "LEFT"
Can anyone help me?
Is it possible or correct change for something like this?:
DELETE FROM tableA
LEFT JOIN tableB ON tableA.id = tableB.c_id
WHERE id IS NULL
No, it is not possible nor correct as Oracle does not support the non-standard DELETE FROM ... JOIN syntax.
You can use:
DELETE FROM tableA a
WHERE NOT EXISTS (SELECT 1 FROM tableB b WHERE b.c_id = a.id);
or:
DELETE FROM tableA
WHERE id NOT IN (SELECT c_id FROM tableB);
MT0's answer is probably what you want. I only want to add that there is a third method that could be useful in more advanced situations:
MERGE INTO tableA
USING (SELECT tableA.rowid AS row_id
FROM tableA
LEFT JOIN tableB ON tableA.id = tableB.c_id
WHERE tableB.c_id IS NULL) src
ON (src.row_id = tableA.rowid)
WHEN MATCHED THEN UPDATE SET tableA.othercol=null -- meaningless change
DELETE WHERE 1=1

How do I update the duplicate values and their foreign key in the child table?

I have 2 tables
Table A
Table B
Table A has 2 columns
ID, CellNo
Now Table B is the CHILD table of A.
In Table B, we have
ID, TableA_CellNO, TableA_ID.
I have duplicate CellNo's in table A and in Table B those two records exist but TableA_ID is updated with the wrong id's.
I need to Update all of the TableA_ID's with the correct values against the duplicate CellNo.
I have written a query to update by matching CellNo in both table but that doesn't work.
UPDATE TableA AS R
INNER JOIN TableB AS P
ON R.CellNo = P.TableA_CellNo
SET P.TableA_ID = R.ID
I'm assuming you want to update table A's Id to table B's latest Id, meaning that the last CellNo record inserted into B is the correct one.
This may not be the most efficient but it should work.
UPDATE a
SET a.Id = b.Id
FROM TableA a
JOIN TableB b
ON b.Id = a.Id
AND b.Id =
(
SELECT MAX(ib.Id) FROM TableB ib WHERE ib.CellNo = a.CellNo
);
You 've a syntax error there. I suppose you 're looking for
UPDATE P
SET P.TableA_ID = R.ID
FROM TableA R
JOIN TableB P ON R.CellNo = P.CellNo;
--WHERE <Some Conditions> if needed

Join equivalent query

I have a simple enough query that will be used as part of a SP to clean the database from time to time. TableA and TableB are related with a ID. I build a table variable with all the IDs in TableA and then delete all the one that appear in TableB. This sintax works just fine:
DELETE #TableIds
FROM #TableIds AS tids
WHERE tids.sharedID IN (SELECT tb.sharedID FROM TableB AS tb WITH (nolock))
How can this query be done with a JOIN instead of a IN (subquery)?
You don't need a table variable to do this. Use delete with a join so the matched id records can be deleted.
delete a
from tablea a
join tableb b on a.id=b.id
Try like below
DELETE Tids
FROM #TableIds AS tids
INNER JOIN TABLEb TB(Nolock) ON TIDS.SHAREID= TB.SHAREId

Delete unmatched records in Access

I have a table in an Access database where records may be referenced from either of two other tables. I need to delete records from that table if they are not referenced by either of the others.
My best solution so far has been to create a subquery to return the id's of the referenced records and to refer to the subquery from a delete query. (The subquery has to be separate because Access does not allow UNION in nested subqueries.)
So ...
SelectQuery:
SELECT TableB.id FROM TableB INNER JOIN TableA ON TableB.id = TableA.id
UNION
SELECT TableC.id FROM TableC INNER JOIN TableA ON TableC.id = TableA.id
DeleteQuery:
DELETE * FROM TableA WHERE id NOT IN (SELECT * FROM SelectQuery)
This is excruciatingly slow ... there must be a better way?
I was trying to avoid having to add a boolean 'Used' field to TableA ...
#Matthew PK suggests using two NOT IN subqueries, which is theoretically a good idea, but as I observed in a comment, NOT IN and NOT EXISTS are poorly optimized by Jet/ACE and will often not use the indexes on both sides of the comparison. I'm wondering whether or not subqueries are necessary or not:
DELETE *
FROM (TableA LEFT JOIN TableB ON TableA.ID = TableB.ID) LEFT JOIN TableC ON TableA.ID = TableC.ID
WHERE TableB.ID Is Null AND TableC.ID Is Null;
This would definitely use your indexes. If a subquery is necessary, you could replace TableB and TableC with the relevant subqueries.
Why not something like this:
DELETE FROM TableA
WHERE
id NOT IN (SELECT id FROM TableB)
AND
id NOT IN (SELECT id FROM TableC)
?
Is it acceptable to create a new table based on your SelectQuery, delete the original table, and rename the new one to the original name?
/* delete more records */
DELETE FROM table1
WHERE NOT EXISTS
(SELECT field FROM table2 WHERE table2.field = table1.field)

SQL Delete based on condition in join

It is possible to delete records based on a satisfied condition with a join query?
For instance, I have a linking table joining 3 records. The query I have at the moment deletes records from this table where one of the id's isn't IN() an imploded Php array. I've come to realise that the query should only remove records from this table if the id's don't exist in the array and they belong to a certain other table based on the a link to another table.
For SQL Server, the command is slightly different:
DELETE FROM TableA
FROM TableA LEFT OUTER JOIN TableB ON TableA.Column = TableB.Column
WHERE TableB.Column IS NULL
No, that's not a typo, yes, you do need "FROM TableA" twice. At least, you need the second FROM (the first is optional). The following has the advantage that it works for both SQL Server and MySQL:
DELETE TableA
FROM TableA LEFT OUTER JOIN TableB ON TableA.Column = TableB.Column
WHERE TableB.Column IS NULL
I like to use EXISTS clauses for this:
DELETE FROM TableA
WHERE
<<put your array condition here>>
AND NOT EXISTS
(SELECT 1 FROM TableB Where TableB.ID=TableA.ID)
You can use :
DELETE Based on a Join:
DELETE A
FROM TableA AS A
LEFT OUTER JOIN TableB As B ON A.Id = B.TabaleAId
WHERE B.Column IS NULL
Delete With SubQuery:
DELETE
FROM TableA AS A
Where
A.id not in ( Select B.TabaleAId From Tab;eB As B )
or
DELETE FROM TableA
WHERE Not EXISTS
(
SELECT *
FROM TableB As B
Where B.TableAId = TableA.Id
)
DELETE Using Table Expressions:
With A
As
(
Select TableA.*
FROM TableA AS A
LEFT OUTER JOIN TableB As B ON A.Id = B.TabaleAId
WHERE B.Column IS NULL
)
Delete From A
DELETE FROM TableA
LEFT OUTER JOIN TableB
WHERE TableB.Column IS NULL
Will delete the records in tableA that don't have a corresponding record in TableB. Is that like what you are after?
DELETE FROM a
FROM TableA AS a LEFT OUTER JOIN TableB AS b
on a.CALENDAR_DATE = b.CALENDAR_DATE AND a.ID = b.ID
Where b.ID is null
You can first use the select statement and verify your records that you want to delete and then remove the select statement and add Delete FROM tablename with the above query syntax.
The easiest way to Delete based on join is as follow:
1.Write your query using SELECT statement instead of DELETE statement
SELECT COLUMNS
FROM Table1
INNER JOIN Table2 ON Table1.YYY = Table2.XXX
2.Replace SELECT COLUMNS with DELETE FROM TABLE
DELETE FROM Table1
FROM Table1
INNER JOIN Table2 ON Table1.YYY = Table2.XXX
Note that we need to specify FROM twice, one for DELETE part and one for JOIN part.
delete from TableA
where id in
(
select id from TableA
except select id from TableB
)
Which means "delete from tableA where id in table a but not in table b)
Otherwise a Merge statement might help you (when matched/not matched delete etc)
http://technet.microsoft.com/en-us/library/bb510625.aspx