2015-10-04 169 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>