Final Part of Cursor inserting twice into MYSQL Using Trigger

I'm using a trigger in MySQL to do the following:

When I add a new client to the client table, it should create a set of entries in a 'Client-Type' table, linking the client id to a set of type ids (client1, type1 client1, type2) etc...

However, the database is inserting the entry for the last type twice when the trigger is run. So the last two entries are (client1, type9 client1, type9).

The trigger code is as follows:

AFTER INSERT ON `nmsTicket`.`client`
FOR EACH ROW
BEGIN

   DECLARE done BOOLEAN DEFAULT 0;
   DECLARE a CHAR(2);

   DECLARE types CURSOR
   FOR
   SELECT typeID FROM type;

   DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;

   OPEN types;


 REPEAT
      FETCH types INTO a;



       INSERT INTO clientType(id_type, id_client) VALUES (a,new.idClient);

   UNTIL done END REPEAT;

   CLOSE types;

I've looked it over a few times, but I can't see why it would be exhibiting this behaviour; all the entries before the last one work fine.

Any pointers?

Answers


I'm not sure why you're using a cursor in your trigger - this should be avoided unless you absolutely need one (see here for example Optimal MySQL settings for queries that deliver large amounts of data?)

The following is a simplified example (minus referential integrity) which uses a cross join instead of a cursor. In addition you'll notice I'm not using a surrogate primary key on the client_types table but a composite one instead which better enforces data integrity.

Schema

drop table if exists client_type; --your type table
create table client_type
(
type_id tinyint unsigned not null auto_increment primary key,
name varchar(255) unique not null
)
engine=innodb;

drop table if exists client;
create table client
(
client_id int unsigned not null auto_increment primary key,
name varchar(255) not null
)
engine=innodb;

drop table if exists client_types; -- your clienttype table
create table client_types
(
client_id int unsigned not null,
type_id tinyint unsigned not null,
primary key (client_id, type_id) -- ** note use of composite primary key **
)
engine=innodb;

delimiter #

create trigger client_after_ins_trig after insert on client
for each row
begin

insert into client_types (client_id, type_id) 
select
 c.client_id,
 ct.type_id
from
 client c
cross join client_type ct
where
 c.client_id = new.client_id
order by
 ct.type_id;

end#

delimiter ;

Testing

mysql> insert into client_type (name) values ('type one'),('type two'),('type three');
Query OK, 3 rows affected (0.03 sec)

mysql> insert into client (name) values ('client A'),('client B');
Query OK, 2 rows affected (0.04 sec)

mysql> select * from client_type;
+---------+------------+
| type_id | name       |
+---------+------------+
|       1 | type one   |
|       3 | type three |
|       2 | type two   |
+---------+------------+
3 rows in set (0.00 sec)

mysql> select * from client;
+-----------+----------+
| client_id | name     |
+-----------+----------+
|         1 | client A |
|         2 | client B |
+-----------+----------+
2 rows in set (0.00 sec)

mysql> select * from client_types;
+-----------+---------+
| client_id | type_id |
+-----------+---------+
|         1 |       1 |
|         1 |       2 |
|         1 |       3 |
|         2 |       1 |
|         2 |       2 |
|         2 |       3 |
+-----------+---------+
6 rows in set (0.00 sec)

Hope this helps :)


Need Your Help

Is there any ready-made calendar control for iPhone apps?

iphone cocoa-touch interface uikit calendar

I am building an applicaiton for the iPhone that will display upcoming and past events. I settled for a list view, but then I realized that a calendar (just like the one displayed in the "month" vi...

Using Visual C++ with R

c# c++ r visual-c++

I am wondering if there was a workaround for using the R package Rcpp within a Visual C++ project within Visual Studio. According to the Rcpp FAQ, this is not possible. I also came across another R