2015-10-04 171 views
1

我有以下結構的表「使用」後2個表:觸發與更新

Job | Start Time | End Time | Session 

而且我還有一個表「會議」具有下列結構來跟蹤會話:

Session Label | Start Time | End Time 

我想有一個觸發器,當第一個表中有條目時,觸發器將檢查其他表的開始時間和結束時間。如果已存在標籤,則第一個表的會話列需要使用會話標籤進行更新,否則應將新條目添加到第二個表中,並將相應的標籤輸入到第二個表的會話列中。我以前從來沒有使用SQL觸發器,也不知道如何實現這一點。

我想:

CREATE TRIGGER test AFTER INSERT ON `usage` 
FOR EACH ROW 
WHEN (SELECT RIGHT(`usage`.starttime,5) != SELECT RIGHT(`sessions`.starttime,5)) 
BEGIN 
    SET `usage`.sessionlabel = `A` 
END; 

邏輯:

for(newrecordinusagetable) 
{ 
//check if same starttime exists in any record of session table 
if(true) 
{ 
//do nothing 
} 
else 
{ 
//add the new starttime to the session table 
} 

PS:不要理會結束時間或標籤。

+1

不要使用觸發器來做到這一點。當你讀取數據時使用'JOIN'來做 - 除非你有一個真正的實時理由。 –

+0

我已經更新了這個問題,你可以幫我實現這個使用觸發器,或者如果聯接更好,如何以及爲什麼? –

回答

2

下面是基於有限的信息提供了一個嘗試:

DELIMITER $$ 

CREATE TRIGGER usage_session_trigger 
AFTER INSERT ON `usage` 
FOR EACH ROW 
    BEGIN 
     -- Count sessions with same start time 
     DECLARE session_count INT; 
     SET session_count = (SELECT COUNT(*) 
          FROM sessions 
          WHERE RIGHT(starttime, 5) = RIGHT(NEW.starttime, 5)); 

     -- If none found, insert one. 
     IF (session_count = 0) THEN 
      INSERT INTO sessions (sessionlabel, starttime, endtime) 
      VALUES (NEW.`session`, NEW.starttime, NEW.endtime); 
     END IF; 
    END; 
$$ 

DELIMITER ; 

注意

  1. 我假設你有充分的理由時代的最後5個字符比較的例子等有在這裏重複。 (不知道爲什麼5雖然!)
  2. 也可能需要考慮是否需要類似的觸發更新(+可能刪除?)
  3. 我不是在評論你是否要求做的是正確的做法 - 試圖回答這個問題。
+0

上面的答案几乎是正確的,但是說有一個記錄的開始時間:「10:00」已經在會話表中,另一個插入「usage」表中,開始時間爲「10:00」,但我沒有希望它能夠觸發並保持原樣,但如果沒有匹配,則插入 –

+0

您的意思是您想僅根據開始時間執行查找並更新結束時間以及會話標籤? –

+0

...已經更新了我的答案,假設情況是這樣的。 –

0

從更好的解決方案角度來看,最好在應用程序邏輯中將數據插入到使用表中時執行此操作,而不是創建觸發器來實現此目的。爲了演示你的風格,我使用了一個存儲過程來插入使用表。您可以相應地修改它。

-- 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>