The difference between--null and empty strings in Django and SQL language

Source: Internet
Author: User

SQL has a unique way of specifying an empty value, which is called NULL.

NULL represents unknown data in the database, there are 3 main meanings: 1) Know the data exists, but do not know the specific value. 2) Do not know whether the data exists. 3) data does not exist.

In SQL, the null value differs from an empty string, just as None in Python is different from an empty string (""). This means that the value of a character field (such as VARCHAR) cannot contain both null and an empty string.

This can lead to unnecessary ambiguity or confusion. Why does this record have a null, and that record has an empty string? Are there differences between them, or are data input inconsistent? Also: How can I get all the records with null values, should I find them by null and empty strings? Or just search by string?

To disambiguate, the Django generation create TABLE statement automatically adds not NULLfor each field. Here is an example of the Author module generated in the fifth chapter:

 CREATE TABLE  books_author   "  ( "   ID    serial not NULL PRIMARY KEY,  Span style= "color: #800000;" > " first_name  "  varchar (30  last_name   " varchar (40  " email   varchar (75) not NULL)  

In most cases, this default behavior is best for your application because it allows you to no longer have headaches due to data consistency. And it works well with other parts of Django. In Administrative Tools, if you leave a character field blank, it inserts an empty string for this (and * is not *NULL).

However, there are exceptions to other data types: date, time, and numeric fields do not accept empty strings. If you try to insert an empty string into a date or integer field, you may get an error returned by the database, depending on the type of the database. (PostgreSQL is strictly forbidden, will throw an exception; MySQL may or may not be accepted, depending on the version you are using and your luck.) In this case,null is the only method that specifies a null value. In the Django module, you can specify that a field is allowed to be nullby adding null=true .

so that's a little tricky to say: if you want to allow a date type (datefield , timefield , datetimefield ) or digital type (integerfield , < Span class= "Pre" >decimalfield , floatfield field is empty, you need to use null=true  * and * blank=true .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.