关于MySQL的外键问题
在mysql的官方页面 http://dev.mysql.com/doc/mysql/en/example-Foreign_keys.html 上有个使用外键的例子.
大家请查看该网页.按该页面的操作,查询shirt的结果如下.
SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style | color | 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 |
+----+---------+--------+-------+
可是在我的机子上确实显示:
SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style | color | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 0 |
| 2 | dress | white | 0 |
| 3 | t-shirt | blue | 0 |
| 4 | dress | orange | 0 |
| 5 | polo | red | 0 |
| 6 | dress | blue | 0 |
| 7 | t-shirt | white | 0 |
+----+---------+--------+-------+
这说明外键根本没起作用,这是为什么?
后来我把那建表的语句改了一下,换成如下格式的,结果也是一样.
CREATE TABLE `person` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`name` char(60) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=InnoDB AUTO_INCREMENT=1;
CREATE TABLE `shirt` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`style` enum('t-shirt','polo','dress') NOT NULL default 't-shirt',
`color` enum('red','blue','orange','white','black') NOT NULL default 'red',
`owner` smallint(5) unsigned NOT NULL REFERENCES person(id),
PRIMARY KEY (`id`)
) TYPE=InnoDB AUTO_INCREMENT=1 ;