首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >(Oracle) SQL DDL -完整性约束

(Oracle) SQL DDL -完整性约束
EN

Stack Overflow用户
提问于 2013-10-16 18:33:59
回答 2查看 469关注 0票数 0

我的教授希望我们在这段代码中为我们的Database类填充一些空白。我认为我这样做是正确的,但我不确定我是否正确,因为我们刚刚开始讨论这个主题。如果有人能给我一些指导或有用的资源,我不知道从哪里开始第五个约束(IC5)。

代码语言:javascript
运行
复制
SPOOL ddl.out 
SET ECHO ON 
-- 
-- Author:
-- 
-- IMPORTANT: use the names IC-1, IC-2, etc. as given below. 
-- -------------------------------------------------------------------- 
DROP TABLE Orders CASCADE CONSTRAINTS; 
DROP TABLE OrderLine CASCADE CONSTRAINTS; 
-- 
CREATE TABLE Orders 
( 
orderNum INTEGER PRIMARY KEY, 
priority CHAR(10) NOT NULL, 
cost INTEGER NOT NULL, 
/* 
IC1: The priority is one of: high, medium, or low 
*/ 
CHECK priority=('high' OR 'medium' OR 'low'),
/* 
IC2: The cost of a high priority order is above 2000. 
*/ 
CHECK priority='high' AND cost>2000,
/* 
IC3: The cost of a medium priority order is between 800 and 2200 (inclusive). 
*/ 
CHECK priority='medium' AND cost BETWEEN 800 AND 2200,
/* 
IC4: The cost of a low priority order is less than 1000. 
*/ 
CHECK priority='low' AND cost<1000,
); 
-- 
-- 
CREATE TABLE OrderLine 
( 
orderNum INTEGER, 
lineNum INTEGER, 
item CHAR (10) NOT NULL, 
quantity INTEGER, 
PRIMARY KEY (orderNum, lineNum), 
/* 
IC5: Every order line must belong to an order in the Order table. 
Also: if an order is deleted then all its order lines must be deleted. 
IMPORTANT: DO NOT declare this IC as DEFERRABLE. 
*/ 
<<< YOUR SQL CODE GOES HERE >>> 
); 
-- 
-- ---------------------------------------------------------------- 
-- TESTING THE SCHEMA 
-- ---------------------------------------------------------------- 
INSERT INTO Orders VALUES (10, 'high', 2400); 
INSERT INTO Orders VALUES (20, 'high', 1900); 
INSERT INTO Orders VALUES (30, 'high', 2100); 
INSERT INTO Orders VALUES (40, 'medium', 700); 
INSERT INTO Orders VALUES (50, 'low', 1100); 
INSERT INTO Orders VALUES (60, 'low', 900); 
SELECT * from Orders; 
EN

回答 2

Stack Overflow用户

发布于 2013-10-16 18:36:10

您需要的是一个外键约束。

票数 1
EN

Stack Overflow用户

发布于 2013-10-16 18:38:29

IC5:

代码语言:javascript
运行
复制
FOREIGN KEY (orderNum) REFERENCES Orders (orderNum) ON DELETE CASCADE
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19401173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档