oracle 每日一题-数据库约束
2017-02-15 13:32
531 查看
以往旧题索引: 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
c70d
),
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) 都不可能为负数
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)的乘积必须大于零。但是如果你把两个负数相乘,结果是正数。所以你可以存储一个订单物品,其单价和数量都为负数!
原始出处:
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
c70d
),
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)的乘积必须大于零。但是如果你把两个负数相乘,结果是正数。所以你可以存储一个订单物品,其单价和数量都为负数!
相关文章推荐
- 完整的解决oracle乱码shell处理(当每日导入数据库是英文字符集但需要导入中文字符集并且显示中文)
- Oracle 数据库唯一约束中的NULL的处理
- 数据库(学习整理)----3--Oracle创建表和设置约束
- oracle学习总结-------数据库的约束
- Oracle--数据库中的五种约束
- Oracle 中建表时数据库不检查外键约束的字段宽度是否一致
- 数据库没有对约束设置名称如何删除约束(oracle)
- 数据库 day60,61 Oracle入门,单行函数,多表查询,子查询,事物处理,约束,rownum分页,视图,序列,索引
- 数据库-Oracle主键约束和唯一索引的黑与白
- Oracle_数据库表的约束
- ORACLE 数据库(查询--主键外键约束)
- Oracle 数据库中关于对表字段约束的操作(设置、删除、查询)
- Oracle 中创建数据库表 随表添加外键约束的方法
- oracle 数据库的约束
- oracle数据库出现“批处理中出现错误: ORA-00001: 违反唯一约束条件”解决方法
- 解除约束的oracle的数据库备份方法
- 每日雕蟲一技[2013-04-10]Oracle一次性删除主键约束和这个索引特殊语法
- oracle 10g 学习之数据进行增删改查、数据库事务、约束(8)
- 源码-Oracle数据库管理-第六章-索引和约束-约束部分-Part 2
- Oracle数据库之约束