Compilation Error in MYSQL trigger? - mysql

Table structure is:
mysql> DESC groups;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| PKey | varchar(64) | NO | PRI | NULL | |
| group_name | varchar(64) | YES | | NULL | |
| Region | varchar(128) | NO | | NULL | |
| Role | varchar(128) | NO | | NULL | |
| parent_group | varchar(64) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
When i am executing this Trigger , i'm having a compilation error
DELIMITER $$
CREATE
TRIGGER `group_before_delete` BEFORE DELETE
ON `groups`
FOR EACH ROW BEGIN
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!`;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.Pkey;
END IF;
END$$
DELIMITER ;
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 ';
ELSE
t_group=old.parent_group
t_group=old.PKey;
END IF;' at line 6
mysql> DELIMITER ;
Can you tell me what i'm missing here ??

You get an error in your code because the syntax of your UPDATE statement is not valid.
The links you give are 4 and 5 years old!
Since the SIGNAL statement is available in MySQL since version 5.5.0 (released 3 years ago), this is really not a good idea to use the hacks described in these 2 webpages. Instead, use the SIGNAL statement.
Note: From the comments we learn that the OP is not using MySQL 5.5, so SIGNAL is not available.

In your IF statement, the following is not a valid SQL statement:
UPDATE `Error: deletion RootGroup is prohibited!`;
This should be this:
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!` set x=1;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.PKey;
END IF;
I have never done things this way. It is a bit of an ugly way of doing it. But if it works, what the heck.

Related

MySQL throwing syntax error while trying to alter a table

I have been trying to alter a table to include a date column with default value of CURDATE() but MySQL is constantly throwing syntax error. Now, I have checked syntax for altering a table from several sources but I believe I do not have any syntax error. When I remove the default value part, the query runs fine but for some reason it cannot add a default value for the date column. I don't know why that is the case.
The code:
mysql> describe test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| col1 | int | YES | | 0 | |
| col2 | varchar(100) | YES | | hello | |
| col3 | varchar(5) | YES | | T | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> ALTER TABLE test ADD COLUMN col4 DATE DEFAULT CURDATE();
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 'CURDATE()' at line 1
mysql>
Edit: My MySQL version: 8.0.31
I think it has to be like this now:
ALTER TABLE test ADD COLUMN col4 DATE DEFAULT (CURRENT_DATE);
Note the parenthesis, or (curdate())

Update only the time stamp in a MySql table

Given this table
mysql> describe last_user_activity;
+--------------+------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+-------------------+-------+
| customer_id | int(11) | NO | | NULL | |
| token | text | NO | | NULL | |
| time_stamp | timestamp | YES | | CURRENT_TIMESTAMP | |
| is_logged_in | tinyint(1) | NO | | 0 | |
+--------------+------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)
I want to "touch" a row of the table, setting the time_stamp to its default, CURRENT_TIMESTAMP.
I thought that I could try
UPDATE last_user_activity WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
(that is a valid token in the table), but that resulted in
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 'WHERE
token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"' at line 1
Oh, well, it seemed clever, but NVM. So, then I tried
UPDATE last_user_activity SET token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
which said that it succeeded, BUT, the timestamp field was not updated.
What am I doing wrongly?
UPDATE last_user_activity
SET time_stamp = NULL
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
OR
UPDATE last_user_activity
SET time_stamp = NOW()
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
It looks as if you have given default value of column time_stamp, but not on update.
You may considering this change :
ALTER TABLE last_user_activity
MODIFY COLUMN time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
I hope this works.After that you could try using your update commands.

ERROR 1048 (23000) Column cannot be NULL, however I am inserting valid data

There are tons of these posts on Stack Overflow, however from the 20 or so that I looked at they were either coding errors faced when interfacing with MySQL (which I am not trying to do) or simply wanted null values but had their table defined incorrectly.
I am seeing an error in MySQL 5.6.19 where I have a column that is not allowed to have a null value. This is fine as it shouldn't have a null value. Here is the table desc below.
mysql> describe z;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| a | int(11) | NO | PRI | NULL | auto_increment |
| data | char(30) | NO | | NULL | |
| t | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
My problem is that I am inserting valid data....
mysql> insert into z (data, t) values('helloworld', sysdate());
ERROR 1048 (23000): Column 'data' cannot be null
There is one other piece of information that might be of some concern... or may not be.
I have a trigger and procedure that execute upon the implementation of inserts into this column. However I don't see that it should be a problem due to the trigger being activated after the insert statement completes.
Here is the trigger:
mysql> show triggers\G
*************************** 1. row ***************************
Trigger: insertuser
Event: INSERT
Table: z
Statement: begin
call triggerproc(sysdate(),user(),(select data from z where a = last_insert_id()));
end
Timing: AFTER
Created: NULL
sql_mode: NO_ENGINE_SUBSTITUTION
Definer: root#localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
And the Procedure:
mysql> show create procedure triggerproc\G
*************************** 1. row ***************************
Procedure: triggerproc
sql_mode: NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`#`localhost` PROCEDURE `triggerproc`(in a datetime, in b char(30), in c char(30))
begin
insert into record (t,u,data) values(a,b,c);
end
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
Just for good measure I will include the definition for the record table as well.
mysql> desc record;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| t | datetime | NO | | NULL | |
| u | char(30) | NO | | NULL | |
| data | char(30) | NO | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
I have looked through the MySQL reference manual for anything that could be of use, however it doesn't seem to have any details on this other than the standard error and to check that your column is not defined as not null... or I missed it...
In any case I would be greatly appreciative if anyone can help me out with finding out either the reason for this error or how I can go about finding the reason.
Thanks in advance.
EDIT: My question was answered wonderfully by TheConstructor he informed me that to grab new information from a column that was just inserted through a trigger that the NEW.column operator may be used. Furthermore he followed up with documentation that helps to understand this issue located at Trigger Syntax.
I only wonder why the trigger that I had wouldn't work with the insert statement even though it should activate after the previous statement, which makes me believe that it should (theoretically) work.
Reading the documentation on LAST_INSERT_ID() I would suggest that the value is only updated after the last trigger runs. I also created a trigger which inserts the result of LAST_INSERT_ID() into another table and it would always insert the id of the row inserted by the INSERT statement before or 0 if there was no previous INSERT.
From within an insert or update trigger you can always refer to the state after the statement by using NEW.column where column is a column-name of your table. See the documentation for examples

field list error - unknown column

I have this stored procedure:
DROP PROCEDURE IF EXISTS buildMySomething;
CREATE PROCEDURE buildMySomething()
BEGIN
UPDATE current_amount SET current_m_amount = 2 WHERE m_id = 1;
END //
This gives me the following error : ERROR 1054 (42S22): Unknowing column 'current_m_amount' in 'field list'
After looking around on the internet, it is apparent to me that people get this error if the column does not exist, an unexpected character, or simply a syntax error (they have typed the column name wrong)... however... i have checked these possibilities a countless number of times. what am i missing here?
+--------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------+------+-----+---------+----------------+
| m_id | int(11) | NO | PRI | NULL | auto_increment |
| current_m_amount | int(11) | NO | | NULL | |
+--------------------+---------+------+-----+---------+----------------+
I think it might be something to do with the delimiter in use. Try this, which executes on my test server:
DROP PROCEDURE IF EXISTS buildMySomething;
delimiter //
CREATE PROCEDURE buildMySomething()
BEGIN
UPDATE current_amount SET current_m_amount = 2 WHERE m_id = 1;
END //
delimiter ;

Mind writing my sql statement for me?

This kind of sad but I've been at it a while and I just can't seem to figure this statement out, google searches turn up similar questions but I haven't successfully applied the solutions.
I have a table of music, and every time I insert a song into it(each row is a song) I want to insert the song into a table of clean music if it is flagged as clean. I'm using mysql.
use music;
CREATE TRIGGER cache_clean_music BEFORE INSERT ON music
FOR EACH ROW
if new.clean then
insert into clean_music values (new.artist, new.album, new.song, new.filename, new.clean);
end if;
The Error I get is
ERROR 1064 (42000) at line 3: 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
and here is a description of the music table, the clean_music table is exactly the same
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| artist | varchar(100) | YES | | NULL | |
| album | varchar(100) | YES | | NULL | |
| song | varchar(100) | YES | | NULL | |
| filename | varchar(100) | YES | | NULL | |
| clean | tinyint(1) | YES | | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
+----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
If the two tables are identical (or almost), you probably do not need triggers (and all their mess) at all.
You may use a VIEW instead of a table (with duplicate data) for cache_clean_music:
CREATE VIEW cache_clean_music AS
SELECT artist
, album
, song
, filename ---- and possibly other fields you need
, id
FROM music
WHERE clean ;
Adding an index on music.clean would be a good idea in this case.
Does it help if you wrap a BEGIN...END around things?
CREATE TRIGGER ...
FOR EACH ROW BEGIN
IF ...
....
END IF;
END
The error - ERROR 1064 (42000) at line 3: 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 - means that the values in some of your input params isn't correct, perhaps there is some mismatching single quote. Can you display your query or the value in the NEW. variables?
Also, once you have fixed that error, your query will also return another error that "the column count doesn't match value count". And that will be because your table has 6 columns but your INSERT has only 5. Mention the columns in your INSERT query and it should be fine, like:
insert into clean_music (artist, album, song, filename, clean) values (new.artist, new.album, new.song, new.filename, new.clean);
Your clue is this
" check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4"
Do you know what version of mysql server you are running?
Did you check the manual to make sure that the command you have written is allowed in that version?