This is just a quick question and I apologize if it's simple but I haven't been able to find any good examples, because I am sure it isn't that hard, I am just not sure on the syntax.

I need to update a status attribute in one table 'Airplane' when someone inserts the airplane in a table called servicing, they set a status too it, say 'inactive' I need that to correspond to the other table. I feel this should be easy but have been struggling with it for a while now.

Thank you in advance.

Here is the airplane table

Create Table Airplane(
    Name    Char(25)    NOT NULL,
    aID     Int NOT NULL Primary Key,
    aCount  Int,
    status  Char(8) NOT NULL DEFAULT 'Active',
    cost    double Default 0.0
);

Here is the servicing table, they are subject to change, was just focused on this trigger for all intensive purposes.

Create table Servicing( 
 TicketNumber Int  NOT NULL AUTO_INCREMENT Primary Key,
 Mechanic  Char(50) NOT NULL,
 Problem   Char(25) NOT NULL,
 Airplane  Char(25) NOT NULL,
 TimeComplete CHAR(25) NOT NULL,
 Servicing  Char(25) NOT NULL,
 Status   Char(25) NOT NULL DEFAULT 'Inactive');

Here is the trigger but seem to be running into issues.

DELIMITER $$
CREATE TRIGGER Airplane_status_update 
AFTER INSERT ON Servicing
FOR EACH ROW
BEGIN
UPDATE Airplane status 
   SET status = new.status
 WHERE name = new.airplane;
END $$
DELIMITER ;
有帮助吗?

解决方案

You're probably looking for something like this

DELIMITER $$
CREATE TRIGGER airplane_status_update 
AFTER INSERT ON servicing
FOR EACH ROW
BEGIN
UPDATE airplane 
   SET status = NEW.status
 WHERE airplane_id = NEW.airplane_id;
END $$
DELIMITER ;

You obviously need to tweak an UPDATE statement specifying correct field names since you didn't post DDL for servicing and airplane tables

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top