如何在 MySQL 中使用关键字“references"?

2022-01-05 00:00:00 reference mysql

创建表格时如何使用references关键字?

How is the references keyword used when creating a table?

假设我想创建两个表 personhobby 并且我希望 hobby 表 id 引用 person 的 id?

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

我该怎么做?

推荐答案

与此类似地创建爱好表:

Create the hobby table similarly to this:

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))

相关文章