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

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?

Related

Access: Delete Query asks for a parameter

I try to run the following DELETE query:
DELETE T_PROPOS.*, T_PROPOS.Projekt, T_PROPOS.[PSP-Element], T_PROPOS.Typ, T_PROPOS.Jahr, T_PROPOS.Wert, T_PROPOS.Datum
FROM T_PROPOS
WHERE NOT (((T_PROPOS.Projekt)=[T_PROPOS_Ohne_Duplikate].[Projekt]) AND ((T_PROPOS.[PSP-Element])=[T_PROPOS_Ohne_Duplikate].[PSP-Element]) AND ((T_PROPOS.Typ)=[T_PROPOS_Duplikate_löschen].[Typ]) AND ((T_PROPOS.Jahr)=[T_PROPOS_Duplikate_löschen].[Jahr]) AND ((T_PROPOS.Wert)=[T_PROPOS_Duplikate_löschen].[Wert]) AND ((T_PROPOS.Datum)=[T_PROPOS_Duplikate_löschen].[Datum]));
But when I run the query Access asks me for the parameters of every condition. So I need to give values to [T_PROPOS_Ohne_Duplikate].[Projekt] etc. But I want to delete all rows from T_PROPOS which are not in T_PROPOS_Ohne_Duplikate. How can I make Access compare the two tables?
Since you refer to columns from two unreferenced tables, consider two NOT EXISTS clauses. Also DELETE * FROM is equivalent to DELETE FROM in MS Access.
DELETE
FROM T_PROPOS main
WHERE NOT EXISTS
(SELECT 1 FROM [T_PROPOS_Ohne_Duplikate] o_dup
WHERE main.Projekt = o_dup.[Projekt]
AND main.[PSP-Element] = o_dup.[PSP-Element])
AND NOT EXISTS
(SELECT 1 FROM [T_PROPOS_Duplikate_löschen] l_dup
WHERE main.Typ = l_dup.[Typ]
AND main.Jahr = l_dup.[Jahr]
AND main.Wert = l_dup.[Wert]
AND main.Datum = l_dup.[Datum]);
Use this below simple query. Not sure if it works in Access.
delete from T_PROPOS where Projekt not in
(select Projekt from T_PROPOS_Ohne_Duplikate);

What's wrong with my DELETE query

I've two tables with same columns. I'm trying to delete rows from table1 '600_LONDON_NUMBER' which are in table2 '600_LONDON_NUMBER1'. Below is my query but when I run it, MS Access says "Could not delete from the specified Tables". Please Help
DELETE [600_LONDON_NUMBER].*
FROM 600_LONDON_NUMBER INNER JOIN 600_LONDON_NUMBER1
ON ([600_LONDON_NUMBER].GFCID = [600_LONDON_NUMBER1].GFCID) AND ([600_LONDON_NUMBER].CUSTBaseNO = [600_LONDON_NUMBER1].[CUST Base NO]);
P.S. When I run the SELECT Statement for the same query, it retrives the data without any Issue. I've also checked that data is not readonly I can delete using simple DELETEquery.
you may use EXISTS to solve it
DELETE 600_LONDON_NUMBER.* FROM 600_LONDON_NUMBER
WHERE EXISTS (
SELECT 1 FROM 600_LONDON_NUMBER1
WHERE [600_LONDON_NUMBER].GFCID = [600_LONDON_NUMBER1].GFCID) AND
[600_LONDON_NUMBER].CUSTBaseNO = [600_LONDON_NUMBER1].[CUSTBaseNO]
)

Cannot update a table using a simple inner join

I have 2 tables in access 2007.
See attached picture to see the structure of the tables and the expected result.
I am trying to update the quantity field (ITQTY) in TABLE_BLNC by summarizing all the quantity field (LOCQTY) from TABLE_DTL for same items (LOITNBR=ITNBR).
In TABLE_BLNC, the item is unique while in TABLE_DTL, the item can be in multiple records.
My query is:
UPDATE TABLE_BLNC INNER JOIN
(
SELECT LOITNBR, Sum(LOCQTY) AS SumOfLOCQTY FROM TABLE_DTL GROUP BY LOITNBR) AS DTL
ON TABLE_BLNC.ITNBR=DTL.LOITNBR SET TABLE_BLNC.ITQTY = DTL.SumOfLOCQTY;
I am getting the error:
Operation must use an updateable query.
Domain Aggregate functions can be useful when Access complains that an UPDATE is not updateable. In this case, use DSum() ...
UPDATE TABLE_BLNC
SET ITQTY =
DSum("LOCQTY", "TABLE_DTL", "LOITNBR='" & ITNBR & "'");
Index TABLE_DTL.LOITNBR for optimum performance.
One of the great annoyances of Access SQL is its inability to update a table from an non-updatable source. Non-updatable sources include read-only links to ODBC tables, and GROUP BY (summary) queries.
What I always do is:
Copy the structure of TABLE_BLNK to a temp table: TABLE_BLNK_temp.
In your code, first delete the temp:
DELETE * FROM TABLE_BLNK_temp;
Insert the result of your summary query into temp:
INSERT INTO TABLE_BLNK_temp (ITNBR, ITQTY)
SELECT LOITNBR, Sum(LOCQTY) AS SumOfLOCQTY
FROM TABLE_DTL GROUP BY LOITNBR;
Update TABLE_BLNK from TABLE_BLNK_temp:
UPDATE TABLE_BLNC INNER JOIN TABLE_BLNK_temp AS t
ON TABLE_BLNC.ITNBR = t.ITNBR
SET TABLE_BLNC.ITQTY = t.ITQTY;
While it is an extra step or two, this approach:
Always works
Is more performant than Domain Aggregate functions for larger datasets

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

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
)