我有两张名为: train_information 和轴的表。
Train_information
有以下内容:
train_id (PK)
train_name
train_length
number_of_axles
axle
有以下内容:
axle_id (PK)
train_id(FK)
axle
当我在 train_information 上插入内容时,让我们说例如将数字16放在 number_of_axles 中。我希望表轴能够自动获取相同的信息。
如何使用触发器执行此操作?
编辑:
现在使用此代码:
CREATE TRIGGER TriggerName AFTER INSERT ON train_information
FOR EACH
ROW
BEGIN
INSERT INTO axle( train_id, axle )
VALUES (
NEW.train_id, NEW.number_of_axles
);
END
但是现在,它输入了表轴中的数字16 但我希望它像:
axle_id = 1
train_id = 1
axle = 1
axle_id = 2
train_id = 1
axle = 2
axle_id = 3
train_id = 1
axle = 3
这是可能的吗?
答案 0 :(得分:1)
您可以在AFTER TRIGGER
表格上创建Train_information
,如下所示:
<强> SQL 强> 有关After Triggers in SQL
的更多信息CREATE TRIGGER TriggerName
ON Train_information
FOR INSERT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO axle
(train_id, axle)
SELECT
train_id, 'axle_name'
FROM inserted
END
<强>的MySQL 强> 有关After Triggers in MySQL
的更多信息DELIMITER $$
CREATE TRIGGER TriggerName
AFTER INSERT ON Train_information FOR EACH ROW
BEGIN
INSERT INTO axle (train_id, axle)
VALUES (NEW.train_id, NEW.axle_name)
END;
$$
DELIMITER ;