How to Update table1 if the table1 primary key existing in table2. I am using following code but it gives mysql syntax error.
CREATE TRIGGER upd_selectoin
BEFORE UPDATE ON customer
FOR EACH ROW
BEGIN
IF NEW.customer_sk IN(SELECT quotation_cname FROM quotation) THEN
UPDATE customer s JOIN quotation m
ON m.quotation_cname = s.customer_sk
SET s.grade = 2
WHERE s.customer_sk = NEW.customer_sk;
END IF;
I got the following Error
#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 '' at line 9
I want to update the customer table grade column if the customer_sk existing in quotation table.Please help me
Related
I am trying to add a trigger, when a table is updated (except for two columns in that table), it updates another table. The query I used is given below:
CREATE TRIGGER my_trigger
BEFORE UPDATE ON town_1a2b3c4d
BEGIN
IF NOT UPDATE (is_killed) AND NOT UPDATE (is_executed)
BEGIN
UPDATE town_details SET game_index = game_index + 1 WHERE town_id = '1a2b3c4d';
END;
END;
But it gives me this error:
MySQL said
#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 'BEGIN
IF NOT UPDATE (is_killed) AND NOT UPDATE (is_executed)
BEGIN...' at line 3
I want to create trigger for update one table in database 2 after update one table in database 1:
This code has syntax error!
I want to sync these field
db2.staff.name
db1.users.username
SQL code:
delimiter |
CREATE TRIGGER t1 AFTER UPDATE ON db1.users
BEGIN
UPDATE db2.staff SET name = NEW.username WHERE name = NEW.username;
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 'BEGIN UPDATE ticket.staff SET name = NEW.username WHERE name = NEW.username' at line 2
I'm trying to insert data in a table where I can track the first print date(PrintDate) and the latest print date (RePrint) of the specific persons and save the dates where I first printed and the latest print into my database.
My database looks like this
INSERT INTO PrintTable (PrintDate, RePrint)
VALUES
(
'2019-07-25 10:37:46',
'2019-07-25 10:37:49'
)
ON DUPLICATE KEY
UPDATE
PrintDate = '2017-07-25 10:37:46',
RePrint = '2019-07-25 10:37:49'
WHERE MEMB_N = '000002';
This is the error that I got:
Query: INSERT INTO PrintTable (PrintDate, RePrint) VALUES ( '2017-07-25 10:37:46', '2019-07-25 10:37:49' ) ON DUPLICATE KEY UPDATE Prin...
Error Code: 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 MEMB_N = '000002'' at line 11
Error Code: 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 MEMB_N = 'The specific user number'' at line 11
The ON DUPLICATE KEY does not allow a where clause and it is not needed because MySQL already know which record has to be updated : it is the one that generated the DUPLICATED KEY
The INSERT part of you query is wrong because it implies that the unique constraint is set on {PrintDate, Reprint}. I guess it is actually on MEMB_N
So your query should be
INSERT INTO PrintTable (MEMB_N, PrintDate, RePrint)
VALUES
(
'000002',
'2019-07-25 10:37:46',
'2019-07-25 10:37:49'
)
ON DUPLICATE KEY
UPDATE
PrintDate = '2017-07-25 10:37:46',
RePrint = '2019-07-25 10:37:49';
It means : try to insert a new record for MEMB_N = '000002'. If this record already exists then update PrintDate and RePrint for this record.
I have a mysql table school
id dob name surname
1 03.04.2011 jj
2 14.07.1999 na
.. ............ ..
I have many rows of data in the above table. Now what I want to do is; fill the surname column using an insert clause as follows
INSERT INTO `school` (surname) VALUES
('highvision'),
('oceanof'),
('malindimetho'),
('tahdhibprima'),
('stpatricks'),
...............
('stpatricks');
note: the number of rows I am inserting equals the number of rows in my table
On using the above INSERT statement I get the following 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 ..
How can I insert the rows?
You cannot use INSERT to fill the table.
You can use the following:
UPDATE `school` SET `surname` = 'highvision' WHERE `id` = '1'
CREATE TRIGGER Tr1
AFTER DELETE ON Table1
REFERENCING
OLD TABLE AS OldTable,
NEW TABLE AS NewTable
FOR EACH STATEMENT
Produces the following error:
ERROR 1064 (42000): 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 'REFERENCING
OLD TABLE AS OldTable,
NEW TABLE AS NewTable
Why is this?
If you want to view old table, then you should create BEFORE DELETE trigger -
CREATE TRIGGER Tr1
BEFORE DELETE
ON Table1
FOR EACH ROW
BEGIN
...
SET #old_count = NULL;
SELECT COUNT(*) INTO #old_count FROM Table1;
...
END