Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using Null to represent a Value Bad Practice?

If I use null as a representation of everything in a database table is that bad practice ?

i.e.

I have the tables: myTable(ID) and myRelatedTable(ID,myTableID)

myRelatedTable.myTableID is a FK of myTable.ID

What I want to accomplish is: if myRelatedTable.myTableID is null then my business logic will interpret that as being linked to all myTable rows.

The reason I want to do this is because I have an uknown amount of rows that could be inserted into myTable after the myRelatedTable row is created and some rows in the myRelatedTable need to reference all existing rows in myTable.

like image 938
Element Avatar asked Dec 07 '22 08:12

Element


2 Answers

I think you might agree that it would be bad to use the number 3 to represent a value other an 3.

By the same reasoning it is therefore a bad idea to use NULL to represent anything other than the absence of a value.

If you disagree and twist NULL to some other purpose, the maintenance programmers that come after you will not be grateful.

like image 77
Ed Guiness Avatar answered Feb 13 '23 00:02

Ed Guiness


Not a good idea, because then you cannot use the "related to all entries" fact in SQL queries at all. At some point, you'll probably want/need to do this.

like image 44
Michael Borgwardt Avatar answered Feb 12 '23 23:02

Michael Borgwardt