Constraints in the hidden data including at NULL

NULL in the database has been often been out cautionary, also read some books or listen to some lectures will be referred Try not to let the field can allow NULL.


Often used example is NULL can not compare (neither 0 nor 1, but unknow), it is often because of the example Where conditional expressions containing NULL data caused by the return of the results are wrong trend.
For example the following example:

Use tempdb
GO
--建一数据表
Create Table tb1(id int identity Primary key,name varchar(10),sex char(1))
GO
--写入数据
Insert Into tb1 values('Rock','M'),('Cary','F'),('John',NULL)
GO
--筛选出性别不是男生的数据
Select * From tb1 Where sex <> 'M'
GO

From the results of view (shown below), male sex is not only the sum of the data, and the sex field is not NULL, John were screened.

Or if there is to NOT IN Subquery NULL's will is the same tragedy

Use tempdb
GO
--建一数据表
Create Table tbtype(id int identity Primary key,typecode char(1))
Create Table tbObject(id int identity Primary key,ObjName varchar(50),typecode char(1))
GO
--写入数据
Insert Into tbtype values('A'),('B'),(NULL)
Insert Into tbObject values('Apple','A'),('Asus','B'),('HTC','B'),('MI','C')
GO
--筛选出typecode不在tbtype数据表中的数据
Select * From tbObject Where typecode Not in(Select typecode From tbtype)
GO

As shown below in red circuit icon, we have found tbObject typecode sum of data = 'C', but in our tests the pen data has not been out of the screen, but the return data back to zero pen. That's when potential problems occur in the presence of NOT IN Subquery NULL.

However, these issues are often brought everyone to review this reason should not have NULL, ROCK recently read an e-book (Defensive Database Programming with SQL Server) found for NULL Constraints (Constraint) influence is not small snort. The following is a simple Demo Code established environment.

Use Tempdb
GO
CREATE TABLE Boxes(id int identity PRIMARY KEY , Leng int ,Width int ,Height int) ;
GO
--第一个Check验证 高<10 
ALTER TABLE Boxes ADD CONSTRAINT Boxes_Check1 CHECK(Height<10) ;
GO
--第二个Check验证 长>宽>高
ALTER TABLE dbo.Boxes
ADD CONSTRAINT Boxes_Check2 CHECK(Height <= Width AND Width <= Leng) ;
GO

After the completion of the build environment, we first test whether our Check Constraints can operate normally. We deliberately lower the figure the data is equal to the height of 10, so that would be contrary to our Boxes_Check1 conditions error.

Next we deliberately greater than the length and width of the write data, as shown because of constraints violation Boxes_Check2 error condition.

More than two simple tests can know the current Check Constraints are functioning properly.

Next, I will write NULL data containing data sheets look at our Check Constrains is not also resist. FIG. Insert the following syntax, a length of I to 8, then the width is NULL height is 9, the height greater than the length Boxes_Check2 clear violation of constraint conditions. However, the pen was able to successfully write data and Table Check Constraints to not be stopped.

Height field and then enter the data in the figure below, I deliberately NULL value, we can also find the results from the sum of the data also avoid the constraints and conditions Boxes_Check1 successfully written to the data table.

In addition to just the Demo will encounter NULL Constraints on useless, but even ensure data integrity Foreign Key also because of the associated field containing the NULL value and broken power, the following is a simple Demo.

Use Tempdb
GO
Create Table tbFk1(id1 int not null,id2 int not null,name varchar(10),primary key(id1,id2));
GO
Create Table tbFk2(id int identity Primary Key,id1 int,id2 int,Phone varchar(10));
GO
--为tbFk1及tbFk2建立Foreign Key的关联
Alter Table tbFk2 Add Constraint FK_tbFk1_tbFk2 Foreign Key(id1,id2)
References tbFK1(id1,id2);
GO

After completion of the above build environment, we have to test whether the Foreign Key will effect normal development. As shown below we write single data ID1 = 1 and id2 = 1 in the parent data table (tbfk1), followed by smoothly we can write single ID1 = 1 and id2 in the sub-data table (tbfk2) in = data 1.

We mean to take this sum Insert parent data table does not exist in the data table to the child, that is id1 = 1 and id2 = 2 data. This diagram can be learned by the sum of the data written to the occurrence of a failure, because in violation of the Foreign Key rules.

However, we will break NULL Foreign Key rules, as shown, whether or id1 = null id2 = null, as long as contained in the data field associated with a NULL value when containing our Foreign Key does not check the data written directly from (red circle at bottom).

After a simple test that can be found NULL Ray really find many, please be sure to pay attention to when developing. However, the above problems are in fact NOT NULL as long as we set in the relevant field can be avoided Check out Constraints no problems occurred.

I am ROCK

[email protected]

Original: Big Box  Constraints in data contain hidden when NULL


Guess you like

Origin www.cnblogs.com/petewell/p/11445564.html