복붙노트

[SQL] 세 번째 테이블에서 FK 제약 조건의 위반을 방지하기 위해 제약

SQL

세 번째 테이블에서 FK 제약 조건의 위반을 방지하기 위해 제약

내가 저장 발동에 원하는 제약 조건을 구현할 수 있습니다,하지만 난 일을 할 것입니다 외래 키 제약 조건의 집합을 정의 할 수 있는지 궁금하네요.

나는이 키 관계와 여러 개의 테이블을 가지고 :

NSNs
---
Id  PK

Solicitations
----
Id            PK
NSNId         FK - NSNs

Parts
-----
Id        PK
NSNId     FK - NSNs

BaseRFQs
-------
Id      PK
NSNId   FK - NSNs

RFQs
----
Id          PK
BaseRFQId   FK - BaseRFQs

BaseRFQsSols
------------
BaseRFQId PK/FK - BaseRFQs
SolId     PK/FK - Solicitations

RFQsSolsParts
-------------
RFQId     PK/FK - RFQs
SolId     PK/FK - Solicitations
PartId    PK/FK - Parts

내 질문이 있습니다 :

나는 (내가 잘못 이해하는 경우에 저를 수정하시기 바랍니다) 그것을 이해 제약 FOREIGN KEY 내가 부탁 해요 구문 있지만 나는 솔루션 oughtn't는 DBMS의 구현에 따라서 다릅니다 할 수 있도록 약, ANSI-준수, MySQL을 사용하고 있습니다.

편집 : 그들이 지금 서로 드류의 요청 @, 여기에 테이블 정의입니다 당 :

CREATE TABLE `nsns` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NSN` char(16) NOT NULL,
  `Description` varchar(100) DEFAULT NULL,
  `ShortDesc` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `NSN_UNIQUE` (`NSN`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=latin7

CREATE TABLE `solicitations` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NSNId` int(11) NOT NULL,
  `UOMId` int(11) NOT NULL DEFAULT '1',
  `QUPId` int(11) NOT NULL DEFAULT '0',
  `SolicitationNo` char(16) NOT NULL,
  `Quantity` int(11) NOT NULL,
  `ReturnByDate` date NOT NULL,
  `StatusId` int(11) NOT NULL DEFAULT '1',
  `Memo` text,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `SolicitationNo_UNIQUE` (`SolicitationNo`),
  KEY `NSN_idx` (`NSNId`)
  CONSTRAINT `NSNId` FOREIGN KEY (`NSNId`) REFERENCES `nsns` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin7

CREATE TABLE `parts` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NSNId` int(11) NOT NULL,
  `VendorId` int(11) NOT NULL,
  `UOMId` int(11) NOT NULL DEFAULT '1',
  `QUPId` int(11) NOT NULL DEFAULT '1',
  `StatusId` int(11) DEFAULT '1',
  `PartNo` varchar(45) DEFAULT NULL,
  `Memo` text,
  PRIMARY KEY (`ID`)
  KEY `NSN_idx` (`NSNId`)
  CONSTRAINT `NSNId` FOREIGN KEY (`NSNId`) REFERENCES `nsns` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin7

CREATE TABLE `baserfqs` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NSNId` int(11) NOT NULL,
  `BRFQNo` varchar(45) DEFAULT NULL,
  `Memo` text,
  `Finalized` bit(1) NOT NULL DEFAULT b'0',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `BRFQNo_UNIQUE` (`BRFQNo`),
  KEY `NSN_idx` (`NSNId`),
  CONSTRAINT `NSNId` FOREIGN KEY (`NSNId`) REFERENCES `nsns` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin7

CREATE TABLE `rfqs` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `BaseRFQId` int(11) NOT NULL,
  `VendorId` int(11) NOT NULL,
  `RFQNo` varchar(45) NOT NULL,
  `StatusId` int(11) NOT NULL DEFAULT '6',
  `DateSent` date DEFAULT NULL,
  `DateResponded` date DEFAULT NULL,
  `VendorNotes` text,
  `QuotedBy` varchar(45) DEFAULT NULL,
  `Title` varchar(45) DEFAULT NULL,
  `ValidityCodeId` int(11) DEFAULT '4',
  `UnitWt` decimal(10,3) DEFAULT NULL,
  `WtUOMId` int(11) DEFAULT '1',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `RFQNo_UNIQUE` (`RFQNo`),
  KEY `BaseRFQId_idx` (`BaseRFQId`),
  KEY `VendorId_idx` (`VendorId`),
  KEY `StatusId_idx` (`StatusId`),
  KEY `ValidityCodeId_idx` (`ValidityCodeId`),
  KEY `WtUOMId_idx` (`WtUOMId`),
  CONSTRAINT `WtUOMId` FOREIGN KEY (`WtUOMId`) REFERENCES `wtuoms` (`ID`),
  CONSTRAINT `BaseRFQId` FOREIGN KEY (`BaseRFQId`) REFERENCES `baserfqs` (`ID`),
  CONSTRAINT `StatusId` FOREIGN KEY (`StatusId`) REFERENCES `rfqstatus` (`ID`),
  CONSTRAINT `ValidityCodeId` FOREIGN KEY (`ValidityCodeId`) REFERENCES `validitycodes` (`ID`),
  CONSTRAINT `VendorId` FOREIGN KEY (`VendorId`) REFERENCES `vendors` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin7

CREATE TABLE `baserfqssols` (
  `BaseRFQId` int(11) NOT NULL,
  `SolId` int(11) NOT NULL,
  `LineItemNo` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`BaseRFQId`,`SolId`),
  KEY `RFQ_idx` (`BaseRFQId`),
  KEY `Solicitation_idx` (`SolId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin7

CREATE TABLE `rfqssolsparts` (
  `RFQId` int(11) NOT NULL,
  `SolId` int(11) NOT NULL,
  `PartId` int(11) NOT NULL,
  `CondId` int(11) NOT NULL,
  `UOMId` int(11) NOT NULL DEFAULT '1',
  `QUPId` int(11) NOT NULL DEFAULT '1',
  `UnitPrice` decimal(10,3) NOT NULL,
  `LeadTime` int(11) DEFAULT NULL,
  `LTCId` int(11) DEFAULT NULL,
  `SplsNSNId` int(11) DEFAULT NULL,
  `SetupCostInc` bit(1) NOT NULL DEFAULT b'0',
  `CertCostInc` bit(1) NOT NULL DEFAULT b'0',
  `MfgCerts` bit(1) NOT NULL DEFAULT b'0',
  `Altered` bit(1) NOT NULL DEFAULT b'0',
  `OrigPkg` bit(1) NOT NULL DEFAULT b'1',
  `SplsContNo` varchar(45) DEFAULT NULL,
  `SplsDate` date DEFAULT NULL,
  PRIMARY KEY (`RFQId`,`SolId`,`PartId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin7

해결법

  1. ==============================

    1.나는 당신의 고체 solitications에 대한 생각 했어요. 나는 원래가 (바위처럼) 고체 의미 생각했다. 그래서,이 구성을 통해 수행 할 수 있습니다.

    나는 당신의 고체 solitications에 대한 생각 했어요. 나는 원래가 (바위처럼) 고체 의미 생각했다. 그래서,이 구성을 통해 수행 할 수 있습니다.

    제공되는 스키마에 몇 오타 오류가 있다고합니다. 일부 누락 된 쉼표, 일부는 인덱스 이름을 속아. MyISAM 테이블은 INNODB로 변경되었습니다. 그래서 몇 가지 이름을 변경했다. 또한, 주변에없는 테이블이 있었다 표 5. 그래서 스크립트가 (테이블 RFQ를 위해) 실행됩니다 것처럼 없습니다.

    마찬가지로, 다음 스키마가 제공 누락 된 테이블로 인해 실패 할 곳 60 그것을 통해 70 % 것입니다.

    지금까지 테이블 :

    create schema slipper;
    use slipper;
    
    CREATE TABLE `nsns` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `NSN` char(16) NOT NULL,
      `Description` varchar(100) DEFAULT NULL,
      `ShortDesc` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`ID`),
      UNIQUE KEY `NSN_UNIQUE` (`NSN`)
    ) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=latin7;
    
    drop table if exists `solicitations`;
    CREATE TABLE `solicitations` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `NSNId` int(11) NOT NULL,
      `UOMId` int(11) NOT NULL DEFAULT '1',
      `QUPId` int(11) NOT NULL DEFAULT '0',
      `SolicitationNo` char(16) NOT NULL,
      `Quantity` int(11) NOT NULL,
      `ReturnByDate` date NOT NULL,
      `StatusId` int(11) NOT NULL DEFAULT '1',
      `Memo` text,
      PRIMARY KEY (`ID`),
      UNIQUE KEY `SolicitationNo_UNIQUE` (`SolicitationNo`),
      KEY `NSN_idx1111` (`NSNId`),
      KEY `NSN_idx1112` (`ID`,`NSNId`), -- atm an necessary evil. Revisit, perhaps collapse one
      CONSTRAINT `NSNId` FOREIGN KEY (`NSNId`) REFERENCES `nsns` (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin7;
    
    drop table if exists `parts`;
    CREATE TABLE `parts` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `NSNId` int(11) NOT NULL,
      `VendorId` int(11) NOT NULL,
      `UOMId` int(11) NOT NULL DEFAULT '1',
      `QUPId` int(11) NOT NULL DEFAULT '1',
      `StatusId` int(11) DEFAULT '1',
      `PartNo` varchar(45) DEFAULT NULL,
      `Memo` text,
      PRIMARY KEY (`ID`),
      KEY `NSN_idx2222` (`NSNId`),
      KEY `NSN_idx2223` (`ID`,`NSNId`), -- atm an necessary evil. Revisit, perhaps collapse one
      CONSTRAINT `NSNId2222` FOREIGN KEY (`NSNId`) REFERENCES `nsns` (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin7;
    
    drop table if exists `baserfqs`;
    CREATE TABLE `baserfqs` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `NSNId` int(11) NOT NULL,
      `BRFQNo` varchar(45) DEFAULT NULL,
      `Memo` text,
      `Finalized` bit(1) NOT NULL DEFAULT b'0',
      PRIMARY KEY (`ID`),
      UNIQUE KEY `BRFQNo_UNIQUE` (`BRFQNo`),
      KEY `NSN_idx4444` (`NSNId`),
      KEY `NSN_idx4445` (`ID`,`NSNId`), -- atm an necessary evil. Revisit, perhaps collapse one
      CONSTRAINT `NSNId4444` FOREIGN KEY (`NSNId`) REFERENCES `nsns` (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin7;
    
    CREATE TABLE `rfqs` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `BaseRFQId` int(11) NOT NULL,
      `VendorId` int(11) NOT NULL,
      `RFQNo` varchar(45) NOT NULL,
      `StatusId` int(11) NOT NULL DEFAULT '6',
      `DateSent` date DEFAULT NULL,
      `DateResponded` date DEFAULT NULL,
      `VendorNotes` text,
      `QuotedBy` varchar(45) DEFAULT NULL,
      `Title` varchar(45) DEFAULT NULL,
      `ValidityCodeId` int(11) DEFAULT '4',
      `UnitWt` decimal(10,3) DEFAULT NULL,
      `WtUOMId` int(11) DEFAULT '1',
      PRIMARY KEY (`ID`),
      UNIQUE KEY `RFQNo_UNIQUE` (`RFQNo`),
      KEY `BaseRFQId_idx` (`BaseRFQId`),
      KEY `VendorId_idx` (`VendorId`),
      KEY `StatusId_idx` (`StatusId`),
      KEY `ValidityCodeId_idx` (`ValidityCodeId`),
      KEY `WtUOMId_idx` (`WtUOMId`),
      CONSTRAINT `WtUOMId` FOREIGN KEY (`WtUOMId`) REFERENCES `wtuoms` (`ID`),
      CONSTRAINT `BaseRFQId` FOREIGN KEY (`BaseRFQId`) REFERENCES `baserfqs` (`ID`),
      CONSTRAINT `StatusId` FOREIGN KEY (`StatusId`) REFERENCES `rfqstatus` (`ID`),
      CONSTRAINT `ValidityCodeId` FOREIGN KEY (`ValidityCodeId`) REFERENCES `validitycodes` (`ID`),
      CONSTRAINT `VendorId` FOREIGN KEY (`VendorId`) REFERENCES `vendors` (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin7;
    
    
    drop table if exists `compTableX001`;
    CREATE TABLE `compTableX001`
    (  -- a composition table for FK's in `baserfqssols`
      `ID` int(11) AUTO_INCREMENT PRIMARY KEY,
      `BaseRFQId` int(11) NOT NULL,     -- baserfqs.ID
      `SolId` int(11) NOT NULL,         -- solicitations.ID
      `NSNId` int(11) NOT NULL,
      unique key (`BaseRFQId`,`SolId`), -- no dupes allowed  
      CONSTRAINT `tx001_base` FOREIGN KEY (`BaseRFQId`,`NSNId`) REFERENCES `baserfqs` (`ID`,`NSNId`),
      CONSTRAINT `tx001_sol` FOREIGN KEY (`SolId`,`NSNId`) REFERENCES `solicitations` (`ID`,`NSNId`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin7; 
    
    drop table if exists `compTableX002`;
    CREATE TABLE `compTableX002`
    (  -- a composition table for FK's in `rfqssolsparts`
      `ID` int(11) AUTO_INCREMENT PRIMARY KEY,
      `BaseRFQId` int(11) NOT NULL,     -- baserfqs.ID
      `SolId` int(11) NOT NULL,         -- solicitations.ID
      `PartId` int(11) NOT NULL,        -- parts.ID
      `NSNId` int(11) NOT NULL,
      unique key (`BaseRFQId`,`SolId`,`PartId`), -- no dupes allowed 
      CONSTRAINT `tx002_base` FOREIGN KEY (`BaseRFQId`,`NSNId`) REFERENCES `baserfqs` (`ID`,`NSNId`),
      CONSTRAINT `tx002_sol` FOREIGN KEY (`SolId`,`NSNId`) REFERENCES `solicitations` (`ID`,`NSNId`),
      CONSTRAINT `tx002_part` FOREIGN KEY (`PartId`,`NSNId`) REFERENCES `parts` (`ID`,`NSNId`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin7; 
    
    drop table if exists `baserfqssols`;
    CREATE TABLE `baserfqssols` (
      `ID` int(11) auto_increment,
      `compId` int(11) NOT NULL,    -- composition ID `compTableX001`.`ID`
      `LineItemNo` int(11) NOT NULL DEFAULT '1',
      PRIMARY KEY (`ID`),
      CONSTRAINT `basesol_compX001` FOREIGN KEY (`compId`) REFERENCES `compTableX001` (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin7; -- changed engine type
    
    -- Is it possible to set up a foreign key constraint on RFQsSolsParts that requires SolId and PartId to reference records
    -- that have the same NSNId, and requires RFQId to reference a BaseRFQId which has the same NSNId as the other two?
    
    drop table if exists `rfqssolsparts`;
    CREATE TABLE `rfqssolsparts` (
      -- `RFQId` int(11) NOT NULL,      -- requirement BBBBBBBBBBBBB
      -- `SolId` int(11) NOT NULL,      -- requirement AAAAAAAAA
      -- `PartId` int(11) NOT NULL,     -- requirement AAAAAAAAA
      `ID` int(11) auto_increment,
      `compId` int(11) NOT NULL, -- composition ID `compTableX002`.`ID`
      `CondId` int(11) NOT NULL,
      `UOMId` int(11) NOT NULL DEFAULT '1',
      `QUPId` int(11) NOT NULL DEFAULT '1',
      `UnitPrice` decimal(10,3) NOT NULL,
      `LeadTime` int(11) DEFAULT NULL,
      `LTCId` int(11) DEFAULT NULL,
      `SplsNSNId` int(11) DEFAULT NULL,
      `SetupCostInc` bit(1) NOT NULL DEFAULT b'0',
      `CertCostInc` bit(1) NOT NULL DEFAULT b'0',
      `MfgCerts` bit(1) NOT NULL DEFAULT b'0',
      `Altered` bit(1) NOT NULL DEFAULT b'0',
      `OrigPkg` bit(1) NOT NULL DEFAULT b'1',
      `SplsContNo` varchar(45) DEFAULT NULL,
      `SplsDate` date DEFAULT NULL,
      -- PRIMARY KEY (`RFQId`,`SolId`,`PartId`) 
      PRIMARY KEY (`ID`),
      CONSTRAINT `triplet_compX002` FOREIGN KEY (`compId`) REFERENCES `compTableX002` (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin7;
    

    compTableX001 이름 ID로 TwoParents - OneChild 미니 계층 같다. 그래서 ID는 부모의 이름입니다. 그것은 두 부모 (BaseRFQId 고체), 한 아이 (NSNId)가 있습니다. 이름 또는 식별자 ID로서, 그것이 지원하는 baserfqssols 행의 FK 대상이다. 참조 된 각각 참조하기.

    마찬가지로, compTableX002는 질문 2에 대한 현재 상태를 해결하기 위해 나타납니다.

    대청소:

    drop schema slipper;
    
  2. from https://stackoverflow.com/questions/38339713/constraint-to-prevent-violation-of-fk-constraint-in-a-third-table by cc-by-sa and MIT license