Thread: Concept of NULL
View Single Post
Old January 11th, 2011, 08:49 PM
dspawn dspawn is offline
Registered User
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Originally Posted by rseni View Post
Hy Guys,

Beginning with mysql. I am not able to grasp the concept of NULL. Check flickr screen-shots. In it when I specifically declared 'name' field to be NOT NULL. When i run the 'desc test' table command, the table description shows default value for name field to be NULL.Why is that so?

From what I have read about NULL, it connotes a missing or information that is not applicable. So when I declare a field to be NOT NULL it implies (as per my understanding) that user must enter a value for the name field else the DB engine should generate an error i.e. record will not be entered in DB. However when i run 'insert into test value();' the DB engine enters the record in table. Check flickr screen-shot(second-one).

FLICKR LINKS *declare_not_null*

*empty_values* Check the second screenshot on flickr

Q.2 what would be sql statement to drop a primary key from a table's field. If I use 'ALTER TABLE test drop key id;' it gives the following:

ERROR: Incorrect table definition; there can be only one auto column and it must be defined as a key.

Thanks for your help.
First off, I cannot see the screen shot due to the image being private.

As for the field being set to NOT NULL and its default is NULL: A field will always have a NULL value as it's default unless predefined as something else. Now when you run your insert command I believe the SQL prompt tries to avoid an error by placing an empty string value in the field instead of the actual NULL value if it is a string field. It would be 0 if it was a number field. The only way to get the error is if you put in NULL in the insert statement like so:
insert into test value(NULL);
As for your second question. If you are trying to make an auto incrementing field a non key, you cannot. An auto increment field must be the primary key. The only way you can remove it is by either dropping the column or changing it to being non auto increment.