I have a simple posts table in MySQL which has a POST_ID as the SK (surrogate key).
Replies to the original post ID are stored in the same table in a PARENT_POST_ID column, but I want to perform the following logic:
BEFORE INSERT (I think ...)
IF a PARENT_POST_ID has not been defined on the INSERT, then default the row value to the newly generated POST_ID (from the auto-int sequence)
IF a PARENT_POST_ID has been defined on the INSERT, then set it to whatever has been passed.
Example
post_id | parent_post_id | date_time | message
12 12 2015-04-14 21:10 A new post (start of a thread)
13 12 2015-04-14 21:12 A reply to the post ID 12
The answer here: https://stackoverflow.com/a/11061766/1266457 looks like it might be what I need to do, although I am not sure what it's doing.
Thanks.
For before insert trigger you can not get the last inserted primary key , the other way of doing it is to get the max value from the table and increment it.
Here is a way to do it
delimiter //
create trigger posts_before_ins before insert on posts
for each row
begin
declare last_id int;
if new.parent_post_id is null then
select max(post_id) into last_id from posts ;
if last_id is null then
set new.parent_post_id = 1 ;
else
set new.parent_post_id = last_id+1 ;
end if ;
end if ;
end ;//
delimiter ;
So the trigger will check if there is no value of parent_post_id in the insert query it will get the max post_id. For the first entry it will be null so we are setting it as 1 i.e. and after that max post_id + 1 after each entry.
Here is a test case of this in mysql
mysql> select * from test ;
Empty set (0.00 sec)
mysql> delimiter //
mysql> create trigger test_is before insert on test
-> for each row
-> begin
-> declare last_id int;
-> if new.parent_id is null then
-> SELECT auto_increment into last_id
-> FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'test'
-> and TABLE_SCHEMA = 'test';
-> set new.parent_id = last_id ;
-> end if ;
-> end ;//
Query OK, 0 rows affected (0.12 sec)
mysql>
mysql> delimiter ;
mysql> insert into test (val) values ('aa');
Query OK, 1 row affected (0.10 sec)
mysql> insert into test (val) values ('bb');
Query OK, 1 row affected (0.04 sec)
mysql> select * from test ;
+---------+-----------+------+
| post_id | parent_id | val |
+---------+-----------+------+
| 1 | 1 | aa |
| 2 | 2 | bb |
+---------+-----------+------+
2 rows in set (0.00 sec)
Related
I'm using MySQL with PhpMyAdmin. I'm looking for a way to put a constraint on a table so that:
column1(int) + column2(int) + column3(int) <= 100
In mysql as of now you can not have such constraints in the table definition. You may use the application layer to validate this. If you want to have it using mysql then you can use trigger and raise error using signal
https://dev.mysql.com/doc/refman/5.5/en/signal.html
Here is how the trigger would look like
delimiter //
create trigger check_col_sum before insert on test
for each row
begin
if (new.column1+new.column2+new.column3) > 100 then
signal sqlstate '45000' set message_text = 'Sum of column1,column2 and column3 must be less than equal to 100';
end if;
end;//
delimiter ;
Test case
mysql> create table test (id int auto_increment primary key, column1 int, column2 int, column3 int);
Query OK, 0 rows affected (0.14 sec)
mysql> delimiter //
mysql> create trigger check_col_sum before insert on test
-> for each row
-> begin
-> if (new.column1+new.column2+new.column3) > 100 then
-> signal sqlstate '45000' set message_text = 'Sum of column1,column2 and column3 must be less than equal to 100';
-> end if;
-> end;//
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> delimiter ;
mysql> insert into test (column1,column2,column3) values (10,20,30);
Query OK, 1 row affected (0.08 sec)
mysql> select * from test ;
+----+---------+---------+---------+
| id | column1 | column2 | column3 |
+----+---------+---------+---------+
| 1 | 10 | 20 | 30 |
+----+---------+---------+---------+
1 row in set (0.00 sec)
mysql> insert into test (column1,column2,column3) values (10,20,80);
ERROR 1644 (45000): Sum of column1,column2 and column3 must be less than equal to 100
mysql> select * from test ;
+----+---------+---------+---------+
| id | column1 | column2 | column3 |
+----+---------+---------+---------+
| 1 | 10 | 20 | 30 |
+----+---------+---------+---------+
1 row in set (0.00 sec)
You can use New feature of MySQL5.7 that is Generated columns, there you can define the sum of two columns in third column while creating table or can add the column by ALTER command. Example
CREATE TABLE triangle(
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb))
);
Refer the link:
MySQL Doc
I tried from several examples and could not reach my goal - so I am asking here...
I have a table :
table1
columns : col1 , col2
I want to change col2 after an insert :
If (col2 = 0) then
alter col2 to be max(col2) + 1
else
leave the value as it is
Thanks
You need to do it using before insert since before insert you can check if the col2 is 0 and then set the value to max col2 + 1
Here is a way to do it
delimiter //
create trigger test_ins before insert on table1
for each row
begin
declare max_col2 int;
if new.col2 = 0 then
set max_col2 = (select max(col2) from table1);
end if ;
if max_col2 is null then
set new.col2 = 1 ;
else
set new.col2 = max_col2 + 1 ;
end if ;
end ;//
delimiter ;
Note that in the above trigger I have added a check such that if the max(col2) is null which will happen when you add the first record in the table and col2 as 0 I am setting it as 1
if max_col2 is null then
set new.col2 = 1 ;
You can set it as you want.
Here is a test case
mysql> create table table1 (col1 int, col2 int);
Query OK, 0 rows affected (0.13 sec)
mysql> delimiter //
mysql> create trigger test_ins before insert on table1
-> for each row
-> begin
-> declare max_col2 int;
-> if new.col2 = 0 then
-> set max_col2 = (select max(col2) from table1);
-> end if ;
-> if max_col2 is null then
-> set new.col2 = 1 ;
-> else
-> set new.col2 = max_col2 + 1 ;
-> end if ;
-> end ;//
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> insert into table1 values (1,0);
Query OK, 1 row affected (0.04 sec)
mysql> select * from table1;
+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
mysql> insert into table1 values (2,0);
Query OK, 1 row affected (0.04 sec)
mysql> select * from table1;
+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
| 2 | 2 |
+------+------+
2 rows in set (0.00 sec)
Again - thanks for your reply.
However, I tested it and it seems :
Not to add + 1 to the next value (and I really do not know why it is like that).
Leave the value if it is defined (I wrote above: "else leave the value as it is")
I changed your code a bit and put some extra IFs.
Now it is working OK (for me).
DELIMITER //
CREATE TRIGGER trigCF7Submits
BEFORE INSERT ON wp_cf7dbplugin_submits
FOR EACH ROW
BEGIN
DECLARE nMaxInternalNumber INT;
IF ((new.form_name='ActionForm') AND (new.field_name='txtActionFormInternalNumber')) then
IF (new.field_value = 0) THEN
SET nMaxInternalNumber = (SELECT max(field_value) FROM wp_cf7dbplugin_submits WHERE ((form_name='ActionForm') AND (field_name='txtActionFormInternalNumber')));
IF (nMaxInternalNumber IS NULL) THEN
SET nMaxInternalNumber = 0;
END IF;
SET nMaxInternalNumber = nMaxInternalNumber + 1;
ELSE
SET nMaxInternalNumber = new.field_value;
END IF;
SET new.field_value = nMaxInternalNumber ;
END IF ;
END ;//
DELIMITER ;
Why is the SELECT statement inside the WHILE loop only returning value for the first iteration ?
Both of the INSERT IGNORE and the second INSERT is working, and are inserting rows equal to amount.
If I set amount to 10, I only get the results from the first inserted row. However, the procedure will INSERT amount rows to rand_strings and rand_strings_info tables.
The Procedure:
DROP PROCEDURE if exists test_while;
DELIMITER $$
CREATE PROCEDURE test_while(amount INT, description VARCHAR(255))
BEGIN
WHILE amount > 0 DO
INSERT IGNORE INTO rand_strings(rand_string) /*WORKS EVERY ITERATION*/
SELECT generate_rand_string(); /*function to generate a random string.*/
SELECT * FROM rand_strings WHERE id = LAST_INSERT_ID(); /*ONLY WORKS FIRST TIME */
INSERT INTO rand_strings_info(id, col2, col3) /*WORKS EVERY ITERATION*/
VALUES (LAST_INSERT_ID(), now(), description);
SET amount = amount - 1;
END WHILE;
END$$
DELIMITER ;
CALL test_while(10, 'This is the description of the string…')
RESULTS:
id | rand_string
1 | jgdlkjaht
Some interfaces do not show all the results as expected, but the code runs correctly.
You can see in the following SQL Fiddle that only shows the first record in the rand_strings table when stored procedure runs, but running the same code on the MySQL command line the result is as follows:
mysql> CALL `test_while`(5, 'This is the description of the string...');
+----+------------------------------------------+
| id | rand_string |
+----+------------------------------------------+
| 1 | f4c77a3155d95ad1e818b1b06a62deec8e0b6754 |
+----+------------------------------------------+
1 row in set (0.00 sec)
+----+------------------------------------------+
| id | rand_string |
+----+------------------------------------------+
| 2 | 7cbcca49596262836f5af91643303d10b3804900 |
+----+------------------------------------------+
1 row in set (0.00 sec)
+----+------------------------------------------+
| id | rand_string |
+----+------------------------------------------+
| 3 | 2ba2c7276c0b66e3dcbb971b7f54af9bced578a4 |
+----+------------------------------------------+
1 row in set (0.00 sec)
+----+------------------------------------------+
| id | rand_string |
+----+------------------------------------------+
| 4 | d52426b19a59c515b02268347c508383873d4d73 |
+----+------------------------------------------+
1 row in set (0.00 sec)
+----+------------------------------------------+
| id | rand_string |
+----+------------------------------------------+
| 5 | fbc25c6204b609e8f4f4f8a33b534bff9a011e5f |
+----+------------------------------------------+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
UPDATE
DELIMITER $$
CREATE PROCEDURE `test_while`(`amount` INT, `description` VARCHAR(255))
BEGIN
DECLARE `_LAST_INSERT_ID`, `_first_inserted_in_this_run` INT UNSIGNED DEFAULT NULL;
CREATE TEMPORARY TABLE IF NOT EXISTS `temp_generate_rand_string` (
`insert_id` INT UNSIGNED PRIMARY KEY,
`first_inserted_in_this_run` INT UNSIGNED
) ENGINE=MEMORY;
WHILE `amount` > 0 DO
INSERT IGNORE INTO `rand_strings`(`rand_string`) /*WORKS EVERY ITERATION*/
SELECT `generate_rand_string`(); /*function to generate a random string.*/
SET `_LAST_INSERT_ID` := LAST_INSERT_ID();
IF (`_first_inserted_in_this_run` IS NULL) THEN
SET `_first_inserted_in_this_run` := `_LAST_INSERT_ID`;
END IF;
INSERT INTO `temp_generate_rand_string` (`insert_id`, `first_inserted_in_this_run`)
VALUES
(`_LAST_INSERT_ID`, `_first_inserted_in_this_run`);
-- SELECT * FROM `rand_strings` WHERE `id` = `_LAST_INSERT_ID`; /*ONLY WORKS FIRST TIME */
INSERT INTO `rand_strings_info`(`id`, `col2`, `col3`) /*WORKS EVERY ITERATION*/
VALUES (`_LAST_INSERT_ID`, NOW(), `description`);
SET `amount` := `amount` - 1;
END WHILE;
SELECT `rs`.`id`, `rs`.`rand_string`
FROM `rand_strings` `rs`
INNER JOIN `temp_generate_rand_string` `tgrs` ON
`tgrs`.`first_inserted_in_this_run` = `_first_inserted_in_this_run` AND
`rs`.`id` = `tgrs`.`insert_id`;
DELETE
FROM `temp_generate_rand_string`
WHERE `first_inserted_in_this_run` = `_first_inserted_in_this_run`;
END$$
DELIMITER ;
SQL Fiddle demo
Im attempting to have MySQL automatically insert data into another table after insert into one. I know to do this required Triggers and potentially Routines. I have a couple I've been trying to modify to do what I wish to accomplish but I appear to be hitting a dead end due to lack of experience, so help is greatly appreciated.
The table that has data inserted (db_tou_tracking):
tou_tracking_ID ICP_ID tou_tracking_start tou_tracking_units
----------------------------------------------------------------
2 2 2013-03-01 10.77
3 2 2013-03-01 11.00
There are a couple of other columns here, that separate out by time, but I'm interested by day, rather than time.
Table data should go into compounded. So as each of the above rows are inserted, it will either create a new row if the tou_tracking_start and ICP_ID do not exist, or update the existing row.
tou_tracking_daily_ID ICP_ID tou_tracking_start tou_tracking_units
------------------------------------------------------------------------------
1 2 2013-03-01 21.77
2 2 2013-03-02 25.36
Below is my Tigger (no errors when setup on MySQL, and it does appear to call when data is attempted to be inserted):
BEGIN
DECLARE presentcount INT;
SET presentcount = (SELECT count(*) FROM db_tou_tracking_daily WHERE tou_tracking_daily_day =
(SELECT tou_tracking_start FROM db_tou_tracking WHERE ICP_ID = db_tou_tracking_daily.ICP_ID ORDER BY tou_tracking_ID DESC)
);
IF (presentcount = 0) THEN
INSERT INTO db_tou_tracking_daily (ICP_ID, tou_tracking_daily_day, tou_tracking_start)
SELECT NEW.ICP_ID, NEW.tou_tracking_start, NEW.tou_tracking_units, calculate_units(NEW.ICP_ID, NEW.tou_tracking_start);
ELSE
UPDATE db_tou_tracking_daily SET tou_tracking_daily_units = calculate_units(NEW.ICP_ID, tou_tracking_daily_day)
WHERE ICP_ID = NEW.ICP_ID AND tou_tracking_daily_day = NEW.tou_tracking_start;
END IF;
END
and then the routine it calls to calculate units.
CREATE DEFINER=`root`#`localhost` FUNCTION `calculate_units`(ICP_ID INT, tou_tracking_daily_day DATE) RETURNS float
BEGIN
DECLARE units FLOAT;
DECLARE last_time DATE;
DECLARE last_watts INT;
DECLARE this_time DATETIME;
DECLARE this_watts INT;
DECLARE loop_done INT;
DECLARE curs CURSOR FOR
SELECT tou_tracking_timestart, tou_tracking_units FROM db_tou_tracking WHERE ICP_ID = ICP_ID AND tou_tracking_start = tou_tracking_daily_day ORDER BY tou_tracking_start DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET loop_done = 1;
SET last_time = (SELECT max(tou_tracking_start) FROM db_tou_tracking WHERE ICP_ID = ICP_ID AND tou_tracking_start < tou_tracking_daily_day);
SET last_watts = (SELECT tou_tracking_units FROM db_tou_tracking WHERE ICP_ID = ICP_ID AND tou_tracking_start = last_time);
SET last_time = CAST(tou_tracking_start AS DATETIME);
SET loop_done = 0;
SET units = 0;
OPEN curs;
REPEAT
FETCH curs INTO this_time, this_watts;
IF last_watts IS NOT NULL THEN
SET units = units + (last_watts + this_watts);
END IF;
SET last_watts = this_watts;
SET last_time = this_time;
UNTIL loop_done END REPEAT;
CLOSE curs;
END
The routine throws back an error on line 3 when I try to run the SQL to setup the routine, but I can't see anything obviously wrong, but I'm not exactly sure what I'd be looking for.
Any help with this is hugely appreciated and any pointers that can be given along the way. Thanks :)
Attempting to replicate your issue, I'm going to guess the error you get is probably because you're not using a DELIMITER.
Executing a similar function creation statement I get the same error, and a syntax parse suggests it's not expecting the delimiter ;.
The one that causes an error on line 3.
CREATE DEFINER = 'root'#'localhost' FUNCTION test_func(foo INT) RETURNS FLOAT
BEGIN
DECLARE bar FLOAT;
RETURN 1;
END
Fixing it using delimiters.
DELIMITER $$
CREATE DEFINER = 'root'#'localhost' FUNCTION test_func(foo INT) RETURNS FLOAT
BEGIN
DECLARE bar FLOAT;
RETURN 1;
END$$
DELIMITER ;
If this does not fix your problem, are you able to provide a self contained function that doesn't rely on any of your existing tables, that also produces the same error so it can be tested?
create table t1 ( start date not null, units decimal(5,2) not null );
create table t2 ( start date not null, units decimal(5,2) not null );
delimiter //
create trigger trg1
after insert on t1
for each row
begin
update t2
set units = units + new.units
where start = new.start;
if ROW_COUNT() = 0 then
insert into t2
(start, units)
values (new.start, new.units);
end if;
end //
delimiter ; //
mysql> select * from t1;
Empty set (0.01 sec)
mysql> select * from t2;
Empty set (0.00 sec)
mysql> insert into t1 (start, units) values ('2014-01-01',100.02);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------------+--------+
| start | units |
+------------+--------+
| 2014-01-01 | 100.02 |
+------------+--------+
1 row in set (0.00 sec)
mysql> select * from t2;
+------------+--------+
| start | units |
+------------+--------+
| 2014-01-01 | 100.02 |
+------------+--------+
1 row in set (0.00 sec)
mysql> insert into t1 (start, units) values ('2014-01-01',200.05);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------------+--------+
| start | units |
+------------+--------+
| 2014-01-01 | 100.02 |
| 2014-01-01 | 200.05 |
+------------+--------+
2 rows in set (0.01 sec)
mysql> select * from t2;
+------------+--------+
| start | units |
+------------+--------+
| 2014-01-01 | 300.07 |
+------------+--------+
1 row in set (0.01 sec)
I've created a trigger that resembles the following:
delimiter //
CREATE TRIGGER update_total_seconds_on_phone AFTER INSERT
ON cdr
FOR EACH ROW
BEGIN
IF NEW.billsec > 0 AND NEW.userfield <> NULL THEN
UPDATE foo
SET total_seconds = total_seconds + NEW.billsec
WHERE phone_id = NEW.userfield;
END IF;
END;//
It seems to go through okay. However it doesn't appear to be invoking when I need it to. Here's an example:
mysql> select total_seconds from foo where phone_id = 1;
+---------------+
| total_seconds |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql> insert into cdr (billsec, userfield) VALUES(60, 1);
Query OK, 1 row affected, 12 warnings (0.00 sec)
mysql> select total_seconds from foo where phone_id = 1;
+---------------+
| total_seconds |
+---------------+
| 0 |
+---------------+
EDIT: The warnings in this particular case do not affect the trigger. It's mostly additional columns which do not have default values in the cdr table that cause the warnings. For the sake of simplicity, I kept my INSERT statement brief.
There's 12 warnings generated. What are they?
ETA: Oh, wait... you need to use is not null rather than <> null. Any comparison with null will always return null.