Bug #13845 Manual error on page 183 (A4 format)
Submitted: 7 Oct 2005 15:02 Modified: 29 Sep 2008 22:10
Reporter: Pedro Doria Meunier Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1.14 OS:Windows (XP)
Assigned to: CPU Architecture:Any

[7 Oct 2005 15:02] Pedro Doria Meunier
Description:
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
colour ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
INSERT INTO person VALUES (NULL, 'Antonio Paz');
INSERT INTO shirt VALUES
(NULL, 'polo', 'blue', LAST_INSERT_ID()),
(NULL, 'dress', 'white', LAST_INSERT_ID()),
(NULL, 't-shirt', 'blue', LAST_INSERT_ID());
INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');
INSERT INTO shirt VALUES
(NULL, 'dress', 'orange', LAST_INSERT_ID()),
(NULL, 'polo', 'red', LAST_INSERT_ID()),
(NULL, 'dress', 'blue', LAST_INSERT_ID()),
(NULL, 't-shirt', 'white', LAST_INSERT_ID());

This is what is on the manual .... it's counting on the LAST_INSERT_ID() function to get the records inserted right in the 'shirt' table
The output of all this in the manual is this:
SELECT * FROM person;
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style | colour | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 1 |
| 2 | dress | white | 1 |
| 3 | t-shirt | blue | 1 |
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
| 7 | t-shirt | white | 2 |
+----+---------+--------+-------+

the output we get:

mysql> SELECT * FROM person;
+----+---------------------+
| id | name                |
+----+---------------------+
|  1 | Antonio Paz         |
|  2 | Lilliana Angelovska |
+----+---------------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style   | colour | owner |
+----+---------+--------+-------+
|  1 | polo    | blue   |     1 |
|  2 | dress   | white  |     1 |
|  3 | t-shirt | blue   |     2 |
|  4 | dress   | orange |     2 |
|  5 | polo    | red    |     4 |
|  6 | dress   | blue   |     5 |
|  7 | t-shirt | white  |     6 |
+----+---------+--------+-------+
7 rows in set (0.00 sec)

the 4,5,6 weren't supposed to be there, right?
As I was demonstrating MySQL to my students you can imagine how embarrassed I was when I couldn't come up with a rational explanation other than that of a bug.

How to repeat:
Just follow the instructions on the manual (PT lang), page 183 (A4 format)...

Suggested fix:
The ball is on you field... :-)
[7 Oct 2005 15:19] Hartmut Holzgraefe
Yes, there was an error in the example that has been corrected in the online manual,
the example code taken from 

  http://dev.mysql.com/doc/mysql/en/example-foreign-keys.html

works fine. The printed manual is currently not as up to date as the online version ...
[7 Oct 2005 15:44] Pedro Doria Meunier
Thanks Hartmut Holzgraefe for your *prompt* response! Yes, this is the obvious solution, although it'd make more sense if there was tracking of the last_insert *per* table.. ;-)
Best regards,
Pedro Doria Meunier
[29 Sep 2008 22:10] Konstantin Osipov
The bug is corrected in the newer version of the printed manual.