I am having a problem with a creation of a trigger.
So I have two tables: product(name,...) and detect(id,name_prod,quantity), and I wrote a code that whenever you insert a value in product, you would create a new row in detect for each id existent and initiate the value of quantity to zero:
DELIMITER $$
CREATE TRIGGER detect_default
AFTER INSERT ON product
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT name_prod FROM detect WHERE new.name=name_prod) THEN
INSERT INTO detect(name_prod,quantity) VALUES(new.name,0);
END IF;
END$$
DELIMITER ;
The problem of this code is that only creates one row in detect with the name insert on product.name and quantity 0, but the id will be null.
How can I create a row for each existent ID?
Related
I have the following tables:
Fruits
Fruit Table Image
Pair
Pair Table Image
Basically I would like to create a trigger, that when I insert a new fruit that has a sweetness of 5, I would like the rest of the fruits to have their ids paired with this newly inserted fruit added to the pair table.
So example if I were to issue the command, Insert into Fruits values (1006, 'Kiwi', 5);
I would expect the Pair table to be updated as follows,
Updated Pair Table
I tried to create a trigger with the following code,
Delimiter //
CREATE TRIGGER t1 AFTER INSERT ON fruits
FOR EACH ROW
BEGIN
IF (sweetness.new=5) THEN
INSERT into Pair values(id.old,id.new);
END IF;
END//
Delimiter;
However, after I tried the above code, my MariaDB console application seems to be stuck, whenever i press enter the -> arrows keep appearing.
Stuck error
Any kind soul in the universe Please help!
You could
Delimiter //
CREATE TRIGGER t AFTER INSERT ON fruits
FOR EACH ROW
BEGIN
IF (new.sweetness=5) THEN
INSERT into Pair
SELECT DISTINCT ID,NEW.ID
FROM FRUITS
where id <> new.id
;
END IF;
END //
DELIMITER ;
Note the space between the end and the delimiter.
What is the error in the following code. I am executing in mysql
CREATE TRIGGER tg_order_insert
BEFORE INSERT
ON `order` FOR EACH ROW
BEGIN
INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END;
Grocery is the database and order_seqid and order are 2 table.
order_seqid is a table with only 1 attribute if type int and auto increment.
Am trying to put a prefix on the id which we insert into order table.
I am getting 2 errors in INSERT INTO..... and END; line
Did you declare a delimiter before your trigger definition? Something like
DELIMITER //
CREATE TRIGGER tg_order_insert
BEFORE INSERT
ON `order` FOR EACH ROW
BEGIN
INSERT INTO `grocery`.`order_seqid` VALUE(NULL);
SET NEW.order_id = CONCAT('#GNC', LPAD(LAST_INSERT_ID(),3,'0'));
END
//
Because if you don't, then MySQL thinks you're trying to end your trigger definition when it sees that first ; and calls syntax error.
Tables
I have the two tables above. In tblUniformAndMaterials the field AllocatedMaterials is populated from a drop down list which is fed by from tblMaterials. Once selected the field MaterialID in tblUniformAndMaterials is Auto Populated from tblMaterials.
What I want to accomplish by using a Trigger is after the record in tblUniformAndMaterials is inserted I want to update the NiveauDeStock field in tblMaterials to (NiveauDeStock-1). In other words after each material allocation I reduce the stock level by one.
Reference for CREATE TRIGGER https://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
Try this:
DELIMITER $$
CREATE TRIGGER tblUniformAndMaterials_ai
AFTER INSERT
ON tblUniformAndMaterials
FOR EACH ROW
BEGIN
UPDATE tblMaterials
set NiveauDeStock = NiveauDeStock -1
WHERE CodeDeMaterial = NEW.MaterialID;
END;
$$
DELIMITER ;
Note AFTER INSERT and how NEW.MaterialID is the ID inserted into tblUniformAndMaterials.
Good Luck!
I need to update prefixed_sku column by prefixing "A|" for every insert or update on column sku
SKU------------prefixed_sku
Hp3001-------- A|HP3001
HS1001-------- A|HP3001
I tried reading many articles but I'm not a programmer so could not figure it out on my own. This is for my orders database. Thanks
table name: orders
column name: sku
Add an after insert and after update trigger to go back and update the prefixed_sku column in the orders table:
DELIMITER $$
CREATE TRIGGER after_order_insert
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
UPDATE orders
SET prefix_sku=CONCAT('A|',sku)
WHERE sku=NEW.sku
END$$
DELIMITER ;
I'm trying to create a trigger, however I keep getting back a syntax error.
Here's the statement:
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END
DELIMITER ;
I have a registration form on my site. When a person registers it populates the Candidates table with their profile information.
In the Candidates table, there are various fields, two of them being 'email' and 'UserID'.
UserID is also the PK in 'useradmin', so I'm linking the two up.
So when a user registers, I need to insert a record into 'useradmin' with the email address that's just been used to register, and then update the 'Candidates' table, with UserID that's just been created in 'useradmin'.
I hope this makes sense?
NB. I am changing the delimiter before running the statement.
Besides properly using DELIMITER when creating a trigger you have at least two fundamental issues with your current code:
In MySQL you can't use issue a DML statement (in your case UPDATE) against a table (candidates) on which you defined a trigger (also candidates). Your only option is to use BEFORE trigger and set a value of userid column of a row being inserted to a proper value.
You can't arbitrarily reference a column (useradmin.userid) of a table out of the context like you did in your UPDATE. You didn't joined useradmin table or used it in a subquery.
That being said and assuming that userid in useradmin table is an auto_increment column your trigger might look like this
DELIMITER $$
CREATE TRIGGER after_candidate_insert
BEFORE INSERT ON candidates
FOR EACH ROW
BEGIN
INSERT INTO useradmin (`username`, `talent`) VALUES (NEW.email, 1);
SET NEW.userid = LAST_INSERT_ID();
END$$
DELIMITER ;
Here is SQLFiddle demo
You should use semicolon after end your insert query.
You can use INSERT ... ON DUPLICATE KEY UPDATE syntax for your purpose
try out this...
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END $$
DELIMITER ;