- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我可以在存储过程中实现我想要的约束,但我想知道我是否可以定义一组外键约束来完成这项工作。
我有几个表,具有以下关键关系:
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
我的问题是:
我正在使用 MySQL,尽管据我所知(如果我理解错误请纠正我)我所询问的 CONSTRAINT FOREIGN KEY 语法符合 ANSI,因此解决方案不应因 DBMS 实现而异。
编辑:根据@Drew 的要求,这里是现在的表定义:
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
最佳答案
我认为您的 solId
用于征集
。我原本以为它的意思是坚固的(像一 block 石头)。因此,这可以通过组合来完成。
请注意,所提供的架构中存在一些拼写错误。一些缺少逗号,一些欺骗了索引名称。 MyISAM 表更改为 INNODB。所以我做了一些重命名。此外,在 table5 周围缺少表格。因此,您的脚本不会运行(对于表 rfqs
)。
同样,由于您缺少提供的表,以下架构将失败,大约有 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
是 parent 身份的名称。它有两个父项(BaseRFQId
和 SolId
)和一个子项(NSNId
)。名称或标识符,如 ID
,是它支持的 baserfqssols
行的 FK 目标。 Referenced 和Referencing,分别。
类似地,compTableX002
现在似乎可以解决问题 2 的条件。
清理:
drop schema slipper;
关于mysql - 约束以防止在第三个表中违反 FK 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339713/
我可以添加一个检查约束来确保所有值都是唯一的,但允许默认值重复吗? 最佳答案 您可以使用基于函数的索引 (FBI) 来实现此目的: create unique index idx on my_tabl
嗨,我在让我的约束在grails项目中工作时遇到了一些麻烦。我试图确保Site_ID的字段不留为空白,但仍接受空白输入。另外,我尝试设置字段显示的顺序,但即使尝试时也无法反射(reflect)在页面上
我似乎做错了,我正在尝试将一个字段修改为外键,并使用级联删除...我做错了什么? ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field
阅读目录 1、约束的基本概念 2、约束的案例实践 3、外键约束介绍 4、外键约束展示 5、删除
SQLite 约束 约束是在表的数据列上强制执行的规则。这些是用来限制可以插入到表中的数据类型。这确保了数据库中数据的准确性和可靠性。 约束可以是列级或表级。列级约束仅适用于列,表级约束被应用到整
我在 SerenityOS project 中偶然发现了这段代码: template void dbgln(CheckedFormatString&& fmtstr, const Parameters
我有表 tariffs,有两列:(tariff_id, reception) 我有表 users,有两列:(user_id, reception) 我的表 users_tariffs 有两列:(use
在 Derby 服务器中,如何使用模式的系统表中的信息来创建选择语句以检索每个表的约束名称? 最佳答案 相关手册是Derby Reference Manual .有许多可用版本:10.13 是 201
我正在使用 z3py 进行编码。请参阅以下示例。 from z3 import * x = Int('x') y = Int('y') s = Solver() s.add(x+y>3) if s.c
非常快速和简单的问题。我正在运行一个脚本来导入数据并声明了一个临时表并将检查约束应用于该表。显然,如果脚本运行不止一次,我会检查临时表是否已经存在,如果存在,我会删除并重新创建临时表。这也会删除并重新
我有一个浮点变量 x在一个线性程序中,它应该是 0或两个常量之间 CONSTANT_A和 CONSTANT_B : LP.addConstraint(x == 0 OR CONSTANT_A <= x
我在使用grails的spring-data-neo4j获得唯一约束时遇到了一些麻烦。 我怀疑这是因为我没有正确连接它,但是存储库正在扫描和连接,并且CRUD正在工作,所以我不确定我做错了什么。 我正
这个问题在这里已经有了答案: Is there a constraint that restricts my generic method to numeric types? (24 个回答) 7年前
我有一个浮点变量 x在一个线性程序中,它应该是 0或两个常量之间 CONSTANT_A和 CONSTANT_B : LP.addConstraint(x == 0 OR CONSTANT_A <= x
在iOS的 ScrollView 中将图像和带有动态文本(动态高度)的标签居中的最佳方法是什么? 我必须添加哪些约束?我真的无法弄清楚它是如何工作的,也许我无法处理它,因为我是一名 Android 开
考虑以下代码: class Foo f class Bar b newtype D d = D call :: Proxy c -> (forall a . c a => a -> Bool) ->
我有一个类型类,它强加了 KnownNat约束: class KnownNat (Card a) => HasFin a where type Card a :: Nat ... 而且,我有几
我知道REST原则上与HTTP无关。 HTTP是协议,REST是用于通过Web传输hypermedia的体系结构样式。 REST可以使用诸如HTTP,FTP等的任何应用程序层协议。关于REST的讨论很
我有这样的情况,我必须在数据库中存储复杂的数据编号。类似于 21/2011,其中 21 是文件编号,但 2011 是文件年份。所以我需要一些约束来处理唯一性,因为有编号为 21/2010 和 21/2
我有一个 MySql (InnoDb) 表,表示对许多类型的对象之一所做的评论。因为我正在使用 Concrete Table Inheritance ,对于下面显示的每种类型的对象(商店、类别、项目)
我是一名优秀的程序员,十分优秀!