MySQL columns with DEFAULT NULL - stylistic choice, or is it?

后端 未结 2 1569
孤独总比滥情好
孤独总比滥情好 2020-12-18 18:37

In many flavors of SQL, there are three ways you can implicitly set a column to NULL on every row insertion. These are:

columnname type NULL
columnname type          


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 19:01

    As documented under Data Type Default Values:

    If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.

    (I think they meant implicit, not explicit).

    Moreover, as documented under CREATE TABLE Syntax:

    If neither NULL nor NOT NULL is specified, the column is treated as though NULL had been specified.

    Therefore, in MySQL the following column definitions are all identical:

    columnname type
    columnname type NULL
    columnname type DEFAULT NULL
    columnname type NULL DEFAULT NULL
    

    The choice of which to use is a balance between being explicit, and being concise. Depending on the circumstances, I might use any of the above.

提交回复
热议问题