I wish to identify the value for the PRIMARY KEY of the row that I have just updated.
i.e : X is an int value PRIMARY KEY on the person table.
This trigger works when I manually enter the int value e.g replace X with 1001.
use mydatabase;
DROP TRIGGER update_approved;
delimiter $
CREATE TRIGGER update_approved AFTER UPDATE ON person
FOR EACH ROW BEGIN
UPDATE referral_person SET
approved = 1 WHERE
id = X;
END$
delimiter ;
just change it to
....
id = OLD.ColumnName
where ColumnName is the name of your primary key column.
Related
I created a table
Databases Name - mytrigger;
Table name - employee_audit
use mytrigger;
create table employee_audit(
id int auto_increment primary key,
employeeNumber int not null,
lastName varchar(50) not null,
changee datetime default null,
action varchar(50) default null
);
After that, I created one update trigger
My trigger name is
before_employees_update
DELIMITER $$
create trigger before_employees_update
before update on employee_audit
for each row
begin
insert into employee_audit
set action ='update',
employeeNumber = OLD.employeeNumber,
lastName = OLD.lastName,
changee = now();
end$$
DELIMITER ;
After that, I inserted values in table using this command ->
insert into employee_audit values(1,112,'prakash','2015-11-12 15:36:20' ,' ');
After that, I want to update my table row where id =1
update employee_audit set lastName = 'Sharma' where employeeNumber =112;
But it is not executed give an error
ERROR 1442 (HY000): Can't update table 'employee_audit' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
When I searched on Google I found a lot of Question with the same error. But not able to fix my problem. what is the reason I'm not able to update my row?
What i suggest,you can create one log table like employee_audit_LOG .
And on every insert or update in main table you can make new entry in this table or update existing record.
Also you can add updated_timestamp column to that LOG table which maintain when did specific record get updated.
The error itself tells you the answer. This is because, you can't use the same table on which trigger is being executed. You need to store your audit logs into some different table.
I hope to this work.
When UserAccount table duplicated, Out Parameter put -1.
When UserInfo table duplicated, Out Parameter put -2.
When all work is succeed, Out Parameter put LAST_INSERT_ID.
UserAccount Table
CREATE TABLE UserAccount (
SocialType ENUM('Google', 'Facebook'),
SocialID BINARY(16),
UserID INT UNIQUE NOT NULL AUTO_INCREMENT,
PRIMARY KEY(SocialType, SocialID)
);
UserInfo Table
CREATE TABLE UserInfo (
UserID INT,
Nickname VARCHAR(20) UNIQUE,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES UserAccount(UserID)
ON DELETE CASCADE,
);
I want to insert tables with duplicate checking in procedure.
But it failed to work and seems inefficient.
DELIMITER $$
CREATE PROCEDURE usp_CreateNewUser(
socialType ENUM('Google','Facebook'),
socialID VARCHAR(100),
nickname VARCHAR(20),
OUT result INT)
Work:BEGIN
IF EXISTS(SELECT 1 FROM UserAccount WHERE SocialType = socialType AND SocialID = UNHEX(MD5(socialID)))
THEN
SET result = -1;
Leave Work;
END IF;
IF EXISTS(SELECT 1 FROM UserInfo WHERE Nickname = nickname)
THEN
SET result = -2;
Leave Work;
END IF;
INSERT INTO UserAccount() VALUE(socialType, UNHEX(MD5(socialID)), 0);
INSERT INTO UserInfo(UserID, Nickname) VALUE(LAST_INSERT_ID(), nickname);
SET result = LAST_INSERT_ID();
END $$
DELIMITER ;
What is the best way I can do in this situation?
Thanks.
Judging from your trigger, you don't want to have duplicate keys in your database, as you are bypassing the insert when you find one.
So all you have to do is have your unique indexes set up correctly in your first table, which will do that work for you, and produce a display in your application.
Do the below:
ALTER TABLE `UserAccount` ADD UNIQUE `UserAccount`(`socialType`, `socialID`);
Since your data appears to be 1 to 1, id put all of this in the same table, and then you wouldn't need the trigger.
I have a table called test like this:
CREATE TABLE test(
id int auto_increment primary key,
prefix varchar(1) not null default ('s'),
newid varchar(10) null);
I would like column newid to be combined values of column id and column prefix when inserting new values into table test. For example:
id prefix newid
1 s s1
2 s s2
...
So i tried to create after insert trigger like this:
DELIMITER $$
CREATE TRIGGER test
AFTER INSERT ON test
for each row BEGIN
set newid = concat(id,prefix);
END$$
DELIMITER ;
But i got this error:
#1193 - Unknown system variable 'newid'
Please tell me what I need to fix to achieve the result needed.
Best Regards
I need a solution for copy the auto increment value to userid column when insert the record.
I have two column like id(AI),userid. Here I have used a trigger
CREATE TRIGGER adduserid BEFORE INSERT ON user
FOR EACH ROW SET NEW.userid = NEW.id
The problems is triggers invoke before insert,so it will get id as 0.
Suggest me how can I modify the trigger?
Sample:
Table definition:
CREATE TABLE departments (
ID NUMBER(10) NOT NULL,
DESCRIPTION VARCHAR2(50) NOT NULL);
ALTER TABLE departments ADD (
CONSTRAINT dept_pk PRIMARY KEY (ID));
CREATE SEQUENCE dept_seq;
Trigger definition:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
Syntax Link1:https://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Assume I am going to emulate auto-increment in MySQL/InnoDB
Conditions
Using MySQL/InnoDB
The ID field don't have unique index, nor it is a PK
Is it possible to emulate only using program logics, without table level lock.
Thanks.
Use a sequence table and a trigger - something like this:
drop table if exists users_seq;
create table users_seq
(
next_seq_id int unsigned not null default 0
)engine = innodb;
drop table if exists users;
create table users
(
user_id int unsigned not null primary key,
username varchar(32) not null
)engine = innodb;
insert into users_seq values (0);
delimiter #
create trigger users_before_ins_trig before insert on users
for each row
begin
declare id int unsigned default 0;
select next_seq_id + 1 into id from users_seq;
set new.user_id = id;
update users_seq set next_seq_id = id;
end#
delimiter ;
insert into users (username) values ('f00'),('bar'),('bish'),('bash'),('bosh');
select * from users;
select * from users_seq;
insert into users (username) values ('newbie');
select * from users;
select * from users_seq;
CREATE TABLE sequence (id INTEGER); -- possibbly add a name;
INSERT INTO sequence VALUES (1); -- starting value
SET AUTOCOMMIT=0;
START TRANSACTION;
UPDATE sequence SET id = LAST_INSERT_ID(id+1);
INSERT INTO actualtable (non_autoincrementing_key) VALUES (LAST_INSERT_ID());
COMMIT;
SELECT LAST_INSERT_ID(); Is even a session-safe value to check which ID you got. Be sure your table support transactions, or that holes in a sequence are no problem.
Create another table with a single row and column that stores the next id value. Then create an insert trigger on the original table that increments the value in the second table, grabs it, and uses that for the ID column on the first table. You would need to be careful with the way you do the select and update to ensure they are atomic.
Essentially you are emulating an Oracle sequence in MySQL. It would cause a lock on single row in the sequence table though, so that may make it inappropriate for what you are doing.
ETA:
Another similar but maybe better performing option would be to create a second "sequence" table that just has a single auto-increment PK column and no other data. Have your insert trigger insert a row into that table and use the generated ID from there to populate the ID in the original table. Then either have the trigger or another process periodically delete all the rows from the sequence table to clean it up.
sequence table need to have id as the autoincrement PK