oracle 每日一题-数据库约束

来源:互联网 发布:精通matlab最优化计算 编辑:程序博客网 时间:2024/05/22 10:45
以往旧题索引:
http://www.itpub.net/forum.php?m ... eid&typeid=1808

原始出处:
http://www.plsqlchallenge.com/

作者:        ChrisSaxon 

运行环境:SQLPLUS, SERVEROUTPUT已打开

你已开始处理新应用程序。它在这些表中存储订单明细:

create table plch_customers (
  customer_id   int not null 
    primary key,
  customer_name varchar2(255) not null 
    unique
);

create table plch_orders (
  order_id       int not null 
    primary key,
  order_datetime date not null,
  customer_id    int not null
    references plch_customers ( customer_id )
);

create table plch_order_items (
  order_id    int not null
    references plch_orders ( order_id ),
  item_number int not null
    check ( item_number between 1 and 5 ),
  product_id  int not null,
  unit_cost   number(10, 2) not null,
  quantity    int not null,
  primary key ( order_id, item_number ),
  check ( ( unit_cost * quantity ) > 0 )
);

给定这个模式,下列哪个说法是正确的?

(A) 
所有的客户(plch_customers)都必须有不同的名字。

(B) 
每个订单(plch_orders)有且仅有一位客户。

(C) 
在给定的日期和时间,一名客户仅能下一个订单。

(D) 
在一个订单内,最多只能有五种不同产品。

(E) 

在订单中,单价(unit_cost) 或者 数量(quantity) 都不可能为负数

NUMBER ( precision, scale)
  scale表示数字小数点右边的位数,scale默认设置为0.  如果把scale设成负数,Oracle将把该数字取舍到小数点左边的指定位数。
   check ( item_number between 1 and 5 ),
check ( ( unit_cost * quantity ) > 0 )
 references plch_customers ( customer_id )

A: customer_name上有一个唯一键。所以这会禁止你在两行数据存储相同的客户名字。
B: 从plch_orders到plch_customers有一个非空的外键。所以你必须存储一个在plch_customers上存在的customer_id。在plch_orders上只有一个客户列,所以你不能在一个订单上有多个客户。
C: 不对!在( customer_id, order_datetime )上并没有一个唯一键或者主键。所以你可以在两行上为这些列存储相同的值。
D: 是的。Plch_order_items.item_number是一个整数,有一个校验约束来确保这个数值在1和5之间。所以你在这个列能够存储的值只有1, 2, 3, 4 或者 5。 和order_id一起,这个列构成了表的主键。所以给定一个order_id, 你只能够存储一次这些值。因此对一个订单来说,物品的数量最多为五。每个订单物品对应一种产品。所以你在一个订单上最多只能有五种产品!
E:不对!单价(unit_cost) 和数量(quantity)的乘积必须大于零。但是如果你把两个负数相乘,结果是正数。所以你可以存储一个订单物品,其单价和数量都为负数!

0 0
原创粉丝点击