我有一个表phone_numbers和表人员
人员结构是
id | name
phone_numbers结构是
id | people_id | phone_number
我发现可以在 PhpMyAdmin 中创建一个 people_id 作为表 people 的链接。我想知道如何实现这一点。
最佳答案
它们被称为外键,您可以在互联网上找到很多教程。 例如;
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)
将people表的id字段引用到phone_numbers表的people_id表。
关于mysql - 将数据链接到另一个表中的唯一索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48343101/