SQL: Using NULL values vs. default values

前端 未结 13 2222
南旧
南旧 2020-12-01 01:28

What are the pros and cons of using NULL values in SQL as opposed to default values?

13条回答
  •  失恋的感觉
    2020-12-01 01:53

    I don't know why you're even trying to compare these to cases. null means that some column is empty/has no value, while default value gives a column some value when we don't set it directly in query.

    Maybe some example will be better explanation. Let's say we've member table. Each member has an ID and username. Optional he might has an e-mail address (but he doesn't have to). Also each member has a postCount column (which is increased every time user write a post). So e-mail column can have a null value (because e-mail is optional), while postCount column is NOT NULL but has default value 0 (because when we create a new member he doesn't have any posts).

提交回复
热议问题