简体   繁体   English

表之间的SQL链接列,其中两个列都不是唯一的

[英]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.

相关问题 SQL用于获取唯一键匹配的数据,但不同表之间的某些其他列中的数据不同 - SQL to fetch data where Unique key matches but the data is different in some other columns between different tables SQL 获取列唯一的所有列 - SQL Get all columns where column is unique 在不使用唯一列或键列的情况下,在两个SQL Server表(父列到子列)之间定义关系 - Define a relationship between two SQL Server tables (parent to child column) without the use of unique or key columns 有没有办法使用 SQL 定义使用 2 个非唯一列的 2 个表之间的关系 - Is there a way to define the relationship between 2 tables using 2 non-unique columns columns using SQL 合并3个表,其中2列的组合不是唯一的 - combining 3 tables where combination of 2 columns is not unique SQL-检索单个列的唯一值,其中多个列可能是唯一的 - SQL - Retrieve Unique Values of single column where by multiple columns may be unique Sql Link 3表,但仍显示其中之一 - Sql Link 3 Tables but still show unique of 1 of them 两个表在唯一键上的SQL连接列 - SQL join columns of two tables on unique key 具有跨多个表的列的 SQL 唯一约束 - SQL Unique Constraint with columns spanning multiple tables 在表之间没有严格唯一的公共字段的情况下,是否可以匹配SQL查询中的“下一个”不匹配记录? - Is it possible to match the “next” unmatched record in a SQL query where there is no strictly unique common field between tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM