继续再看老师给推荐的深入浅出mysql数据库开发这本书(下载见 http://www.linuxidc.com/Linux/2012-01/51177.htm ),看到innodb数据库的外键关联问题时,遇到了一个问题,书上写的是可以对父表进行修改,从而同步到子表的外键上去,可是自己的实验却是没有能够。
- mysql> show create table countryG
- *************************** 1. row ***************************
- Table: country
- Create Table: CREATE TABLE `country` (
- `country_id` smallint(5) unsigned NOT NULL auto_increment,
- `country` varchar(50) NOT NULL,
- `last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
- PRIMARY KEY (`country_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 1 row in set (0.01 sec)
-
- mysql> show create table cityG
- *************************** 1. row ***************************
- Table: city
- Create Table: CREATE TABLE `city` (
- `city_id` smallint(5) unsigned NOT NULL auto_increment,
- `city` varchar(50) NOT NULL,
- `country_id` smallint(5) unsigned NOT NULL,
- `last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
- PRIMARY KEY (`city_id`),
- KEY `country_id` (`country_id`),
- CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
- mysql> select * from city;
- +---------+----------+------------+---------------------+
- | city_id | city | country_id | last_update |
- +---------+----------+------------+---------------------+
- | 1 | hancheng | 1 | 2012-01-09 09:18:33 |
- +---------+----------+------------+---------------------+
- 1 row in set (0.01 sec)
-
- mysql> select * from country;
- +------------+---------+---------------------+
- | country_id | country | last_update |
- +------------+---------+---------------------+
- | 1 | chen | 2012-01-09 09:16:38 |
- +------------+---------+---------------------+
- mysql> update country set country_id=100 where country_id=1;
- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test/city`, CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`))
上面的问题是说因为有关联的存在,所以无法改变country_id这个字段。
ORA-06502错误的反省count(*)与count(1)的区别有多大?相关资讯 MySQL基础教程
- MySQL基础教程:关于varchar(N) (01月22日)
- MySQL SELECT同时UPDATE同一张表 (02/19/2013 07:20:18)
- Linux修改MySQL最大并发连接数 (02/15/2013 15:37:21)
| - 高性能MySQL(第3版) 中文PDF带目 (10/26/2014 10:03:50)
- 如何在MySQL中的获取IP地址的网段 (02/18/2013 12:23:33)
- C++和C#访问MySQL的简单代码示例 (12/21/2012 09:04:10)
|
本文评论 查看全部评论 (0)