深入mysql外键关联问题的详解

时间:2024-01-16 09:19:44 

今儿继续再看老师给推荐的深入浅出mysql数据库开发这本书,看到innodb数据库的外键关联问题时,遇到了一个问题,书上写的是可以对父表进行修改,从而同步到子表的外键上去,可是自己的实验却是没有能够。


mysql> show create table country\G
*************************** 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 city\G
*************************** 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这个字段。
然后自己又重新看了下书本,发现自己的sql语句中没有innodb的外键约束方式(cascade,set null,no action,restrict),感觉这就是自己出问题的地方。
可是怎么加入关联方式呢,上网找了好半天也没有合适的方法。就自己找呗,就通过老师说的方法,? help一点儿一点儿终于找到了怎么改变的方法,文档功能很强大啊


| ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
  | ADD [CONSTRAINT [symbol]]
        PRIMARY KEY [index_type] (index_col_name,...)
  | ADD [CONSTRAINT [symbol]]
        UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)


写了后又是一大堆的错误,无从下手啊


mysql> alter table city add CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE;
ERROR 1005 (HY000): Can't create table '.\test\#sql-ed0_37.frm' (errno: 121)
zhouqian@zhou:~$ perror 121
OS error code 121:  Remote I/O error
MySQL error code 121: Duplicate key on write or update
 
Can't create table 'test.icity' (errno: 150)-----我这里也建立索引了。网上的说法是:字段类型和外键的索引


这里是重新建立一张表icity,结果可以了,总结可能是因为字段类型的问题,可是我的alter的问题还是没有解决呢:


mysql> create table icity(id int not null, city varchar(20), country_id smallint unsigned not null , primary key(id), foreign key(country_id) references country(country_id) on update cascade )engine=innodb;
Query OK, 0 rows affected (0.11 sec)
mysql> show create table icity\G
*************************** 1. row ***************************
       Table: icity
Create Table: CREATE TABLE `icity` (
  `id` int(11) NOT NULL,
  `city` varchar(20) DEFAULT NULL,
  `country_id` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `country_id` (`country_id`),
  CONSTRAINT `icity_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.02 sec)


在大家(老师和网友)的帮助下终于搞定了,做法先drop掉表里的外键,然后在add。呵呵……


mysql> alter table city drop FOREIGN KEY `city_ibfk_1`;
Query OK, 0 rows affected (0.24 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> alter table city add FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE;Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show create table city\G
*************************** 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`),
  KEY `idx_fk_country_id` (`country_id`),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


标签:mysql,外键关联
0
投稿

猜你喜欢

  • python 多线程爬取壁纸网站的示例

    2023-11-14 21:45:30
  • js实现正则匹配中文标点符号的方法

    2024-05-09 10:38:50
  • python实现远程通过网络邮件控制计算机重启或关机

    2022-04-16 01:05:27
  • Go语言集成开发环境IDE详细安装教程

    2024-04-25 15:26:12
  • SQL Server修改标识列方法 如自增列的批量化修改

    2024-01-19 04:59:22
  • python并发爬虫实用工具tomorrow实用解析

    2023-03-18 02:29:07
  • 如何更优雅地写python代码

    2022-03-03 04:53:24
  • Python 6种基本变量操作技巧总结

    2023-04-03 21:50:04
  • 3个适合新手练习的python小游戏

    2023-08-02 02:12:27
  • Sql server中的char、varchar、text和nchar、nvarchar、ntext的区别

    2011-08-14 09:43:44
  • Python实现Sqlite将字段当做索引进行查询的方法

    2021-06-05 13:31:51
  • Mysql 乘法除法精度不一致问题(除法后四位小数)

    2024-01-13 09:44:16
  • 细品Dreamweaver MX 2004内建FW技术

    2010-09-02 12:38:00
  • 详解Python3中的 input() 函数

    2022-09-21 19:50:59
  • MySQL下载安装、配置与使用教程详细版(win7x64)

    2024-01-22 16:44:50
  • python爬虫的数据库连接问题【推荐】

    2024-01-19 18:26:41
  • Go语言基本的语法和内置数据类型初探

    2024-05-28 15:24:09
  • Python序列的推导式实现代码

    2022-04-24 05:53:46
  • Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法示例

    2023-07-09 00:11:24
  • Python3 hashlib密码散列算法原理详解

    2021-07-06 12:59:55
  • asp之家 网络编程 m.aspxhome.com