我在node.js(MySQL模块)中使用MySQL来管理我的数据库。我有两个与一个主表相关的表格如下:
表1(主表):Flights
+-----+-------------+-----------------+
| Id | CreatedDate | Notes |
+-----+-------------+-----------------+
| 1 | 2015-01-01 | No reservations |
| 2 | 2015-02-15 | Without places |
| ... | ... | ... |
+-----+-------------+-----------------+
表2:FlightPassengers
+----------+------------+--------+
| IdFlight | Passenger | Arrive |
+----------+------------+--------+
| 1 | 125 | true |
| 2 | 115 | false |
| ... | ... | ... |
+----------+------------+--------+
表3:PassangerParameters
+----------+-------------+-------+
| IdFlight | Parameter | Value |
+----------+-------------+-------+
| 1 | Age | 12 |
| 2 | Civil status| S |
| 2 | Age | 25 |
| 4 | Civil status| M |
| 4 | Age | 40 |
| ... | ... | ... |
+----------+-------------+-------+
现在,当我想添加航班时,我必须首先进行INSERT INTO Flights SET ?
MySQL查询,然后让Flight Id
让其他人查询:
INSERT INTO FlightPassengers VALUES (?,?,?)
INSERT INTO PassangerParameters VALUES (?, ?, ?), (?, ?, ?)
将值插入其他表中。在这种情况下,我觉得非常低效和不舒服,特别是PassangerParameters
表。
如何使此查询更有效?我可以采取哪种方法来做到这一点?
答案 0 :(得分:1)
当您插入Flights
CREATE TRIGGER insertPassInfo
AFTER INSERT ON Flights
FOR EACH ROW
BEGIN
INSERT INTO FlightPassengers VALUES (NEW.Id, ?, ?);
INSERT INTO PassengerParameters VALUES (NEW.Id, ?, ?), (NEW.Id, ?, ?);
END