Deleting rows based on multiple Joins and where statements in SQL - sql

I have this below SQL statement that I am running on an Oracle database that I'd ideally like to put inside a stored procedure to be called through Java statements. However, I'd like to get the query working first. I have a few tables which have data and my query should satisfy a few conditions through Inner Joins before executing the Delete statement. So, here's the first query I had constructed. I keep getting "SQL command not properly ended" error:
Delete from ur
from ATESTuser_roles ur
Inner Join ATESTresource_roles rr
on ur.role_id = rr.role_id
Inner Join ATESTRESOURCES r
on rr.resource_id=r.RESOURCE_ID
where r.name='TestName'
and ur.user_id = '1401'
I also tried this version and it still dint work. I got a "ATESTRESOURCES"."RESOURCE_ID": invalid identifier error for this:
Delete from ATESTuser_roles
where ATESTuser_roles.role_id = ATESTresource_roles.role_id
and ATESTresources.name='TestName'
and ATESTresource_roles.resource_id=ATESTRESOURCES.RESOURCE_ID
and ATESTuser_roles.user_id = '1401'
I have a feeling I am missing something small but significant, and that I am missing some syntax, so any help is much appreciated.
P.S: I don't know how else to describe the relationship between the tables than the conditions in the query. However, if it is not clear, I can add additional information (don't want to turn this too wordy).
Many thanks!

In Oracle, you can't join multiple tables in a delete like that. One option would be something like
Delete ATESTuser_roles ur
where exists( select 1
from ATESTresource_roles rr
join ATESTRESOURCES r
on rr.resource_id=r.RESOURCE_ID
where ur.role_id = rr.role_id
and r.name='TestName'
and ur.user_id = '1401' )

An alternative solution would be to declare ON DELETE CASCADE in the ATESTuser_roles table. When this is done all you need to do is to run this query:
DELETE FROM ATESTuser_roles
WHERE user_id = '1401'
Because of ON DELETE CASCADE the DB will remove any tuples in ATESTresource_roles where the foreign key (user_id) is equal 1401.
ON DELETE CASCADE example

Related

How to get delete query to work in MS Access

I have set up a subquery to select my records and then the delete query to perform the action. When I run it, I get an error message saying:
Could not delete from specified tables
Here my SQL code in the delete query:
PARAMETERS UnitID Short;
DELETE DISTINCTROW qry_exp_comments_select.*
FROM qry_exp_comments_select;
And the called subquery:
PARAMETERS UnitID Short;
SELECT tbl_Comments.*, tbl_Activity.ActivityID
FROM tbl_BusUnits INNER JOIN (tbl_Activity INNER JOIN tbl_Comments ON tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON tbl_BusUnits.UnitID = tbl_Activity.UnitID
WHERE (((tbl_BusUnits.UnitID)<>[UnitID]));
Why won't the query work? I have tried to set it up as the one in the following thread: MS ACCESS delete query syntax combined with inner join problems
In order to delete all the records in tbl_Comments and tbl_Activity all I had to do was change the relationships of the tables so that they have Cascade Delete activated.
Afterwards a simple delete query on the tbl_BusUnits also deleted all associated records in the other tables.
Code for the Delete Query as follows:
PARAMETERS UnitID Short;
DELETE DISTINCTROW tbl_BusUnits.UnitID
FROM tbl_BusUnits
WHERE (((tbl_BusUnits.UnitID)<>[UnitID]));

delete from 3 tables with one query

i have 3 tables and i dont want define any foreign key in my tables.
my tables structure are like below:
tables diagram
i have written this query :
delete relativedata, crawls, stored
from relativedata inner join
crawls
on relativedata.crawl_id = crawls.id and
relativedata.id = ? inner join
stored
on stored.crawl_id = crawls.id
this query works for me unless one of tables has no records.
now how can i do this delete in 3 tables in 1 query?
If it works if all tables have records, try using LEFT JOIN instread of INNER JOIN. Also, You had some mess with Your joins ON conditions. Try it like this:
delete
relativedata, crawls, stored
from
relativedata
LEFT join crawls on relativedata.crawl_id = crawls.id
LEFT join stored on relativedata.crawl_id = stored.crawl_id
WHERE
relativedata.id = ?
Also, foregin keys are good thing, and not using them is generally bad idea. Yes, they seems to be annoying at first, but try to focus on WHEN they annoy You. Most of the times they do it when You are meddling with data in a way You should not, and without them You wloud cause data incostincency in Your DB.
But, it is just my opinion.

how to use another table\query inside an update query

I tried to make an UPDATE query using data from another table
which in my case was a result of a query and it looks like this:
UPDATE CalculatedQueryINNER JOIN Orders
ON CalculatedQuery.orderid = Orders.OrderID
SET Orders.TotalPrice = [CalculatedQuery].[calculated];
But it's not working, it shows an error must use an updateable query
But i went and copied the results from the query to a table named temp and i did the same and it worked!!
UPDATE temp INNER JOIN Orders
ON temp.orderid = Orders.OrderID
SET Orders.TotalPrice = [temp].[calculated];
Can anyone please provide me with a solution on how to use the query as a table
please?
EDIT: I tried to treat a query as if it was a table by itself, that was my issue, joining tables is not an issue in update queries inaccess
I read a lot through different sites
The problem that I introduced is unsolvable in access.
There is an issue with the update queries since they are not standart in access.
The problem is That in access you can't use another sql query inside an update statement because as i read any queries that use group by or join or select within select etc.. Are marked as unupdateable queries and you can't use them.
As i read i found a few good solutions using a temporary table like i did
Or using dlookup() function.
Keep in mind that you can also perform DELETE and UPDATE statements with a 'FROM', which will help in writing the query in a syntax that is more familiar. Given that you are running two very different update statements above, I've re-written both of them here. Also, given that you're using an Inner Join on both statements, which performs an intersect of data, I've rearranged the tables in the order of update.
UPDATE O
SET [TotalPrice] = [T].[calculated]
FROM [Orders] AS O
INNER JOIN [temp] AS T
ON [T].[orderid] = [O].[OrderID];
UPDATE O
SET [TotalPrice] = [CQ].[calculated]
FROM [Orders] as O
INNER JOIN [CalculatedQuery] as CQ
ON [CQ].[orderid] = [O].[OrderID];
Setting up your Updates (and likewise Deletes) in this syntax is pretty slick, as it allows for another nice feature: running a select to see what data will be changing:
SELECT [O].[TotalPrice]
, [T].[calculated]
FROM [Orders] AS O
INNER JOIN [temp] AS T
ON [T].[orderid] = [O].[OrderID];
SELECT [O].[TotalPrice],
, [CQ].[calculated]
FROM [Orders] as O
INNER JOIN [CalculatedQuery] as CQ
ON [CQ].[orderid] = [O].[OrderID];

Delete s from table

I have a query as following (not the actual one):
DELETE s
FROM
table_expiration s INNER JOIN table_existance d
ON s.ssn = d.ssn AND
s.latest_date = d.latest_date
I don't have any data in those tables so I cannot actually test the query. Can someone explain to me what's the purpose of Delete s? (I always thought the Delete statement should just be Delete from table)
delete s tells the query to delete the rows from table_expiration which has the alias s. Replacing s with d would delete rows from table_existance.
SQLFiddle
It may be worth pointing out here that you cannot delete from both tables involved in the join directly by doing delete s,d in SQL Server (MySQL lets you do that I think).

Difference between DELETE and DELETE FROM in SQL?

Is there one? I am researching some stored procedures, and in one place I found the following line:
DELETE BI_Appointments
WHERE VisitType != (
SELECT TOP 1 CheckupType
FROM BI_Settings
WHERE DoctorName = #DoctorName)
Would that do the same thing as:
DELETE FROM BI_Appointments
WHERE VisitType != (
SELECT TOP 1 CheckupType
FROM BI_Settings
WHERE DoctorName = #DoctorName)
Or is it a syntax error, or something entirely different?
Assuming this is T-SQL or MS SQL Server, there is no difference and the statements are identical. The first FROM keyword is syntactically optional in a DELETE statement.
http://technet.microsoft.com/en-us/library/ms189835.aspx
The keyword is optional for two reasons.
First, the standard requires the FROM keyword in the clause, so it would have to be there for standards compliance.
Second, although the keyword is redundant, that's probably not why it's optional. I believe that it's because SQL Server allows you to specify a JOIN in the DELETE statement, and making the first FROM mandatory makes it awkward.
For example, here's a normal delete:
DELETE FROM Employee WHERE ID = #value
And that can be shortened to:
DELETE Employee WHERE ID = #value
And SQL Server allows you to delete based on another table with a JOIN:
DELETE Employee
FROM Employee
JOIN Site
ON Employee.SiteID = Site.ID
WHERE Site.Status = 'Closed'
If the first FROM keyword were not optional, the query above would need to look like this:
DELETE FROM Employee
FROM Employee
JOIN Site
ON Employee.SiteID = Site.ID
WHERE Site.Status = 'Closed'
This above query is perfectly valid and does execute, but it's a very awkward query to read. It's hard to tell that it's a single query. It looks like two got mashed together because of the "duplicate" FROM clauses.
Side note: Your example subqueries are potentially non-deterministic since there is no ORDER BY clause.
Hi friends there is no difference between delete and delete from in oracle database it is optional, but this is standard to write code like this
DELETE FROM table [ WHERE condition ]
this is sql-92 standard. always develop your code in the standard way.