Delete records by trigger in MS Access - ms-access

My case:
I have to tables A and B. Both of them have columns named 'ID'.
I want to remove records from table B, after insert record to table A. For example, when I insert to table A record with ID = 50, I want to remove from table B all records when ID = 50.
I wanted to use 'After Insert' trigger, but there is no option 'Delete Record' in other table.
It's worth adding that in my case I can't use sql query to delete records.
Any ideas?

Related

SQL Insert with Inner Join trying to insert into wrong column

I have an existing table of products, and I'm trying to insert new attributes from other tables into my main product table.
Here's my query:
INSERT INTO company_attributes (amp)
SELECT company_attr_amperage.amp
FROM company_attr_amperage
INNER JOIN company_attributes
ON company_attr_amperage.product_id = company_attributes.product_id;
The error that I get is: Field 'product_id' doesn't have a default value.
I'm not trying to insert into the product_id column, I'm trying to insert into the amp column (as specified on row 1 of the query)
The amp column exists on the company_attributes table, but right now, every value is NULL
Thanks
You may just want to update the value in existing rows. If so:
UPDATE company_attributes ca JOIN
company_attr_amperage caa
ON caa.product_id = ca.product_id
SET ca.amp = caa.amp;
INSERT's job is to put new rows into a table, in your case company_attributes. Those new rows must be valid. Your table definition says that there's no default value for a column called product_id in that table. So, MySQL doesn't know what to put into that column when you don't provide it with your INSERT operation.
So, when you insert a row you must provide a value for that column.
There's no way to put a value into a column without creating a row for it, or UPDATEing an existing row.

MS Access after insert event

I have 2 tables in access, A and B, both with a case ID field and several other fields. I am trying to set up a process where if I insert a record in A with a new case ID, only the case ID of that record inserted in table A gets populated in table B.
I have tried using an After Insert event macro of table A with a create a record into table B action which should create a record with the case ID in table B. This work fine if none of the fields apart from the Case ID in table B are required. However, if I have other fields that are required in Table B, the create a record into table B does not happen upon insert into Table A.
How do I fix this issue? Thanks in advance!

Insert multiple records into a table based on AVG values from another table in mySQL

I have a mysql database with 2 tables. The first "spec" is a specification table, the second 'log' is table containing logged entries of previous measurements. Each part being logged is identified by a part number and test measurement. There may be many log entries for any given part number, but only 1 entry per part number in the 'spec' database giving the actual specification. What I need to do is obtain an average of the test measurement for every different part in the 'log' table, and insert this into the 'spec' table as a new specification. The log table will have already have been corrected to remove outliers.
I have been able to update existing records in the 'spec' table, but have been unable to insert records that do not already exist.
This works
update no_flow.spec s join
(select part, round(avg(cc),0) as avgcc
from no_flow.log l
group by part) l
on s.part = l.part and l.avgcc > 0
set s.cc = l.avgcc;
This does not work
INSERT INTO no_flow.spec set (part, cc) s join
SELECT part, avg(cc)
FROM no_flow.log l
WHERE id != 0
values (l.part, l.avgcc);
Suggestions?
If there is a unique index on part in spec you could use INSERT ... ON DUPLICATE KEY UPDATE Syntax
It would look something like this:
INSERT INTO noflow.spec (part, cc)
select
part as logPart,
round(avg(cc),0) as avgcc
from
no_flow.log
group
by logPart
ON DUPLICATE KEY UPDATE cc = VALUES(cc);
This inserts all the records from the inner SELECT into the spec table. When a given inserted record encounters a duplicate key error (i.e. there is already a record for the current part number) the ON DUPLICATE KEY clause updates the existing record by setting its cc column equal to the cc column on the record it was trying to insert.

How to remove duplicate records from multiple tables in MySQL

I have a situation like this:
I have 3 tables, for example:
table phones
table computers
table printers
Every table has the same column named "Address" and every column has the same record "06-00-00-00-00-00" (a duplicate record).
Now, I was wondering if it's possible somehow to check all the records from all of the tables and delete the duplicate records from table "computers" and table "printers" but leave the record in table "phones"
In other words: Delete all the duplicate records from all of the tables except from one chosen table (in this case table "phones").
Thanks a lot.
For deleting records,
DELETE TABLE TABLE1
WHERE ADDRESS = (SELECT ADDRESS FROM TABLE2)

My SQL INSERT INTO based on conditions

I have a table called autosaves where i my web-app saves every 4 second a user autosave in case my web-app crashes.
autoSaves
customerId
designType
autosaveFile
The condition is this:
If a customerId and designtype already exists, update the row with these values(customerId designType autosaveFile)
otherwise if the 2 conditions i mentioned do not exist then create a new row with the new values.
I have come accross the Insert Into statement but i cannot seem to understand how to formulate it so that it updates when the 2 conditions are met.
You need to create a unique index on the customerId and designType columns:
CREATE UNIQUE INDEX ix_cust_design ON autoSaves (customerId, designType);
Then you can use the following INSERT statement:
INSERT INTO autoSaves (customerId, designType, autosaveFile)
VALUES (#id, #type, #file)
ON DUPLICATE KEY UPDATE autosaveFile = VALUES(autosaveFile)