从另一个 table 中的记录验证记录数据

Validate Record Data from Records in another table

我有 2 个 table:员工和地区。每个员工可以分配到一个区域,每个区域可以有多个员工。但是,我只希望输入到 'Region' 的员工记录中的数据是区域 table 中存在的数据。我该怎么做?

地区列表:东北、东南、中部、西北、西南

使用 MySQL、MySQL Workbench 和 Amazon RDS

您向区域添加外键约束 table

CREATE tABLE Region (region  VARCHAR(55) Primary KEY  NOT NULL)
INSERT INTO Region VALUES ('Northeast'), ('Southeast'), ('Central'), ('Northwest'), ('Southwest')
CREATE tABLE employee (id bigint Primary KEY auto_increment
, refregion  VARCHAR(55) NOT NULL
, 
    CONSTRAINT FK_RmploxxRegion FOREIGN KEY (refregion)
    REFERENCES Region(region))
INSERT INTO employee VALUES (NULL,'Northpole')
Cannot add or update a child row: a foreign key constraint fails (`db_1115468314`.`employee`, CONSTRAINT `FK_RmploxxRegion` FOREIGN KEY (`refregion`) REFERENCES `Region` (`region`))
INSERT INTO employee VALUES (NULL,'Northeast')
SELECT * FROM employee
id | refregion
-: | :--------
 2 | Northeast

db<>fiddle here