I have two tables with many to many relationship.
The tables are
table user
idUser
name
table right
idRight
type
table user_has_right
idUser
idRight
When I insert a user with the rights I want him to have,
obviously the middle table (user_has_right) must have some inserts
too. Let's say I want to insert the user with id = 1 and name = 'test'
and right = 'boss' , where boss is already a row in table right with id = 1 .
The middle table must have an insert of (1,1).
I know how to do this programmatically, but can this be done with trigger on insert?
Thank you.
//The problem I'm facing is how to find the idRight for type 'boss'. Maybe a nested select inside the trigger?
//Is this task even possible?
I've come across this old question. If you're still interested in an answer then here it is
CREATE TRIGGER tg_user_insert
AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_has_right (idUser, idRight)
SELECT NEW.idUser, idRight
FROM `right`
WHERE type = 'boss';
Here is SQLFiddle demo
Related
I have an assignment from uni where i need to simulate something like Spotify (without player).
Some context: The thing is i want to make a trigger so that when a new playlist is created (column on table playlist), the creator user (userID) is set as it's first follower. The table followingPlaylist is an intermidiate table that keeps record of which user follows which playlist. On top of that, accounts have 2 types, artists and users (so there is the table accounts that keeps the account info, users and artists that keep the accountID and userID or artistID respectively).
The problem i have is that mysql throws sintax error with this query, if anyone could help i'd be much obliged. This is the query:
CREATE TRIGGER primer_seguidor ON playlists
FOR INSERT AS
INSERT INTO followingPlaylist
(accountID, playlistID)
SELECT
playlists.playlistID, users.accountID FROM playlists
INNER JOIN usuers ON playlists.userID = users.userID;
CREATE TRIGGER primer_seguidor
AFTER INSERT
ON playlists
FOR EACH ROW
INSERT INTO followingPlaylist (accountID, playlistID)
SELECT NEW.playlistID, users.accountID
FROM users
WHERE NEW.userID = users.userID;
PS. Syntax correction only, no logic checking.
PPS. Modelling fiddle
I have 4 columns in a table and this are.
when a new item is inserted it will look like this
I have a seperate table that look like this (based on the above image) it will appear like this
It looks like more of a logs. Now I have a code for that.
CREATE TRIGGER insert_on_tbl1
AFTER INSERT ON table1
FOR EACH ROW
INSERT INTO table2 (Person1,Item,tag) VALUES (New.Person1,New.Item,'Inserted');
now here is my question lets there is a modification happen something like this
and the responsilble for updating is on the Person1 Column (AAA) how can I insert that in my logs table? actually i have a code here for that and here it is
CREATE TRIGGER update_basedon_tbl1
AFTER UPDATE ON table1
FOR EACH ROW
IF IFNULL(NEW.Person2,"") = "" THEN
INSERT INTO table2 (Person1,Item,tag) VALUES (New.Person1,New.Item,'Modified');
END IF;
The problem here now is if the person who updates comes from Person2 still it saves the logs how can I fix that? I mean I want that all logs will be saved if changes are made from Person1 column only.
I'm trying to add a record to a table based on the value in a textbox on a form IF the value does not already exist in the table. I tried using INSERT INTO unsuccessfully. I've also tried this. I've seen alot out of the internet regarding how to add a record to a table based on a different table, but not based on the value in a userform. Any suggestions? Thanks!
Update tb1
Set Company = [forms]![form2]![Text0]
WHERE ([forms]![form2]![Text0] NOT IN tb1);
It would be like:
Insert Into Tb1 ( Company )
Select First(NewCompany)
From Tb1
Where NewCompany Not In (Select Company From Tb1 Where Company = NewCompany)
Having First(NewCompany) Is Not Null;
You will have to either pass NewCompany as a parameter or replace it with your Forms reference.
I have two table:
Am using this query to populate my marks table with all 'id' in student table:
$module1= "insert into marks (stud_id,moduleID)
select sid, 1
from user_student
where sid not in (
select * from (
select distinct stud_id from marks where moduleID=1
) alias_name
)";
So i need to take all 'sid' from Table student and populate them into table marks my query above does all that. The problem am encountering is that every time i make a change to the data e.g column test1 , new record are inserted again.
If i populate the marks table manually, Can't i have a code that when new data is available in student table, the data is just updated in marks table...
I don't understand some points of your insert query, why insert sid in stud_id if there's a id in student table to relate them?
Maybe solve your problem the creation of a composed unique key to moduleID and stud_id and use a REPLACE in place of INSERT.
But the right way to do it is using a TRIGGER like:
DROP TRIGGER IF EXISTS `student_after_insert`;
CREATE TRIGGER `student_after_insert` AFTER INSERT ON `student`
FOR EACH ROW INSERT INTO marks (stud_id,moduleID)
SELECT NEW.`id`, `modules_table`.`id` FROM `modules_table`;
PS: I suppose you have a table of modules with name modules_table. Change it accordingly.
I haven't understand your question at the best, my tip is:
Make a unique constraint on marks table (up to you is the choice of best constraint rule).
Then use a trigger to make the right insert/update:
Something like that, (hope that syntax is right):
CREATE TRIGGER myTrigger
AFTER INSERT ON students
FOR EACH ROW
BEGIN
// check that insertion doesn't broke the constraint previously define, then
insert into marks (stud_id,moduleID) values (new.sid, 1);
END
Note: NEW is the row that you have as soon inserted/updated on students. Similar you have OLD metarow.
I have two data base A & B, in each one I have a table called answer, I want to use the 2nd one as an archive table, I want to create a trigger that copies the last inserted row in A.answer to B.answer.
Here what I did
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW INSERT INTO `B`.`answer` SELECT * FROM `answer`
This trigger works, but copy all the answers inserted in A.answer to B.answer.
The problem is : I dont want to copy all answers, but only the last one.
(remark : I dont know the id of the inserted answer, so dont tell me to add a ' WHERE answer.id = xx ').
Thanks for your help
You could write your trigger this way:
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW
INSERT INTO `B`.`answer` VALUES (NEW.col1, NEW.col2, ..., NEW.colN)
where you have to specify all column names.
Please see fiddle here.