mysql trigger with inner join - mysql

I have 3 tables in mysql database that is linked to my issue tracker.
tables are:
1.Issues (contains ID, Issue_name & assignee)
2.change_group (contains ID, Issue_id, created_time)
3.change_item (contains id, group_id,field, old string , new string)
when any change occures about my isuues like changing status or assignee of issue,
table change_group will have new record. one goupid is matched with one or more records in change_item table. for example on group_id can save change in status and assignee of an issue:
now
according to changing assignees of issue I added a column with assignee name into issue table and I want to have a trigger that save snapshot of assigee field when a change happens.and I tried this:
create TRIGGER save_Assignee AFTER INSERT ON changeitem
FOR EACH ROW
BEGIN
SET new.assignee=(select assignee from jiraissue INNER JOIN changegroup ON jiraissue.ID=changegroup.issueid)
END
It seems that I cant Have inner join with trigger at same time.
If anyone has any thoughts on this it would be much appreciated. Thanks
MINA

table "issues"
id | Issue_name | Assignee
11 | R&D | User1
table "changegroup"
id | issue_ID | Created_time
12 | 11 | 2015/2/2 16:40
table "change_item"
id | group_id | field | old string | new string
13 | 12 |status | to do | in progress
I want to have a trigger to save who is assignee at the time that change happens on an issue?

You want a before insert trigger, not an after insert trigger:
create TRIGGER save_Assignee BEFORE INSERT ON changeitem
FOR EACH ROW
BEGIN
SET new.assignee=(select assignee from jiraissue INNER JOIN changegroup ON jiraissue.ID = changegroup.issueid)
END
I also suspect that your subquery is all wrong. It probably returns too many rows, because it needs a reference to new. However, that is not your question. If you decide that you have such problems, then ask another question, with sample data and desired results.

Related

How to implement Update Mechanism in MySql?

My Use Case :
I am trying to create a GUI and implement it with MYSQL Database. The problem I am facing is the scenario when I have to update a certain entry in the Database.
I know that we can update an Entry in MYSQL database using :
ALTER TABLE <TABLENAME> SET <PARAMETERS=NEW VALUES> WHERE <CONDITION> ;
For eg : If I want to change the name of the guy who id is 2 , I have to write :
ALTER TABLE StudentInfo SET Name='ABC' WHERE id=2 ;
But the problem is , in a GUI based environment , a user can choose to update any particular value wihtout having a constant condition like id in the previous example.
In the UI , the user can opt to select anything from the parameters and modify it and then click the update button.
Now How will I figure out what <CONDITION> to put in the MYSQL query when I need to update the database ?
Any help would be greatly appreciated !
you update a by using the UPDATE command not ALTER, which will change table. Your gui already knows ho tow identify the row in your case for example by the column name
UPDATE StudentInfo SET Name='ABC' WHERE Name='QUERTY';
SEE example
CREATE TABLE StudentInfo(
Name VARCHAR(20),
class int,
section VARCHAR(2),
roll_no int
);
INSERT INTO StudentInfo VALUES ('abc',12,'A',18), ('xyz',12,'A',17),('QUERTY',12,'A',16)
UPDATE StudentInfo SET Name='ABC',class = 15,section = 'B',roll_no= 99 WHERE Name='QUERTY';
SELECT * FROM StudentInfo
Name | class | section | roll_no
:--- | ----: | :------ | ------:
abc | 12 | A | 18
xyz | 12 | A | 17
ABC | 15 | B | 99
db<>fiddle here
The main problem is to identify the correct row, so you should have a field that is unique.
Like an id auto_increment, that is invisible for the user, but you can identify every row and use this id to update the row.
UPDATE StudentInfo SET Name='ABC' WHERE id = 3;
So that if you have two rows with John Smith you still could update the right one

mysql trigger one to many relation

So i have 3 tables Production, Stop_Prodcution and triggered_table.
production has a one to many realation with Stop_prodcution where a production can have a lot of stop prodcutions.
production table
-----------------------
id_prod | date
-----------------------
1 |20/03/2019
2 |18/04/2019
Stop_Production table
----------------------------
id_stop | name | id_prod
----------------------------
1 | Any reason | 1
2 | Lunch | 1
3 |damaged prod| 2
triggered_table
----------------------------
id|id_prod|date|id_stop|name
i've created 2 triggers:
after insert into production
for each row
insert into triggered_table
(id_prod,date) values (new.id_prod, curdate())
and the other one:
after update
set id_stop=new.id_stop,
name= new.name
where id_prod= new.id_prod
the problem is that a production record is able to have 2 or more stop_Production records so with the triggers that I have, it will update always the same record, but what I need is a new record with same information of production table and the information that differs from the new inserted row in stop_production, please tell me if I explained my self if not I'll try to be more clear.
This query will give you the results you want, without using a trigger:
SELECT
t1.id_stop,
t1.id_prod,
t1.`name`,
t2.date
FROM stop_production
LEFT JOIN production
ON (t1.id_prod = t2.id);
If you want to make a "table" out of this, you can create a view.
CREATE VIEW triggered_table AS (
SELECT
t1.id_stop,
t1.id_prod,
t1.`name`,
t2.date
FROM stop_production
LEFT JOIN production
ON (t1.id_prod = t2.id)
)
Then, if you want to SELECTfrom this "table", you can simply:
SELECT * FROM triggered_table;

How to copy a table (MySQL) and automatically update the new one?

I want to create a small copy of a bigger table and link both of them.
Every time I make an update in the bigger one, I want that the small one updates too.
For example, I have this data:
Big table:
id | name | price
1 | a | 10
2 | b | 12
Small table:
id | name
1 | a
2 | b
---- UPDATING THE BIGGER ONE ---
Big table:
id | name | price
1 | y | 10
2 | b | 12
3 | c | 13
Small table should become AUTOMATICALLY (after I update the bigger one):
id | name
1 | y
2 | b
3 | c
Do you know how to do it?
To do exactly what you're asking you could use triggers. Triggers are SQL that execute automatically when certain events happen. To mirror the data you would need to create UPDATE, INSERT, and DELETE triggers. (I don't have access to a MySQL instance at the moment to test, so there could be typos)
CREATE TRIGGER big_to_small_insert
AFTER INSERT ON big
FOR EACH ROW
INSERT INTO small (id, name) VALUES (NEW.id, NEW.name)
CREATE TRIGGER big_to_small_update
AFTER UPDATE ON big
FOR EACH ROW
UPDATE small SET name = NEW.name WHERE id = NEW.id
CREATE TRIGGER big_to_small_delete
AFTER DELETE ON big
FOR EACH ROW
DELETE FROM small WHERE id = OLD.id
However, a View is probably a better option if the "small" table is truly just the big table with a subset of data. A View won't store a copy of the data, so if you update the table (big) it will be reflected in the view (small), but the opposite is also true. If you do an INSERT, UPDATE, or DELETE on the view (small) it will actually happen in the table (big).
CREATE VIEW small AS
SELECT id, name FROM big

complex update query SQL using 2 tables

I have 2 tables, original and updated.
original:
|---------|---------|---------|---------|
| URL | funded| days| time|
|---------|---------|---------|---------|
updated
|---------|---------|---------|---------|
| URL | funded | days | time |
|---------|---------|---------|---------|
If two rows (one in table original and one in updated) have the same URL,then I would like to grab funded and days in the updated table and update the funded and days in original table where the matching URL appears.
Can this be achieved using SQL and how?
You should be able to use MySQL's UPDATE join syntax here.
UPDATE original a
INNER JOIN updated b
ON a.URL = b.URL
SET a.funded = b.funded,
a.days = b.days

How to rewrite a database subquery into a join?

I am trying rewrite this subquery into a join. I have read the other questions on SO but cant get this one working.
create table job (
emplid int,
effdt date,
title varchar(100),
primary key (emplid, effdt)
);
insert into job set emplid=1, effdt='2010-01-01', title='Programmer';
insert into job set emplid=1, effdt='2011-01-01', title='Programmer I';
insert into job set emplid=1, effdt='2012-01-01', title='Programmer II';
insert into job set emplid=2, effdt='2010-01-01', title='Analyst';
insert into job set emplid=2, effdt='2011-01-01', title='Analyst I';
insert into job set emplid=2, effdt='2012-01-01', title='Analyst II';
#Get each employees current job:
select *
from job a
where a.effdt=
(select max(b.effdt)
from job b
where b.emplid=a.emplid);
Results:
+--------+------------+---------------+
| emplid | effdt | title |
+--------+------------+---------------+
| 1 | 2012-01-01 | Programmer II |
| 2 | 2012-01-01 | Analyst II |
+--------+------------+---------------+
I would like to rewrite the query into a join, without a subquery. Is this possible?
Writing this as a join is perhaps a bit counterintuitive. The idea is to use a left outer join and include in the condition that b.effdt > a.effdt. This condition will match rows except when a.effdt takes on the maximum value. The query can then filter for these using a where:
select a.*
from job a left outer join
job b
on b.emplid = a.emplid and
b.effdt > a.effdt
where b.effdt is NULL;
Have you considered rewriting your schema?
If you are able to, it might be better to have a history or log table that has entries for when the effective date was changed, for which employee ID and what the previous title was. That way you would just query the actual table and get the results that you want.
This can be achieved by using triggers for whenever a row in the database is changed, then everything is handled at the database level.