

Learn about the NOT NULL constraint in the next chapter.

Sure, you could perform this validation in your application layer. A Foreign key is a column or group of columns in one table that references one or more columns from another table.Ĭheck constraint is a constraint that allows specifying the conditions that the values of columns must meet. Not null constraints are a great way to add another layer of validation to your data. The foreign key defines a relationship between the two tables and enforces referential integrity in PostgreSQL. It is a combination of NOT NULL constraint and UNIQUE constraint. The drawback is that you cannot give explicit names to not-null constraints created this way. A not-null constraint is functionally equivalent to creating a check constraint CHECK (columnname IS NOT NULL), but in PostgreSQL creating an explicit not-null constraint is more efficient. Now if you want to change an existing constraint, you will drop the constraint first and then follow it up with the add constraint statement. Primary key is a column or group of columns that uniquely identify a row in a table. A not-null constraint is always written as a column constraint. You have a table already created with lots of data in PostgreSQL, now you want to add a constraint to that table. Unique constraint enforces unique values for a column or group of columns across the whole table. The NOT NULL constraint restricts the NULL value in the column. A column can have multiple constraints.Ĭonstraints can be specified as part of the table definition. In PostgreSQL, constraints can be defined in two ways:Ĭonstraints can be defined as part of the definition of an individual column. If a user attempts to store data in a column that would violate a constraint, an error will be raised. Constraint enforces a restriction on data to be stored in a column.
