從更好的解決方案角度來看,最好在應用程序邏輯中將數據插入到使用表中時執行此操作,而不是創建觸發器來實現此目的。爲了演示你的風格,我使用了一個存儲過程來插入使用表。您可以相應地修改它。
-- create usage table
create table `usage` (job varchar(100),
start_time date,
end_time date,
`session` varchar(100)
);
-- create `sessions` table
create table `sessions` (session_label varchar(100) ,
start_time date,
end_time date);
-- create procedure prc_insert_into_usage
delimiter $$
Create Procedure prc_insert_into_usage
(
IN p_job varchar(100),
IN p_start_time date,
IN p_end_time date
)
Begin
Declare v_session varchar(100);
-- check if record is there in the session table
-- it is assumed session table will have only one record for composite key (start_time, end_time)
Select session_label into v_session
from `sessions`
where start_time = p_start_time and end_time = p_end_time;
IF (v_session IS NULL) THEN
-- if session_label is generated using auto increment key in sessions table then
-- last_insert_id() can be used to fetch that value after insert into sessions table
-- which can be used while inserting into usage table
-- example below
-- Insert into `sessions` (start_time,end_time) values (p_start_time,p_end_time);
-- set @v_session = last_insert_id();
-- Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session);
-- dummy logic to create the session_label
-- dummy logic is used you can replace it with whatever logic you need
set @v_session = concat('sess_',left(uuid(),8));
-- insert record in both table (first in session table and then in usage table)
Insert into `sessions` values (@v_session,p_start_time,p_end_time);
Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session);
else
-- record already present in sessions table for this session label
-- so insert record only in usage table
Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session);
End If;
End;
$$
-- call this procedure to insert data into usage table
-- sample execution below when both tables have no rows
-- below call will insert 1 rows in both table having same session label
mysql> call prc_insert_into_usage('job_a','2015-01-01','2015-01-02');
Query OK, 1 row affected (0.00 sec)
mysql> select * from `usage`;
+-------+------------+------------+---------------+
| job | start_time | end_time | session |
+-------+------------+------------+---------------+
| job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf |
+-------+------------+------------+---------------+
1 row in set (0.00 sec)
mysql> select * from `sessions`;
+---------------+------------+------------+
| session_label | start_time | end_time |
+---------------+------------+------------+
| sess_abc376bf | 2015-01-01 | 2015-01-02 |
+---------------+------------+------------+
1 row in set (0.01 sec)
-- below call will insert only in usage table as row already present in sessions table
mysql> call prc_insert_into_usage('job_b','2015-01-01','2015-01-02');
Query OK, 1 row affected (0.00 sec)
mysql> select * from `usage`;
+-------+------------+------------+---------------+
| job | start_time | end_time | session |
+-------+------------+------------+---------------+
| job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf |
| job_b | 2015-01-01 | 2015-01-02 | sess_abc376bf |
+-------+------------+------------+---------------+
2 rows in set (0.00 sec)
mysql> select * from `sessions`;
+---------------+------------+------------+
| session_label | start_time | end_time |
+---------------+------------+------------+
| sess_abc376bf | 2015-01-01 | 2015-01-02 |
+---------------+------------+------------+
1 row in set (0.00 sec)
-- below call will again insert rows in both table
mysql> call prc_insert_into_usage('job_c','2015-01-02','2015-01-04');
Query OK, 1 row affected (0.01 sec)
mysql> select * from `usage`;
+-------+------------+------------+---------------+
| job | start_time | end_time | session |
+-------+------------+------------+---------------+
| job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf |
| job_b | 2015-01-01 | 2015-01-02 | sess_abc376bf |
| job_c | 2015-01-02 | 2015-01-04 | sess_dcfa6853 |
+-------+------------+------------+---------------+
3 rows in set (0.00 sec)
mysql> select * from `sessions`;
+---------------+------------+------------+
| session_label | start_time | end_time |
+---------------+------------+------------+
| sess_abc376bf | 2015-01-01 | 2015-01-02 |
| sess_dcfa6853 | 2015-01-02 | 2015-01-04 |
+---------------+------------+------------+
2 rows in set (0.00 sec)
mysql>
不要使用觸發器來做到這一點。當你讀取數據時使用'JOIN'來做 - 除非你有一個真正的實時理由。 –
我已經更新了這個問題,你可以幫我實現這個使用觸發器,或者如果聯接更好,如何以及爲什麼? –