国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > SQL Server > 正文

SQL Server的空值處理策略

2024-08-31 00:48:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
數(shù)據(jù)完整性是任何數(shù)據(jù)庫(kù)系統(tǒng)要保證的重點(diǎn)。不管系統(tǒng)計(jì)劃得有多好,空數(shù)據(jù)值的問(wèn)題總是存在。本文探討了在sql server中處理這些值時(shí)涉及的3個(gè)問(wèn)題:計(jì)數(shù)、使用空表值以及外鍵處理。
用count(*)處理空值

大多數(shù)集合函數(shù)都能在計(jì)算時(shí)消除空值;count函數(shù)則屬于例外。對(duì)包含空值的一個(gè)列使用count函數(shù),空值會(huì)從計(jì)算中消除。但假如count函數(shù)使用一個(gè)星號(hào),它就計(jì)算所有行,而不管是否存在空值。

如果希望count函數(shù)對(duì)給定列的所有行(包括空值)進(jìn)行計(jì)數(shù),請(qǐng)使用isnull函數(shù)。isnull函數(shù)會(huì)將空值替換成有效的值。

事實(shí)上,對(duì)集合函數(shù)來(lái)說(shuō),如果空值可能導(dǎo)致錯(cuò)誤結(jié)果,isnull函數(shù)就非常有用。記住在使用一個(gè)星號(hào)時(shí),count函數(shù)會(huì)對(duì)所有行進(jìn)行計(jì)算。下例演示了空值在avg和count集合函數(shù)中的影響:

    set nocount on
go
create table xcount
(pkey1 int identity not null
    constraint pk_xcount primary key,
col1 int null)
go
insert xcount (col1) values (10)
go
insert xcount (col1) values (15)
go
insert xcount (col1) values (20)
go
insert xcount (col1) values (null)
go
select avg(col1) avgwithoutisnullfunctiononcol1,
avg(isnull(col1,0)) avgwithisnullfunctiononcol1,
count(col1) noisnullfunctiononcol1 ,
count(isnull(col1,0)) usingisnullfunctiononcol1,
count(*) usingasterisk
from xcount
go
drop table xcount
go

output:
avgwoisnullfnctncol1 avgwisnullfnctncol1 woisnullfnctncol1

wisnullfnctncol1 usingasterisk
---------------- ------------- -------------- ------------ ---------
15             11                3           4         4

恰當(dāng)使用空表值
 

sql server可能出現(xiàn)一種特殊情況:在引用父表的一個(gè)表中,因?yàn)椴辉试S空值,所以“聲明引用完整性”(dri)可能不會(huì)得到強(qiáng)制。即使父表不包含空值,在子表引用了父表主鍵約束或惟一約束的列中,也可能包含空值。


假如來(lái)自父表的值目前未知,就不會(huì)有任何問(wèn)題。例如,父表可能是一個(gè)地址表,而子表可能包含聯(lián)系信息。由于許多原因,可能暫時(shí)不知道要傳給父表的聯(lián)系地址。這是一種基于時(shí)間的問(wèn)題,空值在其中或許是合適的。

如下例所示,我們創(chuàng)建父表,并在其中插入兩個(gè)值。

 set nocount on
    gocreate table parent(pkey1 int identity not null    constraint pkparent primary key,col1 int null)goinsert parent (col1) values (284)goinsert parent (col1) values (326)go


以下代碼則創(chuàng)建子表,并在引用父表的列中插入一個(gè)空值。

 create table child
    (pkey1 int identityconstraint pkchild primary key,parentpkey1 int nullconstraint fkchildparent foreign keyreferences parent(pkey1),col1 int null)goinsert child (parentpkey1, col1) values (null,2)go
 
但在以下代碼中,要同時(shí)從父表和子表選擇值。雖然父表不包含空值,但在子表引用了父表的那個(gè)列中,將允許一個(gè)空值。


然后丟棄所有表,清除這個(gè)演示所用的數(shù)據(jù)庫(kù)對(duì)象。


 select * from childgoselect * from parentgodrop table child, parentgo

在可以為空的外鍵中檢查數(shù)據(jù)的有效性
 

如果由兩個(gè)列共同組成主鍵,而且一個(gè)子表將主鍵作為可為空值的外鍵來(lái)繼承,就可能得到錯(cuò)誤的數(shù)據(jù)。可在一個(gè)外鍵列中插入有效的值,但在另一個(gè)外鍵列中插入空值。然后,可添加一個(gè)數(shù)據(jù)表檢查約束,在可為空的外鍵中檢查數(shù)據(jù)的有效性。

任何多列外鍵都可能遇到同樣的問(wèn)題。所以,你需要添加一個(gè)檢查約束來(lái)檢測(cè)異常。最初,檢查約束將檢查構(gòu)成外鍵的所有列中可能為空的值。檢查約束還要檢查這些列中不能為空的值。如兩個(gè)檢查都通過(guò),問(wèn)題就解決了。

以下示范腳本展示了這樣的一個(gè)異常,以及如何用檢查約束來(lái)糾正它。


     set nocount ongocreate table parent (pkey1 int identity not null, pkey2 int
not null, col1 int null,constraint pk_parent primary key nonclustered ( pkey1, pkey2))goinsert parent (pkey2) values ( 2 )insert parent (pkey2) values ( 85 )insert parent (pkey2) values ( 41 )insert parent (pkey2) values ( 11 )goselect * from parentgocreate table child (cpkey1 int identity not nullconstraint pk_child primary key nonclustered (cpkey1),pkey1 int null, pkey2 int null, col1 int null,constraint fk_parent_child foreign key (pkey1, pkey2)references parent (pkey1, pkey2))goinsert child (pkey1, pkey2) values ( null, 85 )goselect * from childgodelete childgoalter table child with nocheckadd constraint ck_fk_parent_child check((pkey1 is not null and pkey2 is not null) or(pkey1 is null and pkey2 is null) )goinsert child (pkey1, pkey2) values ( null, 11 )godrop table child, parentgo


空值是所有數(shù)據(jù)庫(kù)開(kāi)發(fā)者和管理員都要遇到的。所以,要想開(kāi)發(fā)成功的應(yīng)用程序,必須知道如何處理這些值。本文和你分享了空值處理的一些技巧和技術(shù)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 精河县| 鄂尔多斯市| 靖宇县| 来宾市| 丰台区| 辛集市| 额尔古纳市| 高邑县| 雷山县| 象山县| 上思县| 呼图壁县| 金昌市| 微博| 长寿区| 佛山市| 罗源县| 彰化县| 韩城市| 滨州市| 抚顺市| 昆明市| 得荣县| 岚皋县| 新干县| 锦屏县| 长岭县| 巴彦淖尔市| 云龙县| 宁国市| 亳州市| 五原县| 漳浦县| 女性| 革吉县| 台前县| 正宁县| 周口市| 乌兰浩特市| 合山市| 巴东县|