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
|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
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises