简体   繁体   English

MySQL:是否可以仅通过DB设计将一个表中的列值约束为另一表中的列值?

[英]MySQL: Can I constraint column values in one table to values in a column in another table, by DB design only?

Example: 例:

Table "persons", Column "surname" may only contain values predefined in 表“人员”,列“姓”只能包含在中预定义的值

Table "names", Column "surnames", which would contain a collection of surnames acceptable for the purpose. 表“名称”,列“姓氏”,其中将包含为此目的可接受的姓氏集合。

Can I achieve this by design (ie without involving any validation code)? 我可以通过设计实现此目标(即不涉及任何验证代码)吗? On a MyISAM table? 在MyISAM表上? No? 没有? On InnoDB? 在InnoDB上?

Thank you. 谢谢。

What you're asking for is a foreign key constraint. 您要的是外键约束。 You'd need to use InnoDB - quote : 您需要使用InnoDB-quote

For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it. 对于InnoDB以外的存储引擎,MySQL Server在CREATE TABLE语句中解析FOREIGN KEY语法,但不使用或存储它。

To add a foreign key constraint within the CREATE TABLE statement for PERSONS : 要在CREATE TABLE语句中为PERSONS添加外键约束:

FOREIGN KEY (surname) REFERENCES names(surnames)

Using an ALTER TABLE statement if the tables already exist: 如果表已经存在,请使用ALTER TABLE语句:

ALTER TABLE persons 
  ADD CONSTRAINT FOREIGN KEY (surname) REFERENCES names(surname)

Be aware that if you use the ALTER TABLE statement, the data in the PERSONS table can only contain surname values that exist in the NAMES.surname table - it can not be applied until after the data has been fixed. 请注意,如果您使用ALTER TABLE语句,在数据PERSONS表只能包含存在于姓值NAMES.surname表-它不能被应用,直到数据已被固定后。

For MyISAM tables you can achieve desired functionality by using triggers. 对于MyISAM表,您可以通过使用触发器来实现所需的功能。

For instance (validate insert), 例如(验证插入),

DELIMITER // 
CREATE DEFINER=`root`@`localhost` TRIGGER BEFORE INSERT ON persons
FOR EACH ROW
BEGIN
  DECLARE tmp_surname varchar(100);
  SELECT surname into tmp_surname FROM names WHERE surname = NEW.surname; 
  IF (tmp_surname IS NULL) THEN
    INSERT INTO t1(id,value) VALUES('aaa'); #raise an 'exception'
  END IF;    
END;//
delimiter;

Mysql doesn't have exceptions, but you can terminate execution(and, consequently, 'rollback' changes) by creating an invalid statement Mysql没有异常,但是您可以通过创建无效的语句来终止执行(并因此执行“回滚”更改)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用一个来自另一个表的列的值约束一个列? - How to constraint one column with values from a column from another table? MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same 使用另一个表中的列值更新一个表中的列的值SQL? - Updating the values of a column in one table with the values of a column in another table SQL? 如何用另一个表中的列值替换一个MySQL表中的列值 - How to replace values of a column in one MySQL table with values of a column in another table 如何在 MYSQL 中自动将值从一个表列加载到另一列 - How to automatically load values from one table column to another in MYSQL 在mysql中根据条件从另一个表更新一个表中的列中的值 - update values in a column in one table from another on condition in mysql 我可以使用LINQ取同一个表的两个不同的列值,并与另一个表的另一个列值进行连接吗? - Can I take two different column values of same table and connect with different one column values from another table by using LINQ? 约束问题,一列中的值需要以另一列(不同表)的值作为前缀 - Constraint issue, values from one column need to be prefixed with values of another (different table) 如何用另一个表的唯一值更新一个表的列值? - How can I update column values from one table with unique values from another? 如何将一个表中的列及其值添加到 SQL 中的另一个表中? - How can I add a column with its values from one table into another table in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM