181
技术社区[云栖]
MySQL外键-涉及天气预报的小小思考
MySQL中“键”和“索引”的定义相同, 所以外键和主键一样也是索引的一种。不同的是MySQL会自动为所有表的主键进行索引,但是外键字段必须由用户进行明确的索引。
表间一对一关系示例:
有两张表,第一张表是记录公司有多少人,都有谁,也就是员工编号及员工姓名这些基本表。另一张表记录每个月发给用户多少工资,所谓工资表是也。
但是工资表里面不能以员工姓名为主键,同样要通过员工id,因为员工的姓名是可能重复的啊。部门经理叫张三,小弟也叫张三,那这俩张三的工资能一样吗?并且员工表里面的每个人都有工资,否则谁也不给你干活,且一个人只能有一份工资,否则老板也不同意了。所以员工表和工资表是通过员工id进行关联的一对一关系。
/* 建立员工表 */ create table employees ( id int(5) not null auto_increment , name varchar(8) not null, primary key (id) ) type=innodb; /* 建立工资表 */ create table payroll( id int(5) not null, emp_id int(5) not null, name varchar(8) not null, payroll float(4,2) not null, primary key(id), index emp_id (emp_id), foreign key (emp_id) references employees (id) ) type = innodb;
天气预报模仿整一个一对一的
城市表
create table city_info(
-> city_id int(5) not null auto_increment,
-> city_code int(5) not null,
-> city_name varchar(8) not null,
-> primary key(city_id)
-> );
天气表
create table weather_info(
-> id int(5) not null,
-> city_id int(5) not null,
-> weather_date varchar(20) not null,
-> weather_year varchar(20) not null,
-> weather_week varchar(20) not null,
-> weather_temp varchar(20) not null,
-> weather_winddirection varchar(20) not null,
-> weather_windpower varchar(20) not null,
-> weather_description varchar(50) not null,
-> primary key(id),
-> index city_id(city_id),
-> foreign key(city_id) references city_info(city_id)
-> );
搞个例子,简单演示一下使用,做dage和xiaodi两个表,大哥表是主键,小弟表是外键:
建表:
CREATE TABLE `dage` (2
`id` int(11) NOT NULL auto_increment,3
`name` varchar(32) default '',4
PRIMARY KEY (`id`)5
) ENGINE=InnoDB DEFAULT CHARSET=latin1;6

7
CREATE TABLE `xiaodi` (8
`id` int(11) NOT NULL auto_increment,9
`dage_id` int(11) default NULL,10
`name` varchar(32) default '',11
PRIMARY KEY (`id`),12
KEY `dage_id` (`dage_id`),13
CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`)14
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
插入个大哥:
mysql> insert into dage(name) values('铜锣湾');2
Query OK, 1 row affected (0.01 sec)3
mysql> select * from dage;4
+----+--------+5
| id | name |6
+----+--------+7
| 1 | 铜锣湾 |8
+----+--------+9
1 row in set (0.00 sec)
插入个小弟:
mysql> insert into xiaodi(dage_id,name) values(1,'铜锣湾_小弟A');2
Query OK, 1 row affected (0.02 sec)3

4
mysql> select * from xiaodi;5
+----+---------+--------------+6
| id | dage_id | name |7
+----+---------+--------------+8
| 1 | 1 | 铜锣湾_小弟A |9
+----+---------+--------------+
把大哥删除:
mysql> delete from dage where id=1;2
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))
提示:不行呀,有约束的,大哥下面还有小弟,可不能扔下我们不管呀!
插入一个新的小弟:
mysql> insert into xiaodi(dage_id,name) values(2,'旺角_小弟A'); 2
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))3
提示:小子,想造反呀!你还没大哥呢!
把外键约束增加事件触发限制:
mysql> show create table xiaodi;2



3
CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`)4



5
mysql> alter table xiaodi drop foreign key xiaodi_ibfk_1; 6
Query OK, 1 row affected (0.04 sec)7
Records: 1 Duplicates: 0 Warnings: 8
mysql> alter table xiaodi add foreign key(dage_id) references dage(id) on delete cascade on update cascade;9
Query OK, 1 row affected (0.04 sec)10
Records: 1 Duplicates: 0 Warnings: 0
再次试着把大哥删了:
mysql> delete from dage where id=1;2
Query OK, 1 row affected (0.01 sec)3

4
mysql> select * from dage;5
Empty set (0.01 sec)6

7
mysql> select * from xiaodi;8
Empty set (0.00 sec)
得,这回对应的小弟也没了,没办法,谁让你跟我on delete cascade了呢!
例子说明的应该蛮清楚了吧,其他功能对应手册自己实践吧!:-)
经过一天的思考,见表思路如下;
城市ID 城市名 [主键(城市ID)] 1000 北京 1001 上海
1002 重庆
1003 四川
序号 城市ID 日期 温度[主键(序号)][外键(城市ID)]
1 1000 20011101 10
2 1000 20011102 10
3 1000 20011103 10
4 1000 20011104 10
第一张表的建立:
CREATE TABLE `city_info` ( `city_id` int(11) NOT NULL , `city_name` varchar(32) default '', PRIMARY KEY (`city_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
第二章表的建立:
CREATE TABLE `weather_info` (
`id` int(11) NOT NULL auto_increment,
`city_id` int(11) default NULL,
`weather_date` varchar(32) default '',
`weather_year` varchar(32) default '',
`weather_week` varchar(32) default '',
`weather_temp` varchar(32) default '',
`weather_winddirection` varchar(32) default '',
`weather_windpower` varchar(32) default '',
`weather_description` varchar(32) default '',
PRIMARY KEY (`id`),
KEY `city_id` (`city_id`),
CONSTRAINT `weather_info_ibfk_1` FOREIGN KEY (`city_id`) REFERENCES `city_info` (`city_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
最后更新:2017-04-02 06:52:09