<
w3resource

PostgreSQL CONSTRAINTS

CONSTRAINTS

Constraints are a part of the table definition that limits the values inserted into its columns.

Which type of data a table can store that may be decided by the type of data. That is why CONSTRAINTS needed. Suppose the age of a student is always be a positive value but there is no such data type which can accept the only positive value, a CONSTRAINT can do this.

Usage of CONSTRAINTS

  • SQL allows defining constraints on columns and tables.
  • SQL rejects any value that violates the criteria that were defined.
  • The column constraints apply only to individual columns.
  • The table constraints apply to groups of one or more columns.
  • The constraints are declared at the time of creating a table with the CREATE TABLE command.
  • Constraints can be added to a table after its creation and also temporarily disabled.
  • All detail of constraints is stored in data dictionary.
  • Each constraint is assigned a name.
  • It is easier if we give use defined names so that it is easily referenced otherwise the name is automatically generated.

Name and Description of CONSTRAINTS

Name Description
NOT NULL The not-null constraint in PostgreSQL ensure that a column can not contain any null value. This is a column constraint. No name can be defined to create a not-null constraint.
UNIQUE The unique constraint in PostgreSQL ensure that the value entered into a column or a field of a table is unique.
CHECK The check constraint in PostgreSQL is used to specify that the value in a specific column or field of a table must match a boolean expression. This constraint can be defined as a separate name.
PRIMARY KEY This the requirement of an efficient database to ensure that there are no duplicate records within a single table. A field whose value uniquely identifies a record in a table is called a primary key.
FOREIGN KEY The unique constraint in PostgreSQL specifies that the valued in a field or a column of a table must match to the actual value of the primary key of another table.

Previous: Delete Data
Next: ALTER TABLE



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/constraint.php