[20121020]主外键约束以及NULL问题.txt

简介: [20121020]主外键约束以及NULL问题.txt主外键约束可以一定程度保证数据完整性,但是如果外键输入的是NULL,情况会如何呢?SQL> select * from v$version ;BANNER-----------------------...
[20121020]主外键约束以及NULL问题.txt

主外键约束可以一定程度保证数据完整性,但是如果外键输入的是NULL,情况会如何呢?

SQL> select * from v$version ;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

1.测试1:

drop table t1 purge;
drop table t2 purge;

create table t1( id number, name varchar2(10));
alter table t1 add constraint pk_t1 unique (id, name);

SQL> desc t1;
Name   Null?    Type
------ -------- ----------------
ID              NUMBER
NAME            VARCHAR2(10)

create table t2( id2 number ,id number, name varchar2(10));

alter table t2 add constraint fk_t2 foreign key(id, name) references t1(id, name);

insert into t1 values(1,'test');
commit ;

insert into t2 values(1,2,'test');
insert into t2 values(2,1,'aaaa');
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK_T2) violated - parent key not found

--但是当我们插入空值时:

insert into t2 values(3,1,null);
insert into t2 values(4,2,null);
insert into t2 values(5,null,'aaaa');
insert into t2 values(6,null,'bbbb');
insert into t2 values(7,null,null);
commit ;

SQL> set NULL NULL
SQL> select * from t2;

       ID2         ID NAME
---------- ---------- ----------
         3          1 NULL
         4          2 NULL
         5 NULL       aaaa
         6 NULL       bbbb
         7 NULL       NULL

2.测试2:

drop table t1 purge;
drop table t2 purge;

create table t1( id number, name varchar2(10));
alter table t1 add constraint pk_t1 primary key (id, name);

SQL> desc t1
Name   Null?    Type
------ -------- -------------
ID     NOT NULL NUMBER
NAME   NOT NULL VARCHAR2(10)

create table t2( id2 number ,id number, name varchar2(10));

alter table t2 add constraint fk_t2 foreign key(id, name) references t1(id, name);

insert into t1 values(1,'test');
commit ;

insert into t2 values(1,2,'test');
insert into t2 values(2,1,'aaaa');
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK_T2) violated - parent key not found

--但是当我们插入空值时:

insert into t2 values(3,1,null);
insert into t2 values(4,2,null);
insert into t2 values(5,null,'aaaa');
insert into t2 values(6,null,'bbbb');
insert into t2 values(7,null,null);
commit ;

SQL> set NULL NULL
SQL> select * from t2;
       ID2         ID NAME
---------- ---------- ----------
         3          1 NULL
         4          2 NULL
         5 NULL       aaaa
         6 NULL       bbbb
         7 NULL       NULL

3.总结:
    
    所以要保证完整性,还要定义外键NOT NULL.
目录
相关文章
|
SQL Oracle 关系型数据库
[20170516]nvl与非NULL约束.txt
[20170516]nvl与非NULL约束.txt --前几天做的测试http://blog.itpub.net/267265/viewspace-2137853/,实际上差异没有这个大,因为第2个多数是常量.
854 0
|
索引 关系型数据库 Oracle
[20160704]NULL与主外键问题.txt
[20160704]NULL与主外键问题.txt --主外键的问题主要出现在阻塞等情况,有许多极端dba认为应该取消这个约束.当然从使用的角度出发有总比没有好.只是不要过度滥用.
772 0
|
存储 Oracle 关系型数据库
[20160619]NULL在数据库的存储.txt
[20160619]NULL在数据库的存储.txt --简单探究NULL在数据库的存储.这也是别人前几天问的问题,我自己学习oracle这么久,也没有仔细观察过. 1.
840 0
|
Oracle 关系型数据库 Linux
[20151207]filter( IS NULL).txt
[20151207]filter( IS NULL).txt --前一阵子别人问的问题,filter (IS NOT NULL)是什么意思? -- http://www.
776 0
|
Oracle 关系型数据库
[20140823]12c null与缺省值.txt
[20140823]12c null与缺省值.txt --12c 当插入NULL时可以指定缺省值.不知道为什么设置这个特性,有点怪怪的. SCOTT@test01p> @ver BANNER                                ...
725 0
|
SQL 物联网 索引
[20121028]IOT的第2索引-NULL的问题.txt
[20121028]IOT的第2索引-NULL的问题.txt IOT表实际上时索引结构,如果第2索引的键值为NULL,会是什么情况呢? 因为第2索引包含主键,而主键是不能为NULL的,这样即使第2索引的键值为NULL,会包括在第2索引中吗? 自己做一些测试验证看看: 1.
730 0
|
SQL
[20121028]not in与NULL问题.txt
[20121028]not in与NULL问题.txt 在sql语句中使用not in,在遇到子表含有NULL的情况下,会出现没有行返回的情况,自己遇到过几次,好几次没有转过弯来。
854 0
|
2月前
|
机器学习/深度学习 SQL 关系型数据库
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
34 0
|
2月前
|
SQL 关系型数据库 MySQL
总结 vue3 的一些知识点:MySQL NULL 值处理
总结 vue3 的一些知识点:MySQL NULL 值处理
|
4月前
|
SQL 关系型数据库 MySQL
MySQL NULL 值处理
MySQL NULL 值处理