# 语法五:references
# 问题
Let's say I want to create two tables person
and hobby
and I want the hobby table id to reference the id of person?
person table
- id
- name
hobby
- id
- person_id
- hobby_name
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 实现
CREATE TABLE hobby (
id INT NOT NULL AUTO_INCREMENT,
person_id INT NOT NULL,
hobby_name VARCHAR(255),
PRIMARY KEY(id),
FOREIGN KEY(person_id) REFERENCES person(id)
)
1
2
3
4
5
6
7
2
3
4
5
6
7