數據庫外鍵 mysql
01.mysql> show create table country\G
02.*************************** 1. row ***************************
03. Table: country
04.Create Table: CREATE TABLE `country` (
05. `country_id` smallint(5) unsigned NOT NULL auto_increment,
06. `country` varchar(50) NOT NULL,
07. `last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
08. PRIMARY KEY (`country_id`)
09.) ENGINE=InnoDB DEFAULT CHARSET=utf8
10.1 row in set (0.01 sec)
11.
12.mysql> show create table city\G
13.*************************** 1. row ***************************
14. Table: city
15.Create Table: CREATE TABLE `city` (
16. `city_id` smallint(5) unsigned NOT NULL auto_increment,
17. `city` varchar(50) NOT NULL,
18. `country_id` smallint(5) unsigned NOT NULL,
19. `last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
20. PRIMARY KEY (`city_id`),
21. KEY `country_id` (`country_id`),
22. CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`)
23.) ENGINE=InnoDB DEFAULT CHARSET=utf8
24.1 row in set (0.00 sec)
25.mysql> select * from city;
26.+---------+----------+------------+---------------------+
27.| city_id | city | country_id | last_update |
28.+---------+----------+------------+---------------------+
29.| 1 | hancheng | 1 | 2012-01-09 09:18:33 |
30.+---------+----------+------------+---------------------+
31.1 row in set (0.01 sec)
32.
33.mysql> select * from country;
34.+------------+---------+---------------------+
35.| country_id | country | last_update |
36.+------------+---------+---------------------+
37.| 1 | chen | 2012-01-09 09:16:38 |
38.+------------+---------+---------------------+
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) sql
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 |
+------------+---------+---------------------+ .net
01.mysql> update country set country_id=100 where country_id=1;
02.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`))
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`)) rest
上面的問題是說由於有關聯的存在,因此沒法改變country_id這個字段。 code
而後本身又從新看了下書本,發現本身的sql語句中沒有innodb的外鍵約束方式(cascade,set null,no action,restrict),感受這就是本身出問題的地方。 索引
但是怎麼加入關聯方式呢,上網找了好半天也沒有合適的方法。就本身找唄,就經過老師說的方法,? help一點兒一點兒終於找到了怎麼改變的方法,文檔功能很強大啊 ci
01.| ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
02. | ADD [CONSTRAINT [symbol]]
03. PRIMARY KEY [index_type] (index_col_name,...)
04. | ADD [CONSTRAINT [symbol]]
05. UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)
| 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,...)
寫了後又是一大堆的錯誤,無從下手啊 rem
01.mysql> alter table city add CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE;
02.ERROR 1005 (HY000): Can't create table '.\test\#sql-ed0_37.frm' (errno: 121)
03.zhouqian@zhou :~$ perror 121
04.OS error code 121: Remote I/O error
05.MySQL error code 121: Duplicate key on write or update
06.
07.Can't create table 'test.icity' (errno: 150)-----我這裏也創建索引了。網上的說法是:字段類型和外鍵的索引
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的問題仍是沒有解決呢:
01.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;
02.Query OK, 0 rows affected (0.11 sec)
03.
04.mysql> show create table icity\G
05.*************************** 1. row ***************************
06. Table: icity
07.Create Table: CREATE TABLE `icity` (
08. `id` int(11) NOT NULL,
09. `city` varchar(20) DEFAULT NULL,
10. `country_id` smallint(5) unsigned NOT NULL,
11. PRIMARY KEY (`id`),
12. KEY `country_id` (`country_id`),
13. CONSTRAINT `icity_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE
14.) ENGINE=InnoDB DEFAULT CHARSET=latin1
15.1 row in set (0.02 sec)
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。呵呵……
01.mysql> alter table city drop FOREIGN KEY `city_ibfk_1`;
02.Query OK, 0 rows affected (0.24 sec)
03.Records: 0 Duplicates: 0 Warnings: 0
04.
05.mysql> alter table city add FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE;Query OK, 0 rows affected (0.16 sec)
06.Records: 0 Duplicates: 0 Warnings: 0
07.
08.mysql> show create table city\G
09.*************************** 1. row ***************************
10. Table: city
11.Create Table: CREATE TABLE `city` (
12. `city_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
13. `city` varchar(50) NOT NULL,
14. `country_id` smallint(5) unsigned NOT NULL,
15. `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
16. PRIMARY KEY (`city_id`),
17. KEY `country_id` (`country_id`),
18. KEY `idx_fk_country_id` (`country_id`),
19. CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE
20.) ENGINE=InnoDB DEFAULT CHARSET=utf8
21.1 row in set (0.00 sec)
大功告成
end