Running the following query from my PhpMyadmin SQL tab - mysql

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10

Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

Related

Mysql trigger to insert the row if not exist in table but doesn't work

I am trying to create a trigger to check if a row with the same subject and object names is created. If not, I want to insert it else I will show an error message.
I have looked up some examples and written the code below but there are some errors.
DELIMITER $$
CREATE TRIGGER insertOnceKB BEFORE INSERT ON knowledgebase FOR EACH ROW
BEGIN
IF NEW.Object, NEW.Subject, NEW.Predicate not in
(SELECT A.Object, A.Subject, A.Predicate FROM knowledgebase as A
where (NEW.Object = A.Object and NEW.Subject = A.Subject and NEW.Predicate = A.Predicate)) THEN
INSERT INTO knowledgebase (Object,Predicate,Subject,Count,botID,Source,EntityTypeID)
VALUES ( NEW.Object, NEW.Predicate, NEW.Subject, NEW.Count, NEW.botID, NEW.Source, NEW.EntityTypeID);
THEN
CALL `Insert not allowed`;
END IF;
END $$
DELIMITER ;
This is the error message that Xampp server showed:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near ' NEW.Subject, NEW.Predicate not in (SELECT A.Object, A.Subject,
A.Predicate FR' at line 3
this statement
IF NEW.Object, NEW.Subject, NEW.Predicate not in (SELECT A.Object, A.Subject, A.Predicate FROM knowledgebase as A where (NEW.Object = A.Object and NEW.Subject = A.Subject and NEW.Predicate = A.Predicate))
is wrong, even if it works out, I don't think a trigger would allow to query the very table you're about to insert in, but anyway,
you can do it this way
if exists( select * from knowledgebase as a where new.Subject = a.Subject and new.Predicate = a.Predicate and new.Object = a.Object ) then
return bla bla bla;
else if
do whatever you were doing...
end if

triggers in mysql getting an error

I'm trying to create a trigger that will update some value, and if the row is not exists than insert a new row with value = 1.
I wrote the trigger in 3 possible ways and all of them are getting error
version 1:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics
SET item_id = NEW.item_id,
likes = 0,
comment = 1
ON DUPLICATE KEY UPDATE
SET comment = OLD.comment + 1;
END;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'SET comment = OLD.comment + 1' at line 9
version 2:
CREATE TRIGGER stat_trg AFTER INSERT ON comments
FOR EACH ROW
BEGIN
DECLARE num integer DEFAULT 0;
SELECT comment into num FROM statistics
WHERE item_id = OLD.item_id;
SET num := num + 1;
INSERT INTO statistics (item_id, comment, likes) VALUES (num,1,0)
END;
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 4
version 3:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics (item_id, likes, COMMENT) VALUES (NEW.item_id,0,1)
ON DUPLICATE KEY UPDATE
comment = comment + 1;
END;
1064 - You have an error in your SQL syntax; check the manual that corresponds > to your MySQL server version for the right syntax to use near '' at line 6
All looks good according to examples that I found, what am I missing?
Please help on this issue.
Thanks.
Please try this:
DELIMITER $$
CREATE TRIGGER `stat_trg` BEFORE INSERT ON `comments`
FOR EACH ROW BEGIN
INSERT INTO `statistics` set item_id= NEW.item_id, likes = '0', comments = '1' ON DUPLICATE KEY UPDATE
comments = values(comments) + 1;
END$$
DELIMITER ;

Unidentifiable error in SQL syntax, thrown when trying to store a procedure

I am getting an error when I try to enter the following procedure using the phpMyAdmin SQL box. I'm pretty sure the syntax is correct though!
ERROR:
1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use near
')
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = de' at line 1
SQL SYNTAX:
delimiter $$
CREATE PROCEDURE DeleteCard(IN aCard varChar)
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = decks.DeckName
SET decks.DeckTotal = decks.DeckTotal - amount.Amount
WHERE amount.CardName = aCard ;
UPDATE types t1
JOIN cards ON cards.TypeName = t1.TypeName
JOIN Amount ON amount.CardName = cards.CardName
SET t1.TypeTotal = t1.TypeTotal - amount.Amount
WHERE amount.CardName = aCard ;
DELETE
FROM amount
WHERE cardName = aCard;
DELETE
FROM cards
WHERE cardName = aCard;
END
$$
DELIMITER ;

MySQL Trigger not saving

Executing this:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END
Results in this:
/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8 */
What am I missing? This should work, shouldn't it?
Thanks
You're most likely trying to add the trigger without changing the delimiter. Since the trigger contains semicolon, you'll have to change the delimiter to something else temporarily;
DELIMITER //
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END //
DELIMITER ;
An SQLfiddle with the trigger adding successfully. Note that the delimiter is changed in the settings,
Here's another implementation:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)