[英]SQL link columns between tables where neither column is UNIQUE
I've started learning SQL to be able to collect data on a production flow as our current method (excel spreadsheet) is outliving it's usefulness. 我已经开始学习SQL,以便能够在生产流程中收集数据,因为我们当前的方法(Excel电子表格)已不再有用。
I'm using SQlite3 to make the database, but I'm running into an issue where I'm trying to create relations between columns of separate tables, but neither column in the relation stores unique values. 我正在使用SQlite3制作数据库,但是遇到一个问题,我试图在独立表的列之间创建关系,但是关系中的任何列都不存储唯一值。
For example: 例如:
CREATE TABLE process1 (
Product1ID INTEGER PRIMARY KEY,
StartDate TEXT,
EndDate TEXT
);
CREATE TABLE process2 (
Product2ID INTEGER PRIMARY KEY,
StartDate TEXT,
EndDate TEXT
);
As soon as a product has finished process1, it enters process2, so I wanted to constrain any values in process2(StartDate) to be dates already found in process1(EndDate). 一旦产品完成process1,它就会进入process2,因此我想将process2(StartDate)中的任何值限制为process1(EndDate)中已经存在的日期。
But multiple products can start and end process1 (and process2) on the same date, so none of the Date columns in either table contain UNIQUE values, which I understand is a requirement of the Parent Key for Foreign Key constraints. 但是,多个产品可以在同一日期开始和结束process1(和process2),因此,两个表中的Date列都不包含UNIQUE值,据我了解,这是外键约束的父键的要求。
Is there a way to constrain one column of a table so that all values must already exist in another table's column without either column holding unique values? 有没有一种方法可以限制一个表的一列,以便所有值都必须已经存在于另一个表的列中,而任一列都没有唯一值?
CREATE TABLE products(
ProductID INTEGER PRIMARY KEY,
ProductName TEXT
);
CREATE TABLE process1 (
process1ID INTEGER PRIMARY KEY,
ProductID integer,
StartDate TEXT,
EndDate TEXT,
CONSTRAINT process1_productid_fkey FOREIGN KEY (productid)
REFERENCES products (productid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
CREATE TABLE process2 (
process2ID INTEGER PRIMARY KEY,
ProductID integer,
StartDate TEXT,
EndDate TEXT,
CONSTRAINT process1_productid_fkey FOREIGN KEY (productid)
REFERENCES products (productid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
then you can select with a join like this. 那么您可以选择像这样的联接。
Select * from products p
LEFT JOIN process1 p1 on p1.ProductID = p.ProductID
LEFT JOIN process2 p2 on p2.ProductID = p.ProductID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.