Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 引用一个或多个列的复合外键_Mysql_Sql_Database Design_Foreign Keys_Composite Key - Fatal编程技术网

Mysql 引用一个或多个列的复合外键

Mysql 引用一个或多个列的复合外键,mysql,sql,database-design,foreign-keys,composite-key,Mysql,Sql,Database Design,Foreign Keys,Composite Key,我有一个表(表a),将其他3个表与主键“vehicle”、“engine”、“transmission”连接起来。我希望能够将零件分配给其中一个或多个表,例如“only this vehicle”或“only this vehicle with this engine”或“any vehicle with this engine” 我的计划是有一个零件表(表B),其中还包括主键“车辆”、“发动机”、“变速器”,我希望能够插入例如: 4844,null,null仅将零件分配给“车辆”或 4844

我有一个表(表a),将其他3个表与主键“vehicle”、“engine”、“transmission”连接起来。我希望能够将零件分配给其中一个或多个表,例如“only this vehicle”或“only this vehicle with this engine”或“any vehicle with this engine”

我的计划是有一个零件表(表B),其中还包括主键“车辆”、“发动机”、“变速器”,我希望能够插入例如:

  • 4844,null,null
    仅将零件分配给“车辆”或
  • 4844240,null
    将零件分配给“仅此发动机车辆”
是否有某种方法可以在数据库级别强制执行完整性,以便

  • 表B中填写的字段必须引用表A中的字段
  • 必须至少填写一个字段
  • 是否存在不将数据全部插入其中的选项
  • 阅读数据模型模式。有几本好书(hay,fowler,silverston,blaha),hay很好地涵盖了这类内容

  • 如果你想做这类事情,请使用像postgres这样有检查约束的真实数据库。对于你的问题2和3,这很容易通过检查解决