I have a MySQL table like the following
ID - Project - Entry - Value
int int int varchar
ID is AUTO_INCREMENT. Now I want Entry to be AUTO_INCREMENT according to Project. Meaning for each record/row with Project=1, ENTRY is auto-incrementing. The requirement is that ENTRY is always + 1 according to the project.
Any ideas?
You need to write an BEFORE INSERT trigger to manage auto increment for project id, e.g.:
DELIMITER //
CREATE TRIGGER entry_trigger
BEFORE INSERT
ON table FOR EACH ROW
BEGIN
DECLARE entry int;
SELECT IFNULL(MAX(Entry), 0) + 1 INTO entry WHERE project = NEW.project;
SET NEW.Entry = entry;
END; //
DELIMITER ;
You can use mysql Triggers. A trigger is basically an SQL code executes just before or just after an INSERT, UPDATE or DELETE event occurs on a database table.
Using triggers, you can update the Entry field based on the value of the Project field.
Related
This is a continuation of this question: Insert data into a table with a foreign key SQL
I need to create a trigger that updates a counter variable after I insert a row into a table. The counter variable keeps track of how many new rows are inserted into the ItemBook table. However, after running the script, the trigger doesn't seem to be working. Also how can I reuse this trigger if I have created it already?
CREATE TABLE count (
countBook INT DEFAULT 0 NOT NULL,
);
CREATE
TRIGGER count_trigger AFTER INSERT
ON ItemBook
FOR EACH ROW
UPDATE count
SET countBook = (SELECT COUNT(*) FROM Itembook)
INSERT INTO Item
VALUES('Clippers','amazon.com', 'hair clippers');
SET SQL_SAFE_UPDATES = 0;
INSERT INTO ItemBook
VALUES('Clippers','Bob')
SET SQL_SAFE_UPDATES = 1;
SELECT * FROM count;
The countBook column returns nothing after running the script. Furthermore, if I try to rerun this script again it will say Trigger already exists (after creating it). How do I reuse this trigger?
The trigger would look something like this:
CREATE TRIGGER count_trigger AFTER INSERT ON ItemBook
FOR EACH ROW
BEGIN
UPDATE count
SET countBook = countBook + 1;
END;
It seems very curious to have a table with one row for this information. But this appear to be what you are trying to do.
I have an existing table with values already filled in. Last column is of type date.
My requirement is that if any value is updated, that row's corresponding date column should be automatically updated to the current date.
For example, if the table is as follows:
and if I change any of the values of col1 (78) /col2 (nvvb) /col3 (566) of say row no. 2 on date 18Nov2018, then the col4 of row 2 should automatically update to today's date of 18Nov2018 from existing 13-Oct-17.
I've checked existing topics by could not find an answer.
The closest I can find is this code, but it is not working for the above requirement:
create table if not exists my_table (
index1 char(32) not null primary key,
title varchar(50),
my_timestamp timestamp not null default current_timestamp on update current_timestamp
)
Any help will be greatly appreciated.
Thanks
First, you need to create code blocks for whatever code you are displaying in your question/answer by highlighting the lines of code and pressing ctrl-k
You will need a trigger that changes the date value in the row to the current date using the CURDATE() function. BEFORE UPDATE simply means apply the current block enclosed in "BEGIN... END" before the actual update call is made by MySQL. You access the row to be updated with NEW.<field>. To store a value in a particular tuple, use the SELECT ... INTO .... statement
DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE UPDATE ON table_name
FOR EACH ROW
BEGIN
SELECT CURDATE() INTO NEW.`col4(date)`;
END $$
DELIMITER ;
For more info, see: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
https://dev.mysql.com/doc/refman/8.0/en/ansi-diff-select-into-table.html
UPDATE: YOU CANNOT UPDATE WITHIN AN UPDATE TRIGGER, MySQL does not prevent an update trigger from being called by itself, nor does it prevent deadlock issues, so you cannot use this. Must use the method below.
Using the method described in the comments (if your version supports it):
CREATE TABLE last_mod (
last_modified_date DATE NOT NULL,
PRIMARY KEY (last_modified_date)
)
CREATE TABLE IF NOT EXISTS my_table (
my_date DATETIME NOT NULL,
INDEX par (my_date),
FOREIGN KEY (my_date) REFERENCES last_mod(mast_modified_date) ON UPDATE CASCADE
);
DELIMITER $$
CREATE TRIGGER trigger_name
BEFORE UPDATE ON my_table
FOR EACH ROW
BEGIN
UPDATE last_mod SET last_modified_date = CURDATE();
END $$
DELIMITER ;
While creating threadsafe sequence in mysql, I came across mysql documentation - https://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id, suggesting to create sequence like this -
1. mysql> CREATE TABLE sequence (id INT NOT NULL);
2. mysql> INSERT INTO sequence VALUES (0);
3. mysql> UPDATE sequence SET id=LAST_INSERT_ID(id+1);
4. mysql> SELECT LAST_INSERT_ID();
My question is instead of step 4, why can't i query sequence table directly to get the id every time i need to fetch a new id? Like this - SELECT id FROM sequence; Is there any downside of directly querying the sequence table vs LAST_INSERT_ID as suggested in docs?
My mysql implementation looks like this -
CREATE TABLE sequence (id INT NOT NULL);
INSERT INTO sequence VALUES (0);
DELIMITER //
CREATE PROCEDURE nextVal(OUT nextval INT)
BEGIN
UPDATE sequence SET id=LAST_INSERT_ID(id+1);
SELECT id into nextval FROM sequence;
END //
DELIMITER ;
And to generate new id, i can use this
call nextVal(#output1);
select #output1;
Edit1: Updating sequence creation process, to be lock free, after talking to all folks that replied. I have also made the table more generic to accommodate multiple sequences in single table and used function instead of procedure
CREATE TABLE sequences (
name CHAR(20),
id BIGINT DEFAULT 1,
increment TINYINT,
UNIQUE KEY(name)
);
/* Call nextval('seqname'), and it returns the next value. */
/* If the named sequence does not yet exist, it is created with initial value 1 and increment 1 */
DELIMITER //
CREATE FUNCTION nextval (seqname CHAR(20))
RETURNS BIGINT
BEGIN
INSERT INTO sequences(name,id,increment) VALUES (seqname,LAST_INSERT_ID(1),1)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id+increment);
RETURN LAST_INSERT_ID();
END
//
/* Testing */
SELECT nextval('seq1');
SELECT nextval('seq2');
insert into sequences(name,id,increment) values ('seq3', 1000, 5);
SELECT nextval('seq3');
Your procedure won't work if another client runs it at the same time, because they're both updating the same row of the sequence table. You'd need to run both the UPDATE and SELECT in a transaction to prevent overlap.
LAST_INSERT_ID() is managed on a per-connection basis, so each client gets their own sequence, without having to lock each other out with transactions.
I'm running a wateranalyzer and want to save the sensor-data in a MariaDB.
I split the data into 2 tables: one for the automated part and one table which stores data I enter manually:
Tables:
I'm having a hard time (with just basic knowledge about databases) to figure out how I can "bind" ID and DateTime from one table to the other one, so if manual data is added, ID is incremented by 1 and the actual Date and Time is set in DateTime.
I bet I can do this somehow in PHPmyadmin?
thanks for your time!
using triger. this example for you.
DELIMITER //
CREATE TRIGGER contacts_after_insert
AFTER INSERT
ON contacts FOR EACH ROW
BEGIN
DECLARE vUser varchar(50);
-- Find username of person performing the INSERT into table
SELECT USER() INTO vUser;
-- Insert record into audit table
INSERT INTO contacts_audit
( contact_id,
deleted_date,
deleted_by)
VALUES
( NEW.contact_id,
SYSDATE(),
vUser );
END; //
DELIMITER ;
Is it anything more complex than having an ID in Wasser that matches the other ID? That is, first insert into Luft, then get the id and only then, INSERT INTO Wasser....
(A Trigger seems unnecessarily complicated.)
As Rick Suggested, you need to have an ID column in the second table that references ID in first table. Trigger is a better option if the process of getting the ID and inserting it along with other columns (pH, Redox...) into the second table is complicated.
Make ID in the second table as a foreign key to the ID in first table.
In my schema I had two columns ,I am new to MySQL how to concatenate columns using Computed Column in mysql
ordertbl schema
Autoorderid int auto-increment
Orderid varchar(45)
I want to value orderid value insert when new row inserted
i had created the trigger for but this trigger is not firing when i am inserting the row
insert into order_master(customerID,ItemID,quantity,Unitprice,Total) values ('07961A','1000',6,5,30)
use ntc_sales; DELIMITER
$$ CREATE TRIGGER order_master_BINS BEFORE INSERT ON order_master FOR EACH ROW
SET NEW.OrderID = CONCAT('ORD' ,NEW.AutoOrderID) ; end;
create table
enter code here
CREATE TABLE Demo(ID INT PRIMARY KEY,
IDwithChar AS 'ORD' + RIGHT('0000000' + CAST(ID AS VARCHAR(10)), 6) PERSISTED,valuevar nvarchar(45)
)
This is showing syntax error in mysql how to create table as above schema in mysql
You can't really do this using a trigger in MySQL.
A BEFORE trigger is run before the auto increment value is set, so NEW.AutoOrderID will always be 0.
An AFTER trigger cannot update the same table as it is triggered on.
In other words, instead of triggering on an INSERT, creating the row using a stored procedure that does the insert/update in a single transaction is probably the best way to proceed.
EDIT: You could use a separate table for the sequence, but that would not guarantee that the order number is the same value as AutoOrderID, just a unique value.
CREATE TABLE order_sequence (
seq INT PRIMARY KEY AUTO_INCREMENT
);
CREATE TRIGGER order_master_BINS BEFORE INSERT ON order_master
FOR EACH ROW
BEGIN
INSERT INTO order_sequence VALUES (0);
SET NEW.OrderID = CONCAT('ORD', LAST_INSERT_ID());
END
An SQLfiddle to test the alternate solution with.