将数据链接到另一个表

时间:2018-01-19 14:17:34

标签: mysql phpmyadmin

我有一张表 phone_numbers 和表

人员结构是

id | name

phone_numbers结构是

id | people_id | phone_number

我已经看到有可能将一个people_id作为PhpMyAdmin中表的链接。我想知道如何实现这一点。

1 个答案:

答案 0 :(得分:1)

它们被称为外键,您可以在互联网上找到很多教程。 例如;

http://www.mysqltutorial.org/mysql-foreign-key/

为您举例:

CREATE TABLE people (
   id int not null auto_increment primary key,
   name varchar(255) not null,       
);

CREATE TABLE phone_numbers (
   id int not null auto_increment primary key,
   people_id int not null,
   phone_number varchar(32) not null,
   FOREIGN KEY fk_ppkey(people_id)
   REFERENCES people(id)
   ON UPDATE CASCADE
   ON DELETE RESTRICT
);

这些行:

FOREIGN KEY fk_ppkey(people_id) REFERENCES people(id)

表的 ID 字段引用至 phone_numbers 表的 people_id 表。