MYSQL trigger error. How to resolve? - mysql

SQL:
DELIMITER $$ CREATE TRIGGER `Activation_code` BEFORE UPDATE
ON `user_users` FOR EACH ROW
BEGIN
IF OLD.activation_code_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE)) THEN SET NEW.activation_code = SELECT(ROUND((RAND() * (999999-100000))+100000)), NEW.activation_code_time = SELECT(UNIX_TIMESTAMP());
END
$$ DELIMITER ;
#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 'SELECT(ROUND((RAND() * (999999-100000))+100000)), NEW.activation_code_time = SEL' at line 4
Can anyone please tell me where is an error. and How can I resolve it?

Selects need to be bracketed, all statements need to be terminated with ; and you need an end if
DELIMITER $$
CREATE TRIGGER `Activation_code` BEFORE UPDATE
ON `user_users` FOR EACH ROW
BEGIN
IF OLD.activation_code_time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE)) THEN
SET NEW.activation_code = (SELECT(ROUND((RAND() * (999999-100000))+100000))),
NEW.activation_code_time = (SELECT(UNIX_TIMESTAMP()));
END IF;
END $$
DELIMITER ;
If there is a chance that activation_code_time may be null you should code for it.
drop trigger if exists `Activation_code`;
DELIMITER $$
create TRIGGER `Activation_code` BEFORE UPDATE
ON `users` FOR EACH ROW
BEGIN
IF coalesce(OLD.activation_code_time,0) < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) THEN
SET NEW.activation_code = (SELECT(ROUND((RAND() * (999999-100000))+100000))),
NEW.activation_code_time = (SELECT(UNIX_TIMESTAMP()));
END IF;
END
$$ DELIMITER ;
mysql> describe users;
+----------------------+-------------+------+-----+---------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+-------------+------+-----+---------+-------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| uid | int(11) | YES | | NULL | |
| NAMID | varchar(20) | YES | | NULL | VIRTUAL GENERATED |
| activation_code | int(11) | YES | | NULL | |
| activation_code_time | int(11) | YES | | NULL | |
+----------------------+-------------+------+-----+---------+-------------------+
6 rows in set (0.00 sec)
mysql> select * from users;
+----+------+------+-------+-----------------+----------------------+
| id | name | uid | NAMID | activation_code | activation_code_time |
+----+------+------+-------+-----------------+----------------------+
| 1 | aaa | 1 | aaa|1 | 589392 | 1514804785 |
| 2 | bbb | 2 | bbb|2 | NULL | NULL |
+----+------+------+-------+-----------------+----------------------+
2 rows in set (0.00 sec)
mysql> select * from users;update users set name = 'aaa' where id = 1;select * from users;
+----+------+------+-------+-----------------+----------------------+
| id | name | uid | NAMID | activation_code | activation_code_time |
+----+------+------+-------+-----------------+----------------------+
| 1 | aaa | 1 | aaa|1 | 589392 | 1514804785 |
| 2 | bbb | 2 | bbb|2 | NULL | NULL |
+----+------+------+-------+-----------------+----------------------+
2 rows in set (0.00 sec)
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
+----+------+------+-------+-----------------+----------------------+
| id | name | uid | NAMID | activation_code | activation_code_time |
+----+------+------+-------+-----------------+----------------------+
| 1 | aaa | 1 | aaa|1 | 616615 | 1514805252 |
| 2 | bbb | 2 | bbb|2 | NULL | NULL |
+----+------+------+-------+-----------------+----------------------+
2 rows in set (0.00 sec)

Related

I can't use input in Mysql procedure

I how defined a procedure in MySql which has an input and input will be a name of column that will be added to table 'test1' but sql names that column input instead of using the value of input .how can i do this in the right way?
DELIMITER
CREATE PROCEDURE p1
(IN input CHAR(20))
BEGIN
ALTER TABLE test1
ADD COLUMN input char(20);
END
DELIMITER ;
You should google dynamic sql and read the manual https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html
In the meantime here's an example.
drop procedure if exists p;
alter table users
drop column abc;
DELIMITER $$
CREATE PROCEDURE p
(IN input CHAR(20))
BEGIN
#ALTER TABLE test1
#ADD COLUMN input char(20);
set #sql = concat('alter table users add column ',input,' char(20);');
select #sql;
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
END $$
DELIMITER ;
call p('abc');
describe users;
MariaDB [sandbox]> call p('abc');
+--------------------------------------------+
| #sql |
+--------------------------------------------+
| alter table users add column abc char(20); |
+--------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.42 sec)
MariaDB [sandbox]> describe users;
+---------------------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+-------------------+-------+
| id | int(11) | NO | PRI | NULL | |
| userName | varchar(60) | NO | | NULL | |
| photo | varchar(50) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| ts | datetime | YES | | CURRENT_TIMESTAMP | |
| events_participated | int(11) | YES | | NULL | |
| fb_uid | int(11) | YES | | NULL | |
| Column_name | varchar(10) | YES | | NULL | |
| post_type | varchar(10) | YES | | NULL | |
| password | varchar(8) | YES | | NULL | |
| abc | char(20) | YES | | NULL | |
+---------------------+-------------+------+-----+-------------------+-------+
11 rows in set (0.03 sec)

Mysql can't select the record in the table

I have a mysql table with a null-able int column named 'status', and I have two records in the table where one of the status is 2 when the other is NULL, but when I select the records with query 'status!=2', the record (status=null) is not shown.
mysql>
mysql>
mysql> desc admin_user;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| acct_name | varchar(32) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| user_name | varchar(32) | YES | | NULL | |
| description | varchar(128) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| role | int(11) | NO | | 1 | |
| create_date | date | YES | | NULL | |
| update_date | date | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> select id, acct_name, status from admin_user;
+----+-----------+--------+
| id | acct_name | status |
+----+-----------+--------+
| 1 | letme | NULL |
| 3 | admin | 2 |
+----+-----------+--------+
2 rows in set (0.00 sec)
mysql> select id, acct_name, status from admin_user where status=2;
+----+-----------+--------+
| id | acct_name | status |
+----+-----------+--------+
| 3 | admin | 2 |
+----+-----------+--------+
1 row in set (0.00 sec)
mysql> select id, acct_name, status from admin_user where status IS NULL;
+----+-----------+--------+
| id | acct_name | status |
+----+-----------+--------+
| 1 | letme | NULL |
+----+-----------+--------+
1 row in set (0.00 sec)
mysql> select id, acct_name, status from admin_user where status!=2;
Empty set (0.00 sec)
mysql>
As you can see, the record whose status is NULL can not be selected using the query 'status!=2'. I also tried 'status<>2'. Can anybody help?
Nulls are really interesting.
The NULL value can be surprising until you get used to it.
Conceptually, NULL means “a missing unknown value” and it is treated
somewhat differently from other values.
What's more
You cannot use arithmetic comparison operators such as =, <, or <> to
test for NULL
There are lots of different ways to write this. One of them being:
select id, acct_name, status from admin_user where status IS NULL
OR status != 2
Alterantively as #shA.t suggested.
select id, acct_name, status from table1 where COALESCE(status, 0) != 2
Just check that 0 is indeed a number that doesn't appear any where else in the table.

Column value of first table needs to be update automatically whenever the second table row is updated or added

Column value of first table needs to be update automatically whenever the second table row is updated or added.
I have two table CcnCSR and CsrTimeReporting
mysql> select * from CcnCSR;
select * from CsrTimeReporting;
+-----------+-----------------------------------------+----------+-------------+-----------+-------------+------------+------------+------------+-------------------+--------+
| CSRNumber | Slogan | Severity | Customer | Status | CreatedDate | CcnQDate | TTS | IAdate | TotalTimeReported | Remark |
+-----------+-----------------------------------------+----------+-------------+-----------+-------------+------------+------------+------------+-------------------+--------+
| 2718122 | DIAMETER LINK FLICKS BETWEEN GGSN-CCN38 | High | IdeaIndia | NULL | 2105-03-04 | 2105-03-03 | 2015-03-28 | 2105-03-13 | NULL | NA |
| 2718133 | Auto ZR | High | AirtelIndia | Analysing | 2105-03-20 | 2105-03-23 | 2105-04-10 | NULL | NULL | NULL |
+-----------+-----------------------------------------+----------+-------------+-----------+-------------+------------+------------+------------+-------------------+--------+
2 rows in set (0.00 sec)
+-----------+-------+------------------+--------------+-------+
| CSRNumber | SeqId | TimeReportedDate | TimeReported | Shift |
+-----------+-------+------------------+--------------+-------+
| 2718122 | 1 | 2015-03-15 | 8 | NULL |
| 2718122 | 2 | 2105-03-03 | 0.5 | NULL |
| 2718122 | 3 | 2105-03-03 | 3 | NULL |
| 2718122 | 4 | 2105-03-03 | 4.5 | NULL |
| 2718122 | 5 | 2105-03-03 | 5.25 | NULL |
| 2718122 | 6 | 2105-03-05 | 7.25 | NULL |
| 2718133 | 8 | 2015-03-30 | 2 | NULL |
| 2718133 | 9 | 2015-03-31 | 2.5 | NULL |
| 2718133 | 10 | 2015-03-29 | 3.5 | NULL |
+-----------+-------+------------------+--------------+-------+
9 rows in set (0.00 sec)
Where in table CcnCSR there is column "TotalTimeReported", this column value needs to be update automatically whenever the second table column "TimeReported" is updated or new row is added.
How can i do this?
This is how you can do it
create trigger trigger_name
on CsrTimeReporting
for insert
as
begin
declare
#time_reported decimal,
#CSR_Number nvarchar(50) -- select data type as you need
select #CSR_Number = CSRNumber , #time_reported = TimeReportedDate from
CsrTimeReporting
Update CcnCSR set TotalTimeReported = #time_reported where
CSRNumber = #CSR_Number
You can do this using multiple triggers, one for insert, update, and delete:
delimiter //
create trigger trig:csrTimeReport_insert on CsrTimeReporting
after insert for each row
begin
update CcnCSR
set TotalTimeReported = TotalTimeReported + new.TimeReported
where CcnCSR.CSRNumber = new.CSRNumber;
end;//
create trigger trig:csrTimeReport_update on CsrTimeReporting
after update for each row
begin
update CcnCSR
set TotalTimeReported = TotalTimeReported + new.TimeReported - old.TimeReported
where CcnCSR.CSRNumber = new.CSRNumber;
end;//
create trigger trig:csrTimeReport_delete on CsrTimeReporting
after delete for each row
begin
update CcnCSR
set TotalTimeReported = TotalTimeReported + - old.TimeReported
where CcnCSR.CSRNumber = old.CSRNumber;
end;//
delimiter ;

mysql after insert trigger which updates another table's column

i'm trying to write a trigger, I have following tables:
BookingRequest:
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| idRequest | int(11) | NO | PRI | NULL | auto_increment |
| roomClass | int(11) | NO | | NULL | |
| inDate | date | NO | | NULL | |
| outDate | date | NO | | NULL | |
| numOfBeds | int(11) | NO | | NULL | |
| status | int(11) | NO | MUL | NULL | |
| idUser | int(11) | NO | MUL | NULL | |
+-----------+---------+------+-----+---------+----------------+
status table:
+------------+--------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------------------------------------------+------+-----+---------+-------+
| idStatus | int(11) | NO | PRI | NULL | |
| nameStatus | enum('underConsideration','approved','rejected') | YES | | NULL | |
+------------+--------------------------------------------------+------+-----+---------+-------+
OccupiedRoom:
+--------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+----------------+
| idOccupation | int(11) | NO | PRI | NULL | auto_increment |
| idRoom | int(11) | NO | | NULL | |
| idRequest | int(11) | NO | | NULL | |
+--------------+---------+------+-----+---------+----------------+
i need a trigger which will change status in BookingReques to 1 if request with the same id is inserted into OccupiedRoom table, so i tried something like this
create trigger occupy_trig after insert on OccupiedRoom
for each row
begin
if BookingRequest.idRequest= NEW.idRequest
then
update BookingRequest
set status = '1';
where idRequest = NEW.idRequest;
end if;
END;
and it doesn't work, so any suggestions would be very appriciated
Try this:
DELIMITER $$
CREATE TRIGGER occupy_trig
AFTER INSERT ON `OccupiedRoom` FOR EACH ROW
begin
DECLARE id_exists Boolean;
-- Check BookingRequest table
SELECT 1
INTO #id_exists
FROM BookingRequest
WHERE BookingRequest.idRequest= NEW.idRequest;
IF #id_exists = 1
THEN
UPDATE BookingRequest
SET status = '1'
WHERE idRequest = NEW.idRequest;
END IF;
END;
$$
DELIMITER ;
With your requirements you don't need BEGIN END and IF with unnecessary SELECT in your trigger. So you can simplify it to this
CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom
FOR EACH ROW
UPDATE BookingRequest
SET status = 1
WHERE idRequest = NEW.idRequest;
Maybe remove the semi-colon after set because now the where statement doesn't belong to the update statement. Also the idRequest could be a problem, better write BookingRequest.idRequest
DELIMITER //
CREATE TRIGGER contacts_after_insert
AFTER INSERT
ON contacts FOR EACH ROW
BEGIN
DECLARE vUser varchar(50);
-- Find username of person performing the INSERT into table
SELECT USER() INTO vUser;
-- Insert record into audit table
INSERT INTO contacts_audit
( contact_id,
deleted_date,
deleted_by)
VALUES
( NEW.contact_id,
SYSDATE(),
vUser );
END; //
DELIMITER ;

Automatically Update 2 Table or Multiple in Mysql

I have problem
I want to ask, how to change the data that has been entered into the table rekap_nilai. which is where the table is rekapan rekap_nilai total of table nilai_student.
I enter a table B based on trigger
mysql> select * from nilai_student;
+----+-------+------+-------+
| id | name | idmp | nilai |
+----+-------+------+-------+
| 1 | Udin | 1 | 80 |
| 2 | Udin | 2 | 60 |
| 3 | Mamat | 1 | 75 |
+----+-------+------+-------+
table rekap_nilai
mysql> desc rekap_nilai;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| idstudent | int(11) | YES | | NULL | |
| name | varchar(100) | YES | | NULL | |
| nilai | double | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
mysql> select * from rekap_nilai;
+----+-----------+-------+-------+
| id | idstudent | name | nilai |
+----+-----------+-------+-------+
| 1 | 1 | Udin | 140 |
| 2 | 2 | Mamat | 75 |
+----+-----------+-------+-------+
2 rows in set (0.00 sec)
What if there was a remedial student conduct and when update table nilai_student in column nilai, ​​automatically in table rekap_nilai in column nilai
example
now name 'Udin' have nilai 60 in id 2, and he want remedial. when he was remedial, I want to update he's nilai = 70 , and then in table rekap_nilai. udin automatically update to nilai = 150
You can write a trigger which gets executed on update of table nilai_student , some thing like below
Delimiter ///
create trigger update_rekap_nilai after update on nilai_student
for each row begin
update recap_nilai set nilai = nilai - Old.nilai + New.nilai where name=Old.name
end;
///
Delimiter ;
Hope this helps !!