views:

65

answers:

1

Hi, I'm having some problems with my stored procedure. Hope anyone can help me figure out what the problem is.

I use the data from weekly_schedule as a template for how fill my flight-table. I do this by calling CreateFlights(). All data get inserted as expected, so I'm not missing any data, but for some reason every third row is skipped despite the fact that I use auto_increment on flight.id.
(flight.id = 1,2,4,5,7,8,10,11 ... 472,473,475,476...
flight.id = 3,6,9,12 etc. are not null, they're simply just not there.)

I can't figure out why I get the holes in the id-sequence. Can anyone help me understanding what happens? I want the flight-id's in a continuous sequence.

Here is my code..


CREATE PROCEDURE CreateFlights()
BEGIN
  DECLARE startdate date DEFAULT curdate();
  DECLARE enddate date DEFAULT date_add(startdate, interval 367 day);

 WHILE startdate <= enddate DO
 INSERT INTO flight(`id_weekly_flights`, `flightdate`)
  (select `id`, startdate  
   from `weekly_flights`
   WHERE `name_weekdays` = dayname(startdate)
   AND `fl_year` = year(startdate));
 SET startdate = date_add(startdate, interval 1 day);
 END WHILE;
END //

CREATE TABLE weekly_flights (  
id int NOT NULL AUTO_INCREMENT,  
departuretime time,  
fl_year int,  
name_weekdays varchar(9),  
id_route varchar(8),  
PRIMARY KEY (id),  
FOREIGN KEY (name_weekdays) REFERENCES weekdays(name),  
FOREIGN KEY (id_route) REFERENCES route(id)  
)  
ENGINE=INNODB;

CREATE TABLE flight (  
id int NOT NULL AUTO_INCREMENT,  
flightdate date DEFAULT NULL,   
id_weekly_flights int,  
PRIMARY KEY (id),  
FOREIGN KEY (id_weekly_flights) REFERENCES weekly_flights(id)  
)  
ENGINE=INNODB;

INSERT INTO weekly_flights(departuretime, fl_year, name_weekdays, id_route)  
VALUES  
(073000,2010,'Monday', 'LIN2STH'),  
(073000,2010,'Monday', 'STH2LIN'),  
(073000,2010,'Friday', 'LIN2STH'),  
(073000,2010,'Friday', 'STH2LIN'),  
(210000,2010,'Sunday', 'LIN2STH'),  
(210000,2010,'Sunday', 'STH2LIN'),  
(073100,2011,'Monday', 'LIN2STH'),  
(073100,2011,'Monday', 'STH2LIN'),  
(073100,2011,'Friday', 'LIN2STH'),  
(073100,2011,'Friday', 'STH2LIN'),  
(093100,2011,'Sunday', 'LIN2STH'),  
(093100,2011,'Sunday', 'STH2LIN');
+3  A: 

I call a 'non-problem' on this.

Are you aware of the fact, that you are not guaranteed to have consecutive auto_increment values at all? Actually, you are not guaranteed that the next auto_incremented value will be greater, than the one before it. It just works this way sometimes by coincidence.

auto_increment is there to help you generate identity values, that's all there is to it, really.

shylent
Hmm, no, I wasn't aware of that. I just assumed so based on the method name. Reading http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html makes it clearer. It doesn't even mention anything that backs my assumption up. Thanks!
theed
@theed: heh, sorry, I probably should've been less assertive, because, indeed, the name 'auto_increment' mysql folks have chosen may lead to false assumptions. MS SQL Server, for example, calls this kind of field 'Identity', which makes the purpose of the field perfectly clear right from the start.
shylent