Setting Triggers in Maria DB - sql

I am trying to set the trigger in maria db
The query which I am running is
CREATE TRIGGER TEMP
AFTER INSERT
ON sample_details FOR EACH ROW
BEGIN
UPDATE SAMPLE_MONITOR SET TableDataCount= TableDataCount+ 1,Time=NOW() WHERE
TableName='sample_details';
END; //
delimiter ;
I have a table named 'sample_details' there If any insert is done to that table, I want to increment the count in the row present in SAMPLE_MONITOR table by 1 and also insert the current time
But I am getting the following error after running query
ERROR 1064 (42000): 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 1
So anyone know how to solve this?

Related

Getting a syntax error when declaring a table in SQL

New to SQL and trying to run this code and I get the 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 '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.

Trying to add a column to SQL Server table

I am getting incorrect syntax errors with the following statement (SQL Server 2017)
USE ToDo
GO
ALTER TABLE tasks ADD COLUMN completed TINYINT(1);
Started executing query at Line 1
Commands completed successfully.
20:31:38Started executing query at Line 3
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'COLUMN'.
A very similar syntax was used to remove a column that worked fine.
This code would run in MySQL - but not on SQL Server. Consider:
alter table tasks add completed tinyint;
Rationale:
alter table does not support column in SQL Server; just remove that keyword
the tinyint datatype does not take a length

Create Transaction on MariaDB to set expiry time for ip blacklist

The question I used to help build this transaction is here:
How to add 1 hour to currrent_timestamp in mysql which is the default value?
I'm trying to create a transaction in my database to fill in the allowed column in my mariadb database table blacklisted_ips. I want the allowed column to be an hour after my added column which has a default value of CURRENT_TIMESTAMP. Here is my transaction so far:
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips FOR EACH ROW BEGIN
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
END;
The error message I'm getting is the following:
#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 2
Here is my table schema:
CREATE TABLE blacklisted_ips (
ip_id int(11) NOT NULL AUTO_INCREMENT,
ip_add varchar(15) NOT NULL,
added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
allowed timestamp NOT NULL,
PRIMARY KEY (ip_id)
);
In MySQL, there's an ambiguity between the ; that terminates a CREATE TRIGGER statement, and the possible ; characters that terminate individual statements in the body of the trigger.
The error you got is confusing:
#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 2
Normally a syntax error includes the text of the statement following the position where the syntax parser got confused. But in your case, it got confused at ;, thinking it was the end of the CREATE TRIGGER statement. Therefore the error occurs at the termination; there is no text following the error as far as the parser is concerned.
The same issue affects CREATE PROCEDURE and CREATE FUNCTION.
To fix the ambiguity, MySQL client supports a builtin command to change the DELIMITER, so you can change it to something distinct from any sequence of characters that appear in the body of your routine.
DELIMITER ;;
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips
FOR EACH ROW BEGIN
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
END ;;
Alternatively, since in your case the trigger is a single-statement trigger, you don't need a BEGIN...END block at all. This way you can skip changing the DELIMITER, because the ; that terminates your CREATE TRIGGER is the same ; that terminates the single statement of the trigger.
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips
FOR EACH ROW
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
P.S.: This is documented with an example here: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

SQL query regarding Sequence

I made a sequence in Net Beans. But when I tried to insert data in the table I get an error.
My code is:
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen');
The error is:
[Exception, Error code 30,000, SQLState 42X04] Column
'SEQ_PERSON.NEXTVAL' is either not in any table in the FROM list or
appears within a join specification and is outside the scope of the
join specification or appears in a HAVING clause and is not in the
GROUP BY list. If this is a CREATE or ALTER TABLE statement then
'SEQ_PERSON.NEXTVAL' is not a column in the target table.
The error message Error code 30,000, SQLState 42X04 indicates you are using Derby DB not Oracle. That being the case you need to use Derby syntax for getting the next value. So your insert should look like this:
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (NEXT VALUE FOR seq_person,'Lars','Monsen')

Firebird IBOConsole SQL Drop Table if exist not working

I'm pretty new on Firebird.. trying to write a query that drops the table if exists through IBOConsole.
I have written the following sql statement,
EXECUTE block as
BEGIN
if (exists(
SELECT 1 FROM RDB$RELATIONS Where RDB$RELATION_NAME = 'ZZGTTUNIQUEID'))
then
execute statement 'DROP TABLE ZZGTTUNIQUEID';
END
but getting the following result..
ISC ERROR CODE:335544569
ISC ERROR MESSAGE:
Dynamic SQL Error
SQL error code = -104
Unexpected end of command - line 6, column 19
i'm not sure what might be wrong?
In IBOConsole I also experienced problems using the EXECUTE BLOCK statement, resulting in a 'Problem in BindingCursor' message and not executing the statement on the database. Use IBExpert's script executive or FlameRobin instead and it will work.