mysql trigger: insert into another table if duplicate - mysql

This is my first time using mysql and I am tying to learn how to use trigger.
I have two tables: lpr_upload and lpr_traveltime.
my lpr_upload table look like this
+----------+---------+--------+-----------+
| date_time | site_id | lane_id| lpr_text |
+----------+---------+--------+-----------+
Whenever I add a new row into lpr_upload, i want to check if lpr_tex of the new row already exist or not in the table. If it already exist, i want to add a new row into lpr_traveltime with lpr_text and new datetime - old datetime. This is what i have so far:
insert into lpr_opt_travel_times(travel_time, lpr_text)
value (select TIMEDIFF (new.date_time, ...) , lpr_text) on duplicate key update ...
I dont know how to finish this. also, how would I get the date_time from the row that already exist in the table?

Try this:
declare ldt_date_time datetime;
select date_time into ldt_date_time from tbl where tbl.lpr_text = NEW.lpr_text and date_time <> NEW.date_time;
if ldt_date_time is not null then
insert into lpr_opt_travel_times(travel_time, lpr_text) values (ldt_date_time, NEW.lpr_text);
end if;
Declare a var with date time datatype as I assume the column date_time is datetime type.
Select from NEW magic table the value of date_time for the inserted lpr_text but whose date_time is different from the inserted one.
If the var is null meaning no assignment took place and the var retains the initial default value of NULL then insert the row in your other table using the var and the inserted lpr_text

Related

Update MySQL column when new date is greater than the existing one

I'm trying to update a column from a MySQL table so that it changes to current timestamp when the new date from another column is greater than the existing date from that column, and stays the same otherwise.
For example, let's say that I have a table with two columns, enddate and enddate_modif. If the date in enddate in the updating record is greater than the date in enddate in the existing table, I want enddate_modif to change to current timestamp. Otherwise, it keeps the same value.
How can I do it in MySQL?
Thank you in advance.
An after update trigger should do the trick.
Try:
CREATE TRIGGER `enddate_modif_trigger` BEFORE UPDATE ON `my_table`
FOR EACH ROW
SET NEW.enddate_modif = (SELECT CASE WHEN NEW.enddate > enddate_modif
THEN CURRENT_TIMESTAMP
ELSE NEW.enddate_modif
END
FROM my_table
);
I think you can use an after update trigger
something like this
if(New.enddate >Old.enddate ) then
update yourtable set enddate_modif=NOW() where Your_table_ID=NEW.Your_table_ID;
end iff;

MySQL create an integer column that auto-updates to increment value

I'm wondering if I can do this without a trigger:
I want to have an INT column which is auto-updated to increment the integer value.
This is similar but not the same as ON UPDATE CURRENT_TIMESTAMP on a TIMESTAMP or DATETIME column.
I read in the docs it's not possible with ON UPDATE because I can only use literal values unless the column is a TIMESTAMP.
MySQL default and on update
MySQL timestamp initialization
Can I do something else instead of a trigger?
[UPDATE 1]
This example is what I'm trying to achieve:
CREATE TABLE myTable (
stuff VARCHAR (100),
special_col INT DEFAULT 123456789
);
INSERT INTO myTable (stuff) VALUES ('something or other');
SELECT * FROM myTable;
stuff | special_col
----------------------------------------------------
something or other | 123456789
UPDATE myTable SET stuff = 'new info';
SELECT * FROM myTable;
stuff | special_col
----------------------------------------------------
new info | 123456790
The value that the column increments by doesn't have to be 1. It just has to be higher than before.
[UPDATE 2]
I thought there might be some cool mySQL syntax I could use - it does so much non-ANSI SQL, I figured there might be a custom way of doing it.
Anyway, I have given up and I'm implementing a trigger:
CREATE TRIGGER trg_data_update BEFORE UPDATE ON myTable
FOR EACH ROW SET NEW.special_col = OLD.special_col + 1;
Add one to the special_col in the UPDATE as per docs
UPDATE myTable SET stuff = 'new info', special_col=special_col+1;

Auto-store datetime() value in select insert query mysql

need an advice, how to auto-store datetime value for my historyActivity table in select insert mysql query. This is an example:
INSERT INTO history_sequence(CODE, LAST_MOUNTH, LAST_VALUE) SELECT CODE, MOUNTH, VALUE FROM seq WHERE CODE = CODEVALUE
i just want to add datetime to see time when the data inserted. Need help please
You can do this in the MySQL table definition:
ALTER TABLE history_sequence ADD inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
When records are inserted into the table table, the inserted column gets automatically populated with the current timestamp.

creating triggers and functions in postgresql

how can i create a trigger function before adding/updating,the function should check records that have the same id (i.e comparison by id with existing objects that have the same property as the temporary_object)If a record with the id is found, then that entry is set to the time_dead, and then it adds an entry containing the corresponding values ​​of the attributes found in that record (except those that are set for a new record), when time_dead is empty then time_create of a new time is equal to that time at the current moment . Thus,a new record time_create is like the time_dead's ancestor.
If a record with that id is found then it is added to the database with the establishment as the time_create of the current time.
for example here is a simple explanation(just for explanation purposes)
id time_create time-dead student amount
1 06.12 07.12 henry 500
1 07.12 henry 1000
so if a student called henry with id 1 entered a room at 06.12 and left at 07.12 the next time he enters another room again time_dead will be equal to time_create(so time_dead of old entry and time_create of new entry - will be equal)
these are my tables below in sql format
CREATE TABLE temporary_object
(
id integer NOT NULL,
time_create timestamp without time zone NOT NULL,
time_dead timestamp without time zone,
CONSTRAINT temporary_object_pkey PRIMARY KEY (id, time_create)
)
CREATE TABLE persons
(
fname text,
fsurname text,
)
INHERITS (temporary_object)
CREATE TABLE rooms
(
roomnum integer,
course integer,
passport text,
students_number text
)
INHERITS (temporary_object)
this is what i am trying to do but im afraid i do not know how to finish it but im 100% not right may some help out
CREATE TRIGGER trigger2
BEFORE INSERT OR UPDATE
ON persons
FOR EACH ROW
EXECUTE PROCEDURE func1();
and this is the function
CREATE OR REPLACE FUNCTION func1() RETURNS TRIGGER AS $persons$
DECLARE
time_create integer;
time_dead timestamp;
id timestamp;
BEGIN
IF (TG_OP = 'INSERT') THEN
time_create=
I can't tell you what I'm missing from your question, but I try to answer what I think I understood.
Row level triggers can access the version of the row affected with the NEW and OLD variables (depending on TG_OP). In this case, you can use NEW:
CREATE OR REPLACE FUNCTION func1()
RETURNS TRIGGER AS
$persons$
DECLARE
i integer;
BEGIN
IF TG_OP = 'INSERT'
THEN
UPDATE persons
SET time_dead = NEW.time_create
WHERE
id = NEW.id -- we are looking for the same ID
AND time_dead IS NULL
;
ELSE -- UPDATE
-- do here something
END IF;
END;
$persons$
LANGUAGE plpgsql;
This is only a starter, modify it to your needs.

SQL - Creating a Table with Two Timestamp Column with No Default Value

I want to create a table with two column, one "created" and one "modified", with a timestamp type. I don't want them to have a default value. But when I create them, it automatically puts CURRENT_TIMESTAMP as default for at least one of them, and also on update.
How can I just have type timestamp with no default or on update value for any of my column?
Edit: I use MySQL
If you are using SQL Server, the timestamp type is used for row versioning, not for storing an actual date or time value. See MSDN.
You could create the columns with a datetime datatype and then set your created and modified values with triggers.
CREATE TRIGGER trg_SetDateCreated ON MyTable
FOR INSERT AS
UPDATE MyTable
SET created = CURRENT_TIMESTAMP
WHERE MyTable.id = (SELECT Id FROM Inserted);
GO
CREATE TRIGGER trg_SetDateModified ON MyTable
FOR UPDATE AS
UPDATE MyTable
SET modified = CURRENT_TIMESTAMP
WHERE MyTable.id = (SELECT Id FROM Inserted);
GO