Hi everyone, this is V. In database design, is the use of theNOT NULL
is a very important decision that directly affects data integrity, query performance, and the complexity of the business logic. The use ofNOT NULL
The key to this is understanding the business requirements and specific scenarios.
Here are some scenarios by which V analyzes when it should be usedNOT NULL
When will it be allowed?NULL
. Together, we will talk about our experiences and look forward to discussing them with our brothers.
1. Fields for which a value must exist
For some key fields, if the business logic requires that they always have values, then you should use theNOT NULL
Constraints. This prevents incomplete data and potential business problems.
Example: User Registration Scenario
-
field:
username
、email
-
analyze: Username and email are required when a user registers. Missing these two fields will prevent subsequent logins or notifications from taking place, so these fields should be set to
NOT NULL
。
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
Impact:
- Data Integrity: Ensure that any user has a username and email address.
- Performance Optimization:
NOT NULL
fields can optimize database indexing and query efficiency.
2. Optional fields
Some fields are optional and their absence does not affect the business logic. In this case, allowing theNULL
Flexibility can be provided to the business to avoid forcing users to provide all information.
Example: User Information Scenario
-
field:
middle_name
、profile_picture
-
analyze: The middle name and profile picture are usually optional information. If it is mandatory to use
NOT NULL
Instead, default values need to be provided, which doesn't make much sense in some cases. For example, if the user does not provide a profile picture, the field could be set toNULL
。
CREATE TABLE user_profiles (
id INT PRIMARY KEY,
middle_name VARCHAR(255),
profile_picture VARCHAR(255)
);
Impact:
- Flexibility: Allowing fields to be empty provides more freedom for the user while maintaining the flexibility of the data structure.
- Business Adaptability: as business needs change, allow for
NULL
fields can accommodate more edge cases.
3. Fields needed to identify unknown status
In some business scenarios, theNULL
It is possible to indicate an "unknown" or "not provided" state, rather than just a "null value". In this case, theNULL
is reasonable because it makes a clear distinction between "no value" and "empty value".
Example: Order Processing Scenario
-
field:
shipped_date
-
analyze: The ship date of an order may be unknown at the time of creation and will only be filled in after the order has been shipped. If using the
NULL
Indicating an unshipped status simplifies business logic and is more intuitive to business needs.
CREATE TABLE orders (
id INT PRIMARY KEY,
order_date DATE NOT NULL,
shipped_date DATE
);
Impact:
- State Expression:
NULL
It is possible to clearly indicate that a certain status has not occurred, e.g. an order has not yet been shipped. - Simplified business logic: no need to add extra boolean fields to indicate whether to ship or not.
4. Foreign key fields andNOT NULL
Whether or not the design of the foreign key uses theNOT NULL
Dependent on business logic. MandatoryNOT NULL
Implies that the affiliation is mandatory; allowsNULL
If this is the case, it means that some records may not have associated items for the time being.
Example: blog post and author
-
field:
author_id
-
analyze: If the business logic allows certain articles to have no specific author (e.g., automatically generated by the system), then the
author_id
fields can allowNULL
.. If each article must have an author, theNOT NULL
Makes more sense.
CREATE TABLE posts (
id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
author_id INT REFERENCES users(id) -- can be NULL
);
Impact:
- Forced associative relationships: use the
NOT NULL
Data integrity and consistency can be ensured. - Flexible associations: allow
NULL
The foreign key fields provide more flexibility to the business and support special scenarios.
5. Performance and storage overhead
From a performance perspective, theNOT NULL
fields can speed up queries in some cases. This is because the database can more efficiently handle queries that do not allowNULL
field, there is no need to make any changes to theNULL
values to make additional judgments. However, if too many fields are set toNOT NULL
In addition, it may lead to an increase in operational complexity.
Example: High Frequency Query Fields
-
field:
last_login
-
analyze: For a user's login system, querying the last login time is a common operation. If this field is set to
NOT NULL
The database allows for faster retrieval of data.
CREATE TABLE user_sessions (
id INT PRIMARY KEY,
user_id INT NOT NULL,
last_login TIMESTAMP NOT NULL
);
Whether or not to useNOT NULL
The decision should be based on business needs. Forkeyword(e.g., user name, order ID, etc.).NOT NULL
Data integrity can be guaranteed. And for theOptional Fieldsor fields that indicate status (e.g., ship date, optional information, etc.), allow theNULL
may provide greater flexibility.
6. Versioning and evolutionary database design
Over the course of a long-term project, the database architecture evolves as business needs change. At times, it is permissible toNULL
Flexibility can be provided for unforeseen future expansion.
Example: Product upgrade and new feature scenarios
-
field:
discount_rate
-
analyze: It is assumed that in the initial design, all products are not discounted and therefore
discount_rate
Fields are not required at the time of database design. However, as the business expands, certain products begin to introduce discount mechanisms. In this case, the initial product may not have a discount value, so it is possible to have thediscount_rate
allowed toNULL
, indicating that the discount has not been used.
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
discount_rate DECIMAL(5, 2) -- allowed to NULL,Indicates no discount
);
Impact:
- Evolutionary flexibility: allowing
NULL
Provides more room for future expansion and avoids being overly restrictive at the beginning of the design. - Code Maintenance: The development team can add new features incrementally based on business needs without having to redesign the table structure at each iteration.
7. Design based on statistics and analysis of data
In some statistical data scenarios, it is permissible for fields to beNULL
A clearer view of the data can be provided, especially for handling missing data.NULL
Explicitly indicates that the data does not exist, rather than a meaningless default value.
Example: User Activity Tracking
-
field:
last_purchase_date
-
analyze: In user behavior analysis, tracking the date of a user's last purchase is a common requirement. If a user has never made a purchase, the
last_purchase_date
can provideNULL
. UseNULL
Rather than using spurious defaults (e.g. '1970-01-01') can more accurately reflect the state of the business.
CREATE TABLE user_activity (
user_id INT PRIMARY KEY,
last_login DATE NOT NULL,
last_purchase_date DATE -- can be NULL, means no purchase record
).
Impact:
- Data accuracy:
NULL
Expresses the "not occurred" status better than the default value, avoiding the use of incorrect data for analysis. - Analyzing efficiency: by
IS NULL
Judgment can quickly filter out users who have not performed certain behaviors.
8. Migration and data compatibility
When performing database migrations or data integration between different systems, it is permitted toNULL
Often improves compatibility, especially if the earlier design and the target system do not match. If the source system's data allows certain fields to be empty and the target system does notNULL
, then the migration process may encounter problems.
Example: Cross-System Data Migration
-
field:
address_line_2
-
analyze: Suppose a system divides user addresses into
address_line_1
cap (a poem)address_line_2
whichaddress_line_2
is optional. Whereas in the target system, theaddress_line_2
utilizationNOT NULL
can lead to partial data migration failures. Therefore, in this case, the design should allow theNULL
The data compatibility is ensured.
CREATE TABLE user_addresses (
user_id INT PRIMARY KEY, address_line_1 VARCHAR(255) NOT NULL, user_addresses
address_line_1 VARCHAR(255) NOT NULL, address_line_2 VARCHAR(255) -- NULL allowed because not all users need to be filled in.
address_line_2 VARCHAR(255) -- NULL is allowed because not all users need to be filled in.
);
Impact:
- Migration success rate: allowed
NULL
It can improve data migration compatibility and ensure that data from different systems can be seamlessly integrated. - Data mapping: Improve data accuracy by avoiding the forced use of default values or spurious data to fill in gaps.
9. UtilizationNOT NULL
and the combination of default values
In some cases, the use ofNOT NULL
amalgamatedefault valueIt is possible to improve the robustness of a field and prevent developers from omitting certain information when inserting data. For example, for Boolean or enumerated fields, it's common to pass theNOT NULL
and default values to ensure logical integrity.
Example: Order Status Management
-
field:
order_status
-
analyze: Order status is an essential field in an order management system. To ensure that each order has an explicit status when it is created, use the
NOT NULL
and setting a default value (e.g. "Pending") can avoid omissions.
CREATE TABLE orders (
id INT PRIMARY KEY,
order_date DATE NOT NULL,
order_status VARCHAR(50) NOT NULL DEFAULT 'Pending'
);
Impact:
- Business robustness: through
NOT NULL
Combined with default values, it ensures that every process in the business has an initial state and avoids logical errors. - Easy to maintain: default values reduce complexity when inserting data and ensure system consistency.
10. Dynamic data structures andJSON
typology
In modern database design, the use ofJSON
It is increasingly common for types to store irregular or dynamic data. For such scenarios, is it better to use theNOT NULL
decisions are different from traditional relational field designs. In most cases, theJSON
Fields are flexible and can be empty to accommodate diverse data formats.
Example: User Preferences
-
field:
preferences
-
analyze: Suppose we need to store user preferences, but the type and number of preferences vary from user to user. Use the
JSON
type allows flexibility in storing this information. AllowsNULL
User data for which preferences are not provided can be processed.
CREATE TABLE user_settings (
user_id INT PRIMARY KEY,
preferences JSON -- NULL is allowed to indicate that the user has not set preferences.
).
Impact:
- Flexibility: use
NULL
cap (a poem)JSON
The combination of types can handle dynamic and irregular data structures and adapt to the needs of different users. - Scalability: In subsequent changes in requirements, it is not necessary to modify the table structure, but to update the table structure directly by updating the
JSON
Data will suffice.
To summarize: how to balanceNOT NULL
together withNULL
In deciding whether to useNOT NULL
When doing so, the following points should be considered:
-
business need: Fields that require mandatory values for critical business logic should use the
NOT NULL
The options and edges of the situation allowNULL
。 -
Data accuracy:
NULL
The ability to express "unknown" or "did not occur" states better than using meaningless default values. -
Performance and Maintainability:
NOT NULL
It is possible to optimize query performance and reduce the database indexing burden, but it is not possible to do so while allowing theNULL
The flexibility and compatibility will be higher in the case of the -
Future expansion: Early in the design process, consideration should be given to the future evolution of the business by prematurely limiting fields to
NOT NULL
May present challenges when scaling. -
utilization
NOT NULL
Scenarios: business-critical fields, high data consistency requirements, and frequently queried fields. -
permissible
NULL
scenarios: optional fields, representation of unknown states, flexible association relationships.
So what, use it wisely according to the business scenarioNOT NULL
The database can be designed to provide the necessary flexibility and performance optimization while maintaining data integrity. When designing a database, we can find a balance between flexibility, performance and data integrity.