I know this is against normalization rules, but how can I (if it is possible) set default value for column as a query, eg.:
ALTER TABLE Data
ALTER Sum SET DEFAULT SELECT Total*0.15 FROM Data;
Assuming to trigger on UPDATE:
DELIMITER $$
CREATE TRIGGER after_data
AFTER UPDATE ON Data
FOR EACH ROW BEGIN
UPDATE Data
SET NEW.Sum = (SELECT OLD.Total * 0.15)
END$$
DELIMITER ;
Related
I have three columns column 1, column 2, column 3(total). i want to put the sum of column 1 and column 2 into column 3 after every update query..... note i am using code igniter.. how can i do it if i update col 1 then total column automatically update itself.
Try like this:
DELIMITER $$
CREATE TRIGGER updtrigger AFTER UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.col1 <> OLD.col1 OR NEW.col2 <> OLD.col2 THEN
SET NEW.col3 = NEW.col1 + New.col2;
END IF;
END $$
Here is a very good tutorial.
An AFTER UPDATE Trigger means that MySQL will fire this trigger after
the UPDATE operation is executed.
why dont you just update the db instead of creating a trigger. plus youve created the trigger after the insert, it should go before. but really you shouldn't be doing it that way
try this code:
DELIMITER $$
CREATE
TRIGGER `db_name`.`trigger_name` AFTER UPDATE
ON `db_name`.`table_name`
FOR EACH ROW BEGIN
UPDATE
`table_name`
SET
`column3` = `column1` + `column2`;
END$$
DELIMITER ;
What is the postgres equivalent of the below mysql code
CREATE TABLE t1 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t2 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
As per Alex Brasetvik answer below, it seems i should go with triggers, my problem is i have a number of tables t1, t2... with created and modified fields, is it possible to write a generalized procedure?
--update
Almost ready
CREATE FUNCTION update_timestamp() RETURNS trigger AS $update_timestamp$
BEGIN
NEW.modified := current_timestamp;
RETURN NEW;
END;
$update_timestamp$ LANGUAGE plpgsql;
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t1
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t2
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
Just make sure all tables have the same columnname:
CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE TRIGGER t_name
BEFORE UPDATE
ON tablename
FOR EACH ROW
EXECUTE PROCEDURE upd_timestamp();
Thank you for the information Mithun and Alex Brasetvik.
I'd like to add one minor tweak to the trigger. Since we mostly likely want the modified column to store the timestamp when the row was last changed, not when it was the target of an UPDATE statement, we have to compare the new and the old value of the row. We update the modified column only if these two values differ.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
IF (NEW != OLD) THEN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END IF;
RETURN OLD;
END;
$$;
This trigger ensures that the modified column is updated only if the UPDATE operation actually changes the values stored in the row.
Update it with a trigger. Documentation and examples.
To further improve, the answer given by #Lauri Silvennoinen:
This trigger uses the WHEN clause as recommended by the official docs to check for changes in the row even before calling the specified function.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER tg_update_modified
AFTER UPDATE ON table_name
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE FUNCTION update_modified_timestamp();
I have a MySQL trigger using the BEFORE INSERT ON table that calculates a value and updates the same table after a user inserts values in specific columns. This works as expected. But a user makes a mistake in their entry and fixes their error and I want to write a trigger that will update the calculated value after the error has been fixed. Is there a way to achieve this?
A BEFORE UPDATE ON table trigger has access to the existing values in the row as well as newly supplied values, and can set the value of any column in the table, based on whatever conditions and expressions we want.
For example, it's possible to test whether the value of one or more columns of concern has been modified, and then set some other column to some expression.
DELIMITER $$
CREATE TRIGGER my_before_update_trigger
BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
IF NOT ((NEW.col1 <=> OLD.col1) AND (NEW.col2 <=> OLD.col2)) THEN
SET NEW.col3 = NEW.col1 * NEW.col2 ;
END IF;
END$$
DELIMITER ;
Hi guys I intend to create a Trigger for MySQL table so that after Values are Inserted into the table, the Column exp_sales is set to qnty_received value *selling_price but I am getting this error:
ERROR 1193 (HY000): Unknown system variable 'exp_sales'
This is My Query:
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
set exp_sales = qnty_received * selling_price;
END$$
What is the problem and what is the best way to create the trigger so that after teh Quantity and selling Price is Inserted, the field for exp_sales(Total) is Updated with the right value?
If I unserstand corrctly exp_sales is a column name you want to update:
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
update Store_info_table
set exp_sales = NEW.qnty_received * NEW.selling_price
where id = NEW.id;
END$$
better approach would be to use INSERT INTO ON DUPLICATE KEY UPDATE
INSERT INTO table_name(...)VALUES(...)
ON DUPLICATE KEY SET exp_sales = qnty_received * selling_price;
If I understood you right you want to update a field of your record after insert. Try
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
begin
UPDATE Store_info_table
SET exp_sales = NEW.qnty_received * NEW.selling_price
WHERE id = NEW.id;
END;
$$
Haven't you forgotten about the NEW keyword?
Try this one -
SET NEW.exp_sales = NEW.qnty_received * NEW.selling_price;
But, I think you should not do this, because you always can calculate 'total' value in SELECT query.
Try the following code, it works for me
create trigger tsales
after insert
on
Store_info_table
for each row
set new.exp_sales = new.qnty_received * new.selling_price;
but since you want the exp_sales calculated automatically, why would you not use before instead of after?
What is the postgres equivalent of the below mysql code
CREATE TABLE t1 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t2 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
As per Alex Brasetvik answer below, it seems i should go with triggers, my problem is i have a number of tables t1, t2... with created and modified fields, is it possible to write a generalized procedure?
--update
Almost ready
CREATE FUNCTION update_timestamp() RETURNS trigger AS $update_timestamp$
BEGIN
NEW.modified := current_timestamp;
RETURN NEW;
END;
$update_timestamp$ LANGUAGE plpgsql;
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t1
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t2
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
Just make sure all tables have the same columnname:
CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE TRIGGER t_name
BEFORE UPDATE
ON tablename
FOR EACH ROW
EXECUTE PROCEDURE upd_timestamp();
Thank you for the information Mithun and Alex Brasetvik.
I'd like to add one minor tweak to the trigger. Since we mostly likely want the modified column to store the timestamp when the row was last changed, not when it was the target of an UPDATE statement, we have to compare the new and the old value of the row. We update the modified column only if these two values differ.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
IF (NEW != OLD) THEN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END IF;
RETURN OLD;
END;
$$;
This trigger ensures that the modified column is updated only if the UPDATE operation actually changes the values stored in the row.
Update it with a trigger. Documentation and examples.
To further improve, the answer given by #Lauri Silvennoinen:
This trigger uses the WHEN clause as recommended by the official docs to check for changes in the row even before calling the specified function.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER tg_update_modified
AFTER UPDATE ON table_name
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE FUNCTION update_modified_timestamp();