Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:17:59+00:00 2026-06-02T00:17:59+00:00

I’m using a trigger in MySQL to do the following: When I add a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:18:02+00:00Added an answer on June 2, 2026 at 12:18 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.