深入mysql外鍵關(guān)聯(lián)問題的詳解
更新時間:2013年06月14日 10:09:05 作者:
本篇文章是對mysql外鍵關(guān)聯(lián)問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
今兒繼續(xù)再看老師給推薦的深入淺出mysql數(shù)據(jù)庫開發(fā)這本書,看到innodb數(shù)據(jù)庫的外鍵關(guān)聯(lián)問題時,遇到了一個問題,書上寫的是可以對父表進(jìn)行修改,從而同步到子表的外鍵上去,可是自己的實驗卻是沒有能夠。
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`))
上面的問題是說因為有關(guān)聯(lián)的存在,所以無法改變country_id這個字段。
然后自己又重新看了下書本,發(fā)現(xiàn)自己的sql語句中沒有innodb的外鍵約束方式(cascade,set null,no action,restrict),感覺這就是自己出問題的地方。
可是怎么加入關(guān)聯(lián)方式呢,上網(wǎng)找了好半天也沒有合適的方法。就自己找唄,就通過老師說的方法,? help一點(diǎn)兒一點(diǎn)兒終于找到了怎么改變的方法,文檔功能很強(qiáng)大啊
| 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)-----我這里也建立索引了。網(wǎng)上的說法是:字段類型和外鍵的索引
這里是重新建立一張表icity,結(jié)果可以了,總結(jié)可能是因為字段類型的問題,可是我的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)
在大家(老師和網(wǎng)友)的幫助下終于搞定了,做法先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)
復(fù)制代碼 代碼如下:
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 |
+------------+---------+---------------------+
復(fù)制代碼 代碼如下:
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`))
上面的問題是說因為有關(guān)聯(lián)的存在,所以無法改變country_id這個字段。
然后自己又重新看了下書本,發(fā)現(xiàn)自己的sql語句中沒有innodb的外鍵約束方式(cascade,set null,no action,restrict),感覺這就是自己出問題的地方。
可是怎么加入關(guān)聯(lián)方式呢,上網(wǎng)找了好半天也沒有合適的方法。就自己找唄,就通過老師說的方法,? help一點(diǎn)兒一點(diǎn)兒終于找到了怎么改變的方法,文檔功能很強(qiáng)大啊
復(fù)制代碼 代碼如下:
| 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,...)
寫了后又是一大堆的錯誤,無從下手啊
復(fù)制代碼 代碼如下:
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)-----我這里也建立索引了。網(wǎng)上的說法是:字段類型和外鍵的索引
這里是重新建立一張表icity,結(jié)果可以了,總結(jié)可能是因為字段類型的問題,可是我的alter的問題還是沒有解決呢:
復(fù)制代碼 代碼如下:
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)
在大家(老師和網(wǎng)友)的幫助下終于搞定了,做法先drop掉表里的外鍵,然后在add。呵呵……
復(fù)制代碼 代碼如下:
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)
相關(guān)文章
MySQL巧用sum、case和when優(yōu)化統(tǒng)計查詢
這篇文章主要給大家介紹了關(guān)于MySQL巧用sum、case和when優(yōu)化統(tǒng)計查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03解決mysql時區(qū)問題導(dǎo)致錯誤Incorrect datetime value: &apo
這篇文章主要介紹了解決mysql時區(qū)問題導(dǎo)致錯誤Incorrect datetime value: '1970-01-01 00:00:01',具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10xtrabackup備份還原MySQL數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了xtrabackup備份還原MySQL數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06一文詳解MySQL數(shù)據(jù)庫索引優(yōu)化的過程
在MySQL數(shù)據(jù)庫中,索引是一種關(guān)鍵的組件,它可以大大提高查詢的效率,但是,當(dāng)數(shù)據(jù)量增大或者查詢復(fù)雜度增加時,索引的選擇和優(yōu)化變得至關(guān)重要,本文將記錄MySQL數(shù)據(jù)庫索引優(yōu)化的過程,以幫助開發(fā)人員更好地理解和應(yīng)用索引優(yōu)化技巧2023-06-06在linux服務(wù)器上配置mysql并開放3306端口的操作步驟
這篇文章主要介紹了在linux服務(wù)器上配置mysql并開放3306端口,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09