国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

深入mysql外鍵關聯問題的詳解

2024-07-24 12:48:24
字體:
來源:轉載
供稿:網友
今兒繼續再看老師給推薦的深入淺出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),感覺這就是自己出問題的地方。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天气| 桃园县| 三明市| 嘉义县| 陇川县| 嘉祥县| 峨山| 偏关县| 乐至县| 洮南市| 寿宁县| 尉氏县| 莱西市| 同江市| 枣庄市| 稷山县| 四川省| 安溪县| 丽江市| 广饶县| 榆林市| 农安县| 巴中市| 云浮市| 英吉沙县| 当涂县| 聂拉木县| 宿松县| 调兵山市| 梁山县| 伊春市| 崇信县| 宣化县| 星座| 桦南县| 尼玛县| 含山县| 古蔺县| 霸州市| 永安市| 鹤岗市|