NULL值得数据出现在数据库发展的最初阶段的确给开发和使用者带来了很大的便利,这是因为它为我们节省了太多的磁盘空间,而且在那个年代磁盘是相当昂贵的。但是随着科技的发展,硬件系统的改进突飞猛进,NULL又给我们带来了很大的困扰。
下面我们来看下当我们进行JOIN操作时候,NULL值给我们带来的困扰。
CREATE TABLE dbo.Student ( Sno int null ,Name nvarchar(23) ) CREATE TABLE dbo.Score ( Sno int ,Score INT ) INSERT INTO dbo.Student(Sno,Name)VALUES(1,‘Jesse‘); INSERT INTO dbo.Student(Sno,Name)VALUES(2,‘Jessca‘); INSERT INTO dbo.Student(Sno,Name)VALUES(3,‘June‘); INSERT INTO dbo.Student(Sno,Name)VALUES(4,‘Supper‘); INSERT INTO dbo.Score(Sno,Score)VALUES (1,45) INSERT INTO dbo.Score(Sno,Score)VALUES (1,56) INSERT INTO dbo.Score(Sno,Score)VALUES (2,100) INSERT INTO dbo.Score(Sno,Score)VALUES (3,25) INSERT INTO dbo.Score(Sno,Score)VALUES (5,99) SELECT * FROM dbo.Student SELECT * FROM dbo.Score
下面向学生表中插入一条id 为NULL的值。
INSERT INTO dbo.Student(Sno,Name)VALUES(NULL,‘Mike‘);
看下几种JOIN 的结果
SELECT * FROM dbo.Student SELECT * FROM dbo.Score SELECT * FROM dbo.Student a LEFT JOIN dbo.Score b ON a.Sno=b.Sno SELECT * FROM dbo.Student a INNER JOIN dbo.Score b ON a.Sno=b.Sno SELECT * FROM dbo.Student a RIGHT JOIN dbo.Score b ON a.Sno=b.Sno SELECT * FROM dbo.Student a FULL JOIN dbo.Score b ON a.Sno=b.Sno
这条NULL的值完全被作为一条新的id 值处理。
下面我们再为score插入一条NULL
INSERT INTO dbo.Score(Sno,Score)VALUES (NULL,98)
SELECT * FROM dbo.Student SELECT * FROM dbo.Score SELECT * FROM dbo.Student a LEFT JOIN dbo.Score b ON a.Sno=b.Sno SELECT * FROM dbo.Student a INNER JOIN dbo.Score b ON a.Sno=b.Sno SELECT * FROM dbo.Student a RIGHT JOIN dbo.Score b ON a.Sno=b.Sno SELECT * FROM dbo.Student a FULL JOIN dbo.Score b ON a.Sno=b.Sno
wi
此时,已经出来了好多的NULL,左表的NULL和右表的NULL都没有同时出现在一条记录里面,这意味着,NULL和NULL不是相等的。
时间: 2024-11-05 15:02:28