[英]Enforcing integrity through predefined database lists
Apologies for the terribly worded question (will change it once I work out how to define this problem better). 为这个措辞很深的问题表示歉意(一旦我确定了如何更好地定义这个问题,它将改变它)。
Lets say I have a table in a database called Supplier
. 可以说我在名为
Supplier
的数据库中有一个表。 A supply can have a vendor id
as PK, and country
. 供应可以具有
vendor id
作为PK和country
。 The supplier must be working on one, and only one project from a set of three pre-defined project names. 供应商必须在一个项目中工作,并且在三个预定义的项目名称集中只有一个项目。
Would the best way to capture this data be: 捕获此数据的最佳方法是:
Create a table called Project
which has a Project ID
as PK and 'Project Name' field. 创建一个名为
Project
的表,该表具有作为PK的Project ID
和“项目名称”字段。 Create another table called SupplierProject
which inherits vendor id
and Project ID
as primary keys (or is that primary and alternative keys?). 创建另一个名为
SupplierProject
表,该表继承vendor id
和Project ID
作为主键(或者是主键和替代键?)。
I am thinking in that way, if a project name was ever updated you would only need to update one field in the Project
table and not every instance of the project name if instead a field was created in Supplier
table called Project Name
. 我正在以这种方式思考,如果项目名称曾经被更新过,那么如果在
Supplier
表中创建了一个名为Project Name
字段,则只需要更新Project
表中的一个字段,而不是项目名称的每个实例。
It should be noted that multiple companies could be working on the same project at the same time, but they need to be one of the pre-defined projects from the 'list'. 应该注意的是,多个公司可能同时在同一个项目上工作,但是它们必须是“列表”中预定义的项目之一。
Does this also increase data integrity has can't enter something which is not on the list? 不能输入列表中未包含的内容是否也提高了数据完整性?
Is this a normal thing to do in this situation, sort of creating pick tables
through a many-to-many relationship structure? 在这种情况下,通过多对多关系结构创建
pick tables
,这是正常的事情吗? I am confused because technically the supplier must work on one, and only one project at any time. 我很困惑,因为从技术上讲,供应商必须同时从事一个项目,并且只能进行一个项目。 This structure gives me the pick list but doesn't enforce this rule.
此结构为我提供了选择列表,但没有执行此规则。
Hope this is making sense and I am not completely missing the point. 希望这是有道理的,我不会完全忽略这一点。 Any advice on the best way to handle these situations is much appreciated!
任何有关处理这些情况的最佳方法的建议,将不胜感激!
In MySQL, you want a Projects
table that contains the list of allowed projects. 在MySQL中,您需要一个
Projects
表,其中包含允许的项目列表。
In the Suppliers
table, you want a project id and a foreign key reference. 在“
Suppliers
表中,您需要一个项目ID和一个外键引用。
Something like this: 像这样:
create table Projects (
ProjectId int not null auto_increment primary key,
ProjectName varchar(255) unique
);
create table Suppliers (
SupplierId int not null auto_increment primary key,
. . .
ProjectId int not null,
. . .
foreign key ProjectId references Projects(ProjectId)
);
Then populate Projects
with the values you are allowing. 然后使用您允许的值填充“
Projects
”。 Each supplier then has to refer to one of those valid values (the not null
requires a reference). 然后,每个供应商都必须引用这些有效值之一(
not null
要求引用)。
In other databases, you could do this with just a check constraint
in the Suppliers
table. 在其他数据库中,您可以仅在
Suppliers
表中使用check constraint
来执行此操作。 MySQL allows you to write a check constraint
, but it doesn't actually do the checking. MySQL允许您编写
check constraint
,但实际上并没有进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.