FUNCTION does not exist error, trigger in MySQL - mysql

I am having this issue with my newly created database. I created my table and tried adding a prefix when adding data like it is shown in this question How to make MySQL table primary key auto increment with some prefix and came up with this trigger to use:
CREATE TRIGGER tg_contracts_insert BEFORE INSERT ON contracts
FOR EACH ROW
BEGIN
INSERT INTO contracts_seq VALUES (NULL);
SET NEW.cid = CONCAT('CN', LPAD(LAST_INSERT_CID(), 5, '0'));
END
cid being my primary key to be auto-inserted. I was apparently able to add the trigger However, when I tried adding my values on the main table, "contracts", with this:
INSERT INTO contracts (name, conres, conven, start, end, tipo) VALUES ('Roberto Grajales', 'Pedro', 'Pascual', '2022-12-12', '2022-08-08', 'Servicio');
name, conres, conven being VARCHAR type, start and end DATE and tipo being ENUM.
I get the following error message:
FUNCTION database.LAST_INSERT_CID does not exist
Anyone has any idea why and how to solve this?
Thanks!

There is no function LAST_INSERT_CID(). You probably meant LAST_INSERT_ID().

Related

Auto generate Primary Key if not present in MySQL INSERT Query

I created a table in MySql
CREATE TABLE newuser(id VARCHAR(10) PRIMARY KEY,sname VARCHAR(20));
When I INSERT record it works fine
INSERT INTO newuser VALUE('abc123','monika');
But sometimes I don't want to supply id in the INSERT query and sometimes I want to supply. In case I don't supply id MySql automatically generate one.
What can I do to get both below query works?
INSERT INTO newuser VALUE('abc123','monika');
INSERT INTO newuser VALUE('nikita');
'I don't understood ANYTHING' - very new then.
Firstly second insert statement is invalid please review https://dev.mysql.com/doc/refman/8.0/en/insert.html -
'If you do not specify a list of column names for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided by the VALUES list, SELECT statement, or TABLE statement.'
Secondly uuid is a function in which 'A UUID is designed as a number that is globally unique in space and time.' https://dev.mysql.com/doc/refman/8.0/en/insert.html
You can easily select uuid() to see what it produces.
You will need to increase the id size
If you wish to use it in an insert
insert into users values (uuid(),<sname>);

Using Trigger to update database when insert is used

Im looking for a way to use triggers to update a row in a different table.
The table im trying to get the info from is AI (Auto increment) and also the primary key of that table.
My problem is that i cant find a simple way of doing it.
My code:
DROP TRIGGER IF EXISTS Konsistens;
DELIMITER $$
CREATE TRIGGER Konsistens AFTER INSERT ON PersonData
FOR EACH ROW
BEGIN
INSERT INTO Bruker(BrukerNavn, Passord, PersonId)
VALUES('Bruker1', 'pw1', LAST_INSERT_ID(PersonData.PersonID));
END
All i want is for PersonID to be given a value inside the Bruker table.
Since PersonID is a foreign key from Person to Bruker it doesnt seem to exist when i try to get the value from it. When i use this trigger it also creates a bug where i can no longer insert values into PersonData.
My desired result is that when you insert something into PersonData you get a new row into Bruker which have the same PersonID as the new entry in PersonData.
You just reference recenty inserted row by NEW
CREATE TRIGGER Konsistens
AFTER INSERT ON PersonData
FOR EACH ROW
INSERT INTO Bruker (BrukerNavn, Passord, PersonId)
VALUES(NEW.BrukerNavn, NEW.Passord, NEW.PersonId);

"INSERT IF NOT EXISTS", ELSE UPDATE certain columns in MySQL

I'm using MySQL Workbench (6.3) and I'm trying to create a stored procedure with a specific "INSERT IF NOT EXSISTS" but I think I don't do it well. By the way, I did read this topic http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html and tried both examples, it didn't work. Here is the statement :
CREATE DEFINER=`test`#`localhost` PROCEDURE `onPlayerJoin`(
IN uuid CHAR(36),
IN nickname CHAR(16),
IN firstConnection TIMESTAMP,
IN lastConnection TIMESTAMP
)
BEGIN
INSERT IGNORE INTO `test`.`player` (`playerUuid`, `playerNickname`, `playerFirstConnection`, `playerLastConnection`)
VALUES (uuid, nickname, firstConnection, lastConnection);
UPDATE `test`.`player` SET
`playerNickname` = nickname,
`playerLastConnection` = lastConnection
WHERE `playerUuid` = uuid;
END
IF the PK isn't found, INSERT it. ELSE, UPDATE certain columns. (that I can specified) However, it seems that it updates every column, which I would like to restrict to certain columns. Here is my procedure : http://pastebin.com/NfcdU9Rb !
Optional question : is it injection-safe ?
Use INSERT ... ON DUPLICATE KEY UPDATE Syntax
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Kindly refer the link for details:
http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

IF NOT EXISTS then INSERT

I'm trying to add a value to a table but not without checking if the value already exists. This is what I have so far:
IF NOT EXISTS (
SELECT series.seriesName
FROM series
WHERE series.seriesName='Avengers'
)
BEGIN
INSERT INTO series (seriesName) VALUES 'Avengers'
END;
Database is a MySQL db on Ubuntu
You can use IGNORE keyword here.
It could look like:
INSERT IGNORE INTO series (seriesName) VALUES 'Avengers'
The important thing is to create a unique key on seriesName field as it seems that you want it to be unique.
INSERT IGNORE doesn't make the insert when key value already exists.
If you would like to be able to get id (primary key value) for row that wasn't inserted (already existed), you can do the following trick:
INSERT IGNORE INTO series (seriesName) VALUES 'Avengers'
ON DUPLICATE KEY UPDATE seriesID= LAST_INSERT_ID(seriesID)
Then you will be able to get the ID with LAST_INSERT_ID() function no matter if the row was inserted or not.

updating source table on insert into using mysql

Hi all I have the following query which works nicely but i know need to add the unique id generated in the insert table to the destination table
insert into claims.third_party(tp_names,tp_insurer,tp_registration)
select c.tpnames,c.tpinsurers,c.tpregistration from claims as c;
so in other words i need to return the unique id and add it to the source table creating a foreign key link
You can use LAST_INSERT_ID() to get the last auto increment value.
SELECT LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
UPDATE:
Use ROW_COUNT() alongside LAST_INSERT_ID() and you will have the range of keys generated.
[LAST_INSERT_ID(), (LAST_INSERT_ID() + ROW_COUNT())]