如何在MySQL中使用关键字“引用"? [英] How to use the keyword 'references' in MySQL?

查看:315
本文介绍了如何在MySQL中使用关键字“引用"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How is the references keyword used when creating a table?

假设我要创建两个表personhobby,并且我希望业余爱好表ID引用人员的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))

这篇关于如何在MySQL中使用关键字“引用"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆