1
I have two tables - 

League        (lg_id, lg_Userid, lg_JoinDate)

Here, lg_id is PK and its auto incremented.

LeagueMember  (lgMbr_lg_id, lgMbr_Userid, lgMbr_JoinDate) 

lgMbr_lb_id is a FK referencing League(lg_id)

After a row is inserted into League table, a corresponding row should be inserted into leagueMember with the same lg_id which was inserted into League table. I am trying to create an after insert trigger for this.

CREATE TRIGGER trigger_league AFTER INSERT ON `League` 
FOR EACH ROW 
BEGIN 
    INSERT INTO `LeagueMember` (`lgMbr_lg_id`, `lgMbr_Userid`,`lgMbr_JoinDate`) values (NEW.lg_id,NEW.lg_UserId,'2000-10-10')
END;

I tried this but it doesn't work. Whats wrong here?

Edit -

I receive an error message which says there's an error with my syntax. Based on this mysql create trigger reference page I even tried this, but I still get a syntax error.

CREATE TRIGGER trigger_league AFTER INSERT ON `League` 
FOR EACH ROW 
BEGIN 
    INSERT INTO `LeagueMember` SET lgMbr_lg_id = NEW.lg_id;    
END;
6
  • it doesn't work - Can you be more specific? Commented Nov 21, 2010 at 19:48
  • I edited the question with details. Commented Nov 21, 2010 at 19:56
  • it will never work - stop and think about it ! Commented Nov 21, 2010 at 20:00
  • it won't?? why? After row is inserted to a table. I pick that row's PK value and insert into a referencing table. Shouldn't that work? Commented Nov 21, 2010 at 20:06
  • how many times do you create a league ? - see my answer which i just knocked up for you. Commented Nov 21, 2010 at 20:12

1 Answer 1

3

maybe this might help ???

drop table if exists league;
create table league
(
league_id smallint unsigned not null auto_increment primary key,
user_id int unsigned not null,
name varchar(255) unique not null
)
engine=innodb;

drop table if exists league_user;
create table league_user
(
league_id smallint unsigned not null,
user_id int unsigned not null,
created_date datetime not null,
creator_flag tinyint unsigned not null default 0,
primary key (league_id, user_id)
)
engine=innodb;


drop procedure if exists create_league;
delimiter #

create procedure create_league
(
in p_user_id int unsigned,
in p_name varchar(255)
)
begin

declare v_league_id smallint unsigned default 0;

    insert into league (user_id, name) values (p_user_id, p_name);

    set v_league_id = last_insert_id();

    call add_league_user(v_league_id, p_user_id, 1); -- 1 = owner
end #

delimiter ;

drop procedure if exists add_league_user;
delimiter #

create procedure add_league_user
(
in p_league_id smallint unsigned,
in p_user_id int unsigned,
in p_creator_flag tinyint unsigned
)
begin
    insert into league_user (league_id, user_id, created_date, creator_flag) values 
        (p_league_id, p_user_id, now(), p_creator_flag);
end #

delimiter ;

call create_league(1,'Premiere League');

call add_league_user(1,2,0);
call add_league_user(1,3,0);
call add_league_user(1,4,0);

select * from league;
select * from league_user;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. My first time with php and mysql. I wasn't aware of last_insert_id(). I wouldn't bother creating triggers if I can do the same with sp.
You're welcome - i generally avoid trigs as they have a habit of being forgotten about. Keep that code in sprocs and way to go :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.