Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Add a Not Null Constraint in PostgreSQL
A NOT NULL constraint in PostgreSQL ensures that a column cannot have NULL values, which helps maintain data integrity.
Why Use a NOT NULL Constraint?
By adding a NOT NULL constraint, you guarantee that every row in your table has a value for the specified column. This is useful when you want to ensure essential data, like user email addresses or order numbers, is always present.
Adding a NOT NULL Constraint to a New Table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
Here, the email column is set to NOT NULL when creating the table.
Adding a NOT NULL Constraint to an Existing Column
ALTER TABLE users
ALTER COLUMN email SET NOT NULL;
This command updates the existing email column to require non-NULL values.
Important Considerations
-
Make sure no existing rows have
NULLvalues; otherwise, the command will fail. -
To check for NULLs, run:
SELECT * FROM users WHERE email IS NULL; -
If needed, update NULL values before applying the constraint:
UPDATE users SET email = 'unknown@example.com' WHERE email IS NULL;
Removing a NOT NULL Constraint
ALTER TABLE users
ALTER COLUMN email DROP NOT NULL;
This command allows the column to accept NULL values again.
Summary
Adding a NOT NULL constraint is a simple but powerful way to improve your database’s reliability. By ensuring essential fields are never empty, you reduce the chance of errors in your application.
Automate PostgreSQL reporting with DataReportive
Turn your PostgreSQL queries into scheduled reports delivered to your team or customers.