0

What's the difference or benefit of writing "NOT NULL" in a mysql field creation...

For example if I'm creating a table like this...

CREATE TABLE IF NOT EXISTS game(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
description VARCHAR(200) NOT NULL,
PRIMARY KEY(id)
)

Here, id and name always has a value so their NOT NULL is just fine. I get that. But, description is an optional field so, it can be blank.

So, in this situation, should I put NOT NULL or not ?

3
  • Think about a situation when you have a field called status and it could be either 0 1 and your app rely on 0,1 and no null values to see the status of certain things. Commented May 12, 2015 at 8:31
  • It's up to you. To my way of thinking, if a description is unavailable (but could conceivably be provided) then it should be NULL. Commented May 12, 2015 at 8:37
  • putting NOT NULL means the field should not be null. If it is nullable then leave it as is and don't put NOT NULL or else it would prompt error once the field is null. Commented May 12, 2015 at 8:41

3 Answers 3

4

NULL and a blank field are not the same thing (unless, under some circumstances, you're a brain-dead DBMS coughOraclecough).

NULL means unknown or not applicable whereas a blank field means, well, known and blank.

It depends entirely on how you want to handle the field itself. For example, let's consider the middle initial of your name. If you do not wish to distinguish between 'unknown' and 'does not have one', set the column as NOT NULL and just use a blank value to represent both - that will ease your queries somewhat.

However, if you want to mail all your clients without middle names (for whatever bizarre reason) without bothering those where you don't know if they have one, you need to be able to distinguish between the two classes.

Sign up to request clarification or add additional context in comments.

9 Comments

What if you want to email all people using their full names, including middle names if they have them?
@Strawberry: assuming null allowed, something like select first || ' ' || last as full from table where middle is null or middle = '' union all select first || ' ' || middle || ' ' || last as full from table where middle <> ''. Slightly easier if column is not nullable.
That's easier than CONCAT_WS() ?? I disagree
@Strawberry, that's why I said "something like" :-) In any case, concat_ws will still need the union unless you want multiple consecutive separators for those without a middle name, yes?
Consider the alternative: create table x (a varchar(10), b varchar(10), c varchar(10)); insert into x values ('pax', NULL, 'diablo'); insert into x values ('george', 'w', 'bush'); select concat_ws('@',a,b,c) from x; There, now isn't that better.
|
1

First of all, in your situation, you really don't have to put NOT NULL, if you put NOT NULL, when you insert data to your database , if the field used NOT NULL, its mandatory to put values to this field, however ,as mentioned earlier by paxdiablo, that NULL doesn't mean blank field, since blank field can be of a lot of spaces or one space. Anyways, if it is not mandatory to have values in a certain Field, you really don't have to put NOT NULL.( I really don't think it has anything to do with braindead DBMS or not @paxdiablo)

1 Comment

My comment re brain-deadedness of any particular DBMS had to do with the fact that it can't distinguish between '' and NULL for some data types. It was not central to the discussion, I just can't pass up the opportunity for some Larry-bashing :-)
0

what my opinion is, NOT NULL is for those field that with dependency such as primary key, foreign key.

what i working previously, in a function(a,b,c,d,e); without enter foreign key value, it execute but return no result and the auto_increment value is increased. this is not a good practice. if with NOT NULL, the insertion will be stopped when foreign key value is NULL, the auto_increment value wont be increase.

Addition

to handle parent record deletion, there is a cascade action or you can make it manually update another value to the record.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.