MYSQL Stored procedure for concatenating code - mysql

I have a table which contain some fields(id,code,name,...), when I want to insert new record the code field should be like (ABC-001) and next record should be like (ABC-002) and so on.
I want to write a MYSQL procedure for this but I don't know how, any body has any idea? thanks in advance

First User Insert Query like:
Insert Into YourTable Values(..,'ABC',...);
Now write trigger after update which set Code value in expected format:
CREATE TRIGGER TriggerNmae AFTER INSERT ON YourTable
FOR EACH ROW
Begin
UPDATE YourTable
SET code = concat(code,'-',LPAD(NEW.id,3,'0'));
WHERE id = New.id ;
END;

Related

How to use trigger to update only one row in table with Mysql

I have an after insert trigger that is supposed to update the field total in my table "test" where the id_cart is equal to new.id_cart. However my trigger is updating every single row in the table not only the one desired. I would like to know how can I modify my trigger so it only updates the row that I want.
This is my trigger.
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW BEGIN
UPDATE test set total= (select sum(price_product) from test_product_quantity_cart where id_cart=new.id_cart);
END
So if the new row inserted in table "test_product_quantity_cart" has an new.id_cart=1, then only the row in table "test" with id_cart=1 should be uptated.
I think I am missing a "where" clause to indicate the update statement which rows it is suppossed to upate. However I do not know how to add that clause.
Thank you!
CREATE DEFINER=`root`#`localhost` TRIGGER `update_total_test`
AFTER INSERT ON `test_product_quantity_cart`
FOR EACH ROW
UPDATE test
JOIN ( SELECT id_cart, SUM(price_product) total
FROM test_product_quantity_cart
WHERE id_cart=NEW.id_cart ) value_for_update USING (id_cart)
SET test.total = value_for_update.total;

SQL trouble with insert in if statement

DROP PROCEDURE IF EXISTS kund2orderNew;
DELIMITER ;;
CREATE PROCEDURE kund2orderNew(kundId2 INT)
BEGIN
IF kundId2 <> (SELECT kundId FROM kund2order) THEN
INSERT INTO kundOrder VALUES ();
INSERT INTO kund2order VALUES (kundId2, (SELECT id FROM kundOrder));
END IF;
END
;;
DELIMITER ;
Alright am I doing something wrong here? What im trying to do is to check if kundId is in the kund2order, if its not then what I want to do is create a new row in the kundOrder table that just uses the default values and then take the recently created id from that row in the kundOrder and put it inside the new row in kund2order (together with kundId).
For some reason it just gives me (node:18328) UnhandledPromiseRejectionWarning: Error: ER_BAD_NULL_ERROR: Column 'kundId' cannot be null
I am a bit confused as to what the problem is, both tables are empty after I have called this procedure. Is the problem my if statement or is it something else?
That's not the correct way to check if an ID is already in the table. When you use a SELECT query as an expression, it has to return just one row. You can use:
IF NOT EXISTS (SELECT * FROM kund2Order WHERE kundId = kundId2) THEN
And if you want to insert the auto-increment of the row that was just inserted into kundOrder, you should use LAST_INSERT_ID():
INSERT INTO kund2order VALUES (kundId2, LAST_INSERT_ID());

How to update to other table when inserted into one table in mysql

I have two tables in mysql. One is create_load_test and another isload_test. I am inserting data into create_load_test table via html form. It is get updating.
There are 9 columns in create_load_test and 3 columns in load_test.
I want to update id and load_test column values of 'load_test' table, When a row is inserted in create_load_test table via html form.
How to do this? I know only basics of mysql.
Can you help me to solve this?
create_load_test:
load_test table:
try something like this using trigger.
CREATE TRIGGER `after_update_A` AFTER UPDATE ON `A` FOR EACH ROW
BEGIN
UPDATE TABLE B
SET username = NEW.username
, password = NEW.password
, email = NEW.email
WHERE id = NEW.id;
END $$
I have used like this, now it is working fine.
DELIMITER $$
CREATE TRIGGER triggered_from_createload
BEFORE insert ON create_load_test
FOR EACH ROW
BEGIN
INSERT INTO load_test
SET load_test.id = new.id,
load_test.loadtest = new.loadtest;
END$$
DELIMITER ;

How to automatically update database fields with random values in MYSQL

I understand how to fill a field with random values, but I need my field to automatically update with a random value for each new column. For some reason, I'm having trouble figuring out how to do this.
By the way, as it says above, this is for a database in MySQL.
Thanks everyone
You can use a trigger to update a specific colum after insert like this:
DELIMITER |
CREATE TRIGGER randtrigger AFTER INSERT ON your_table
FOR EACH ROW BEGIN
UPDATE your_table SET some_column = rand()
WHERE id = NEW.id;
END;
|
DELIMITER ;

Using columns from another table in mysql if statement

I wrote a trigger something like this:
CREATE TRIGGER `update_after_itemPresent` AFTER INSERT ON `bus_repair`
FOR EACH ROW begin
IF NEW.unit <> `item_present`.`unit` THEN
update item_present
set unit = unit-new.unit
where item_present.item_group_id = new.item_group_id;
END IF;
end
But when I insert new row in bus_repair table it gives an error that:
unknown table item_present in field list
Any idea how to fix this?
Move your UPDATE item_preset statement above the IF and the IF inside the UPDATE or rephrase as a condition. You need to UPDATE or SELECT the table item_present first.