Trigger ID from Table A to Table B - mysql

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.

Related

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

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.

Why everytime I go and execute insert statements that I want to insert into a table, it says field doesn't have a default value?

Here is the screenshot of the Parts table
INSERT INTO Supplies(Cost)
VALUES(10.0);
INSERT INTO Supplies(Cost)
VALUES (20.0);
INSERT INTO Supplies(Cost)
VALUES (30.0);
SELECT * FROM supplies;
Here is the Supplier table
Hello Everyone,
I am working on creating a database (three tables) in MySQL and every time I run an insert statement to input a record into my Supplies table, it says 'Field Supplier_SupplierID' doesn't have a default value'. When I created my model in MySQL, I already auto-incremented 'Parts_PartID' column and that error doesn't exist anymore. So why is it doing it with 'Supplier_SupplierID' column now? Can someone please help me? Here is my model pic and sample of SQL code when I insert records into the Supplies table.
You cannot insert into Supplies without providing the foreign keys for the Parts table and Suppliers table. While those primary keys in those tables may be incrementing automatically, they need to be already known when inserting into Supplies.
Typically you will use the known PartID and SupplierID that have previously been created when inserting into the Supplies table. Those columns cannot be NULL (by definition of your table).
'Field Supplier_SupplierID' doesn't have a default value' means that you are not providing a value, there is no default value and it's not allowed to be NULL.

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)