30.Evaluate the following CREATE TABLE commands:

30.Evaluate the following CREATE TABLE commands:
CREATE TABLE orders(
       ord_no NUMBER(2) CONSTRAINT ord_pk PRIMARY KEY
       ,ord_date DATE
       ,cust_id NUMBER(4)
);
CREATE TABLE ord_items(
       ord_no NUMBER(2)
       ,item_no NUMBER(3)
       ,qty NUMBER(3) CHECK (qty BETWEEN 100 AND 200)
       ,expiry_date date CHECK (expiry_date > SYSDATE)
       ,CONSTRAINT it_pk PRIMARY KEY (ord_no,item_no)
       ,CONSTRAINT ord_fk FOREIGN KEY(ord_no) REFERENCES orders(ord_no)
);
The above command fails when executed. What could be the reason?
A.SYSDATE cannot be used with the CHECK constraint.
B.The BETWEEN clause cannot be used for the CHECK constraint.
C.The CHECK constraint cannot be placed on columns having the DATE data type.
D.ORD_NO and ITEM_NO cannot be used as a composite primary key because ORD_NO is also the FOREIGN KEY.
答案:A

A:正确

SQL> create table test( a date ,constraints ck_a check(a<sysdate));
create table test( a date ,constraints ck_a check(a<sysdate))
ORA-02436: 日期或系统变量在 CHECK 约束条件中指定错误
B:错误,between可以用于check约束

SQL> create table test(a number,constraints ck_a check(a between 1 and 100));
Table created
C:错误,date类型的列可以使用check约束

SQL> create table test(a date,constraints ck_a check(a<to_date('2015-12-12','yyyy-mm-dd')));
Table created
SQL>
D:错误
可以作为主键,因为他们是联合主键,如果这里只是将ord_no作为主键的话,就会有问题,
因为他上面已经存在了外键了

--判断foreign 和primary
SQL> create table test1(a date,constraints pk_a primary key(a),
  2  constraints fk_a foreign key(a) references test(a));
create table test1(a date,constraints pk_a primary key(a),
constraints fk_a foreign key(a) references test(a))
ORA-02270: 此列列表的唯一关键字或主键不匹配
--判断check 和primary
SQL> create table test1(a date,constraints pk_a primary key(a),
  2  constraints ck_test1_a check(a<to_date('2015-12-12','yyyy-mm-dd')));
Table created
--判断unique和primary
SQL> create table test1(a date,constraints pk_a primary key(a),
  2   constraints un_test1_a unique(a));
create table test1(a date,constraints pk_a primary key(a),
 constraints un_test1_a unique(a))
ORA-02261: 表中已存在这样的唯一关键字或主键
--判断null和prmay
create table test1 ( a date primary key constraints nu_a null);
这里尽然可以??但是查看user_constraints却没有这个的信息

SQL> select constraint_name,constraint_type from user_constraints where table_name ='TEST1';
CONSTRAINT_NAME                CONSTRAINT_TYPE
------------------------------ ---------------
SYS_C0030903                   P
--没有null约束的信息
SQL> select nullable from user_tab_columns where table_name='TEST1';
NULLABLE
--------
N
--不能为null
虽然成功了,但是找不到任何null约束的信息,也不起作用


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值