Unable to create a trigger in mysql hosted on amazon cloud - mysql

When am trying to create a simple trigger in mysql, am encountering the below error message. Please suggest me how to overcome this.
delimiter $$
create trigger trg_addresses_ins before insert on addresses
for each row
begin
declare msg varchar(128);
if length(new.addressstate) > 2 then
set msg = concat('MyTriggerError: Trying to insert a state value of more than 2 character: ', new.addressstate);
signal sqlstate '45000' set message_text = msg;
end if;
end$$
delimiter ;
`
Error Code: 1419. You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable) 0.078 sec
Super user is enabled but still get the same error and also am unable to change database parameter group associated with mysql aws db instance to 1. I am unable to modify db instance to select newly created group as the parameter group field is read only.
Appreciate your valuable inputs.
Thanks!

I guess you are using the default DB parameter group which you can not modify, the solution is you need to create your own parameter group, and set log_bin_trust_function_creators = 1, and apply your own parameter group to your current instance.

Related

the dbms assumes that a column name is a system variable inside the trigger

I have a table with three variables. one is the amout, the other is a restricting. Strict is the name of a column, but the system views it as a variable. It is not. It is not a reserved word either.
As this is an update, New is forbidden
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1193: Unknown system variable 'strict'
SQL Statement:
CREATE DEFINER = CURRENT_USER TRIGGER `bibliotech`.`patronus_AFTER_UPDATE` AFTER UPDATE ON `patronus` FOR EACH ROW
BEGIN
if amount > 5.65
then set strict =1;
elseif amount <5.65
then set strict =0;
end if;
ND

How to create a Trigger in a google cloud sql instance Database

I'm with a problem during creationg of a trigger in my database.
I've a MySQL Second Generation instance with a database (name: test) in my google cloud sql.
Right now i have multiple tables in my database and im trying to create a trigger in one of that tables using:
CREATE TRIGGER date_overlap_insert_start_date
BEFORE INSERT ON driver_operation
FOR EACH ROW
BEGIN
if exists(
select 1
from driver_operation
where nif = NEW.nif
and (NEW.start_date > start_Date and NEW.start_date < end_Date)) then
signal sqlstate '45000' SET MESSAGE_TEXT = 'Overlaps with existing data';
end if;
END;
The error i get is this:
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 '' at line 10
Is there anyone who can help me on this? This trigger is to prevent overlap in date fields.
With respect to Google Cloud Platform, an important thing to do is to go to your instance configuration, ie "Edit Configuration" then go to "Flags" and ensure that "log_bin_trust_function_creators" is set to on. This will solve issues in GCP making triggers, as well as allow you to create triggers in MySQL Workbench CE 8.0 without it crashing.
I had this exact same issue today. It is simply due the cloud sql instance viewing each semi colon as the end of a statement, so I can only assume it tries to execute each time it encounters one.
Try creating your trigger with:
/*!50003 CREATE*/ /*!50003 TRIGGER date_overlap_insert_start_date
BEFORE INSERT ON driver_operation
FOR EACH ROW
BEGIN
if exists(
select 1
from driver_operation
where nif = NEW.nif
and (NEW.start_date > start_Date and NEW.start_date < end_Date))
then
signal sqlstate '45000' SET MESSAGE_TEXT = 'Overlaps with
existing data';
end if;
END */
edited to fix my syntax. Was missing a semi-colon.

Cannot Create Trigger on MYSQL AWS RDS

I am trying to create a trigger on my database which is in a MYSQL AWS RDS instance.
When I try the code below:
DELIMITER $$
USE database_name $$
CREATE DEFINER=oldloginuser#localhost trigger newtrigger after update on db_col
for each row
begin
if new.hits != old.hits then
insert into log(id, access_time) values (new.id, now());
end if;
end
I get the following error:
Error Code: 1419. You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
How can I solve this issue?
You have to change the DB Parameter Group value.
you can change at RDS -> Select Database -> configuration -> DB Parameter Group -> click default parameter group you will be redirect to DB Parameter Group page -> create new DB Parameter Group with option
and reboot the db;

Protect column, disallow update, only allow insert if NULL in MySQL

I want to protect existing dates in a date column from being overwritten. So disallow updates to the date column and only allow inserts if the existing field value is NULL (date column default is NULL). Are triggers the only way to accomplish this in MySQL? If so, would the trigger below work?
create trigger date_check
before insert, update on date
for each row
begin
if(date IS NOT NULL) then
SIGNAL 'date already set'
end if ;
end ;
Background: I have a table with critical dates that was accidentally changed due to user error. I put some checks in the user interface to prevent this from happening again but want another layer of safety directly with the database if possible.
Yes, in MySQL triggers are the only way to do this. MySQL does not support constraints.
Your trigger is not exactly right. First, you have update on date, but this should be update on <table name>. Second, you are checking the date value used for the update. Perhaps you mean:
create trigger date_check_update
before update on <the table name goes here>
for each row
begin
if (old.date IS NOT NULL) then
SIGNAL 'date already set'
end if ;
end;
An insert trigger on this condition doesn't make sense.
If anyone like me stumble upon this thread and is getting syntax error, it's because "When you try to raise errors via SIGNAL you need to specify the SQLSTATE which is the error code and for the user defined generic error codes its 45000 along with the message text MESSAGE_TEXT"
So the SIGNAL line should look like this.
signal SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Your custom error message';
See this answer for more details.
https://stackoverflow.com/a/42827275/4164651
Just combining the above two answers, however, if you are writing triggers directly at the terminal, you'll have to change the delimiter before writing the trigger and then change it back once done.
delimiter $$
create trigger date_check_update
before update on <the table name goes here>
for each row
begin
if (old.date IS NOT NULL) then
signal SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Your custom error message';
end if ;
end $$
delimiter ;

super priviliege not set for master user in aws rds mysql

I have created an AWS RDS instance, I have created my master user with master password, and it is working/connecting fine.
But when I am going to create a function on that instance, it shows me the following error:
ERROR 1418: This function has none of DETERMINISTIC, NO SQL,
or READS SQL DATA in its declaration and binary logging is enabled
(you might want to use the less safe log_bin_trust_function_creator variable).
In my instance the variable log_bin_trust_function_creators shows OFF, and if I try to change the variable using SET GLOBAL log_bin_trust_function_creators = 1;, it gives me another error "Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation"
Set log_bin_trust_function_creators = 1 for Parameter group of the RDS instance.
Note: Default Parameter-Group is not editable. Create a new Parameter-Group and assign it to the RDS instance by modifying it from UI (AWS Admin Console) OR maybe using commands
DELIMITER $$
CREATE DEFINER=`DB_USERNAME_HERE`#`%` FUNCTION `GetDistance`(coordinate1 VARCHAR(120), coordinate2 VARCHAR(120)) RETURNS decimal(12,8)
READS SQL DATA
BEGIN
DECLARE distance DECIMAL(12,8);
/*Business logic goes here for the function*/
RETURN distance;
END $$
DELIMITER ;
Here, you have to replace DB_USERNAME_HERE with you RDS database username and function names according to you need.
Important thing is: DEFINER=`DB_USERNAME_HERE`#`%`
This was the problem I was facing after setting log_bin_trust_function_creators = 1 in parameter group. And it worked like a charm.
A better way is to apply your own parameter group, with log_bin_trust_function_creators set to true. (its false by default)
This happens when you try to create a procedure/function/view with a DEFINER that is not the current user.
To solve this remove or update the DEFINER clause.