Delete s from table - sql

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).

Related

SQL DELETE FROM several tables

I have the following tables:
dataset, links, files
dataset has a field called tiComplete, if it is 0 then the record is incomplete, links and files both have a field "biDataset" that references the record in the dataset table.
I'm trying to create a query that deletes all entries from dataset, links and files where tiComplete = 0, this is what I have:
DELETE
`datasets`.*,
`links`.*,
`files`.*
FROM
`datasets` `d`
INNER JOIN
`links` `l`
ON
`l`.biDataset=`d`.biPK
INNER JOIN
`files` `f`
ON
`f`.biDataset=`d`.biPK
WHERE
`d`.tiComplete=0;
However when I try to save the procedure that contains this I get:
SQL Error(1109): Unknown table `datasets` in MULTI DELETE
I'm using MariaDB version 10 with HeidiSQL version 11.0.0.5919
Your multiple table delete syntax is off. Use this version:
DELETE d, l, f
FROM datasets d
INNER JOIN links l ON l.biDataset = d.biPK
INNER JOIN files f ON f.biDataset = d.biPK
WHERE d.tiComplete = 0;
If you alias the tables, as you have done, then the aliases whose tables are intended for deletion should appear in the DELETE clause as a CSV list.
Note that I removed the ugly backticks everywhere, which weren't necessary and only obfuscate the code. Also, an alternative approach here would be to look into cascading deletion. Using that approach, deletion of a record in the parent table would automatically delete all records in linked children tables.

Why can't I do my SQL delete query do not work am deleting from multiple joined tables?

$sql ="DELETE FROM elimu_advance,elimu_astashahada,elimu_msingi,elimu_secondary,elimu_shahada,elimu_shahada_uzamili,maelezobinafsi,mahali_utokapo,matatizo,michezo_unayocheza,mkataba,mrithi,ujuzi_mwingine,walezi,wazazi,picha WHERE maelezobinafsi.Id=elimu_advance.Id and maelezobinafsi.Id=elimu_msingi.Id and maelezobinafsi.Id=elimu_astashahada.Id and maelezobinafsi.Id=elimu_secondary.Id and maelezobinafsi.Id=elimu_shahada.Id and maelezobinafsi.Id=elimu_shahada_uzamili.Id and maelezobinafsi.Id=mahali_utokapo.Id and maelezobinafsi.Id=matatizo.Id and maelezobinafsi.Id=michezo_unayocheza.Id and maelezobinafsi.Id=mkataba.Id and maelezobinafsi.Id=mrithi.Id and maelezobinafsi.Id=ujuzi_mwingine.Id and maelezobinafsi.Id=wazazi.Id and maelezobinafsi.Id=walezi.Id and maelezobinafsi.Id=picha.Id and mkataba.Mkataba='KUJITOLEA' and maelezobinafsi.Id= ?";
In order to delete from multiple tables, you should join on the condition, for example :
DELETE elimu_advance, elimu_astashahada
FROM elimu_advance
INNER JOIN T2 ON elimu_advance.key = elimu_astashahada.key
WHERE condition;
Please note that this example works on MySQL (might not work in some other SQL versions, please state if you use a different one)

Deleting rows based on multiple Joins and where statements in 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

SQL, How to delete rows from related tables using a query?

I have the following tables
vehicle (veh_num(PK), veh_desc)
log (log_num(PK), veh_num(FK), log_date, log_complaint)
log_line (log_num(PK,FK), logline_num(PK), emp_id(FK), logline_date,logline_action)
part (part_code(PK), logline_num(FK), log_num(FK), part_desc)
signout (signout_num(PK), part_code(FK), emp_id(FK), log_num(FK), signout_date)
I want to run a query which will delete all the records in the vehicle table with for instance, veh_num = "EK458" and also delete rows which are related to the veh_num in the other tables.
I have started with the following query,
DELETE FROM signout WHERE EXISTS
(select * from vehicle,log,log_line,part
where
vehicle.veh_num = 'EK458' AND
vehicle.veh_num = log.veh_num AND
log.log_num = log_line.log_num AND
log_line.log_num = part.log_num AND
part.part_code = signout.part_code);
This query deletes all the associated values of veh_num = "EK458" in the signout table, however, I want a query which will delete the rows from the all the tables which are related to veh_num.
Thanks in advance
I think what you want to do is having the delete cascade into other tables.
Take a look at this How do I use cascade delete with SQL Server?
Read this link http://beginner-sql-tutorial.com/sql-integrity-constraints.htm
and have you tried join instead of subquery in query?

Avoiding join in MS Access delete query

I am trying to create a delete query to remove records from one table, based on whether or not one of the field exists in another master table. The situation is that I am importing new records into a database, but I want to remove the records that have already been imported, i.e. that already have an account in the master table. The field I need to join on, however is not equal: it is prefixed with a constant three letter code XYZ.
tbl_to_import.Account master_table.Account
123456 XYZ.123456
345678 XYZ.345678
To avoid using a join in the delete query I tried the following:
Delete tbl_to_import.*
From tbl_to_import
Where Exists( Select master_table.Account From master_table
Where master_table.Account = ("XYZ."& tbl_to_import.Account) ) = True;
However, the query gets hung up in Access. I'm not sure what I'm doing incorrectly. I don't get an error message, but the query runs without producing anything and I eventually stop it. In this situation, tbl_to_import has 2,700 records and master_table has 50,000 records. Additionally, I am connecting to the master_table via ODBC.
Originally, I constructed two queries using a join to perform the delete. tbl_to_import.Account has a primary key called ID. One query, qry_find_existing_accounts, located the ID numbers in tbl_to_import for which there exists a corresponding account in master_table.Account:
SELECT DISTINCTROW tbl_to_import.ID AS DELETEID
FROM tbl_to_import LEFT JOIN master_table
ON ("XYZ."& tbl_to_import.Account) = master_table.Account
WHERE ((("XYZ." & [Account])=[master_table].[Account]));
Then I used this query to construct the delete query:
DELETE DISTINCTROW tbl_to_import.*, tbl_to_import.ID
FROM tbl_to_import RIGHT JOIN qry_find_existing_accounts
ON tbl_to_import.ID =qry_find_existing_accounts.DELETEID
WHERE (((tbl_to_import.ID)=[qry_find_existing_accounts].[DELETEID]));
The query qry_find_existing_accounts worked fine; however, when I tried to run the second query to delete, I got the error: Could not delete from specified tables. Typically, when I get this error, it is because I have not selected unique records only, however, I used DISTINCTROW in both queries.
Any ideas what I am doing wrong and how I can accomplish what I need to do?
I'd go with a simpler nested SQL statement:
Delete tbl_to_import.*
From tbl_to_import
Where "XYZ." & tbl_to_import.Account In
(Select master_table.Account From master_table);
This should be fairly fast, especially if your Account fields are indexed.
I think you can simplify the query; delete based on the ID, where the IDs are in the query:
DELETE * FROM tbl_to_import
WHERE tbl_to_import.ID IN (
SELECT DISTINCT [DELETED] FROM qry_find_existing_accounts
)