Reputation: 141
i want that when i pass query that add values in mysql database then null values should not be passes what should i do ? and where and how to use these codes ? please tell me in detail i am totally new here
Upvotes: 0
Views: 2149
Reputation: 845
Before you go to great lengths to keep nulls out of your data, make sure you really don't want them. Nulls are the best way to represent unknown values. You have to write code that expects nulls, though, and that drives a lot of people away.
It's tempting to use a default value, or an absolutely absurd value, for null. This frequently leads to problems down the road, though. There are anecdotes about people with vanity plates like "NOPLATE," and "XXXXXXXX," getting hundreds of tickets for other cars, because ticket writers would use those values, where NULL would have been the best choice. If the person who put the ticket data in the computer had a way to put NULL in the plate number field, the problem wouldn't happen. ...and hey, Devil's advocates - the string "NULL" is different than the value, NULL, so you could have a vanity plate that said, "NULL," in a perfect world. (I won't accept responsibility for training Traffic Enforcement Officers on NULL, but I would accept a contract to train the police IT folks, for the right fee. :) )
It's not uncommon to use an "absurd" value like, "1/1/1899," for an unknown date, instead of allowing nulls. There are two issues with this, however. First, users will shock you by actually needing that date some day. Second, you have to write code that looks for "1/1/1899," when writing code to handle null would be the same amount of work, and would be more semantically correct.
Use a NOT NULL constraint for the use case, "this value can never, ever be null. Whatever is described by the primary key must ALWAYS have a value for this." Avoid using a NOT NULL constraint to keep nulls out of the database for a value that could actually be missing or unknown.
NULL can be useful. It's the best way to represent unknown data.
Upvotes: 0
Reputation: 272247
To start with, constrain your columns in the database to be NOT NULL
. That will prevent invalid data being stored.
Then you need to consider a data representation in your code that doesn't involve NULLs. That may impact how you represent default or unspecified values.
You may find (as a side effect) that your code becomes simpler and less error-prone since you don't have to worry about dereferencing NULLs in your code. Consequently, eliminating NULLs from the database and your code strikes me as a good practise, generally.
Upvotes: 4
Reputation: 1272
If you want an error while on add nulls into database, you can set the NOT NULL.
also if you don't want an error , you can use a default value for that field ,so the field value would be as the default value,instead of null.
When your table is in edit mode(select the pen in PhpMyAdmin),
you can set a field's default value,
For setting not null you can un-check the NULL check box
Upvotes: 1
Reputation: 28824
The only way to add a record without NULLs is to specify values for every column.
Upvotes: 1