复制代码 代码如下:
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
复制代码 代码如下:
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
以下代码则创建子表,并在引用父表的列中插入一个空值。
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
新闻热点
疑难解答