i am getting error while using NEW.id in database triggers - mysql

i am trying to create a trigger for inserting data.and I want to get the current id for that author or user who is inserting or change in table.but I am getting this error:
unknown column id in new
here is my query
INSERT INTO logs VALUES(null,NEW.id,'inserted',NOW());
I have two table in my database comments table and logs table...
can anybody help me how can I get rid of this error...

It sounds like your comments table has columns like this:
CREATE TABLE comments (
comment_id ...
author ...
message ...
);
But your INSERT statement in the trigger references NEW.id. There is no id column in your comments table. The NEW.* syntax is a way to get access to the row that made the trigger execute. You can only reference columns that exist in that table.
So since I guess your primary key column is comment_id, you should drop the trigger and recreate it to run the INSERT like this:
INSERT INTO logs VALUES(null,NEW.comment_id,'inserted',NOW());
The point is that the NEW.comment_id must reference a column in the comments table, for which you defined the trigger.

Related

Duplicate entry error when inserting into table in MySQL

I have a big table already in MySQL and I'm trying to create a another smaller table that consists of data from the big one. I'm using this code:
CREATE TABLE IF NOT EXISTS borrower (
idBorrower VARCHAR(255),
borrower_address VARCHAR(255),
PRIMARY KEY (borrower_address)
);
INSERT INTO borrower
(idBorrower,
borrower_address)
SELECT idBorrower, borrower_address
FROM final_data;
I'm using the borrower_address as the PK because it is made up of email addresses and all of them are unique/ no duplicates. When I run the code to insert the data into the new borrower table I get the below error:
Error Code: 1062. Duplicate entry 'rmeigh8g#51.la' for key 'borrower.PRIMARY'
The email address the error message is mentioning is in the first row of the big table(final_data) that I'm inserting the data from. But there is no duplicate value, it only appears once in the big table.
Any idea what might be causing the issue or how to fix it?
If the data does not exist in the original table, then it would appear to exist multiple times in final_data. You can easily check this:
SELECT borrower_address, COUNT(*)
FROM final_data
GROUP BY borrower_address
HAVING COUNT(*) >= 2;
You can fix this problem then by using GROUP BY or some other construct to only fetch the data once -- or use ON DUPLICATE KEY UPDATE.

Trigger ID from Table A to Table B

I have two Tables as follows:
Report
Checks
ID column on Table "Report" has AI (Auto-Increment enabled and set to Primary Key)
ID column on Table "Checks" has AI (Auto-Increment Disabled and Indexed)
Here are my Foreign Key Constraints created under "checks" Table:
Foreign Key
What I need to accomplish is:
Whenever I run my Powershell query to insert a new row into table "Report", I need the same ID to get written under the ID column of the "Checks" Table.
Powershell Query:
"INSERT INTO Checks (WiFi, Printers, Notepad) VALUES ('$WiFi', '$Printers', '$NotepadPlus')"
Should I create a trigger in phpmyadmin in this case? and if so could anyone please assist with this?
Do I need to specify the ID in my Powershell command above? if yes how could this be done?
Thank you in advance,
Yes, you can specify the ID in PowerShell command.
I assume you are using Powershell command to insert into Reports too. To capture new ID generated you can use $command.LastInsertedId and it can be used while inserting into checks table.
Hope it helps.
SQL for creating TRIGGER to insert ID to Checks table:
CREATE DEFINER = CURRENT_USER TRIGGER `dbname`.`trigger_name` AFTER INSERT ON `Report` FOR EACH ROW
BEGIN
insert into Checks (id) values (NEW.id)
END
But the values '$WiFi', '$Printers', '$NotepadPlus' cannot be inserted by TRIGGER. You need to set these values by Updating the row related to the new inserted ID in your PHP code.

Copy entire table into other table and change id

I want to copy all the rows from one table to another and change the ID if there is a duplicate.
I'm using phpmyadmin and tried the operations tab.
Copy table to (database.table):
Data Only
Add Auto Increment
This is the SQL it gives me:
INSERT INTO `wsuca2_dbwsuca2`.`cxtb4_menu` SELECT * FROM `wsuca2_dbwsuca2`.`j25_menu`
This is the error I'm getting:
#1062 - Duplicate entry '0-0-root-*' for key 'idx_client_id_parent_id_alias_language'
what you need is
INSERT ...... ON DUPLICATE KEY UPDATE
also, "change the ID if there is a duplicate" is not enough it seems. Your table have a complex unique key of client_id,parent_id,alias,language.
both your table had an entry of with the above field set to 0-0-root-* so it throws a error because MySQL doesn't know how to handle it.
either
update those entry manually before copying over
use INSERT ...... ON DUPLICATE KEY UPDATE to specify how to update those entries when found.
use INSERT IGNORE to ignore all duplicate entries (probably not what you want)
Having Duplicate key error is clear notification that you did not use the same structure and keys in the both tables. So first recreate the structure:
DROP TABLE `wsuca2_dbwsuca2`.`cxtb4_menu`;
SHOW CREATE TABLE `wsuca2_dbwsuca2`.`j25_menu`; //old table structure
and paste the structure of j25_menu as structure od cxtb4_menu (change the name of the table). After that insert the data with your INSERT clause.

enter data into 2 tables with foriegn key relation based on one attribute of primary table

I have a table 'project' that has attributes:name, UID(PK), section(distinguisher)... and 3 tables A,B,C based on section with specific properties of each section.i want to be able to enter data into project and the section specific data into A/B/C TABLE based on entry in section attribute of project table of that row. all tables have Foreign key with UID as UID_A, UID_B, UID_C...any ideas on how i can do it? all help appreciated as i am quite a novice...thanks! I am working with Mysql workbench.
Well, create a trigger on the table project:
CREATE TRIGGER `fill_abc` AFTER INSERT ON `project`
FOR EACH ROW BEGIN
INSERT INTO A <some specific data...>;
INSERT INTO B <some specific data...>;
INSERT INTO C <some specific data...>;
END;
Probably, you want to handle not only inserts into this table, but updates and deletes as well - it is nearly the same idea. See trigger definition here and examples here

MySql Trigger, before insert delete a row in the same table

Is this possible? Before a row is inserted into tableA, I need to delete any rows in tableA containing duplicate data.The trigger below does not generate an error but doesn't work either.
CREATE TRIGGER remove_old_user
BEFORE INSERT ON userStatus
FOR EACH ROW
DELETE FROM userStatus WHERE username = NEW.username
Any ideas?
Is it essential that the previous record be deleted, rather than updated to match the new info? Sounds like INSERT ... ON DUPLICATE KEY UPDATE would meet most of your needs. More info in the MySQL manual.
(I'm assuming based on the question that the field is unique, and can be part of the primary key)