PostgreSQL ConstraintsThe constraints are used to describe the rules for the data columns in a table. If there is any destruction between the constraints and the data action, the action is terminated immediately. The constraints make sure the dependability and the correctness of the data in the database. In this section, we are going to discuss all the PostgreSQL Constraints. The constraints can be further divided as column level or table level where the table level constraints are used for the whole table, and the Column level constraints are used only for one column. Where we use the constraints?The constraints are most commonly used in below areas:
Type of PostgreSQL ConstraintsLet us see the most commonly used constraints in PostgreSQL:
NOT NULL ConstraintIn not-null constraint, a column can hold the Null values by default. If we don't want a column to have a NULL value, then we need to explain such constraint on this column state that NULL is now not acceptable for that particular column. It is always created as a column constraint, and it represents unknown data but it doesn't mean that the data would be null. For example In the below case, we are creating a new table called Customer, which has the five columns, such as Cust_Id, Cust_ Name, Cust_Address, Cust_Age, and Cust_Salary. SQL Query in PgAdmin4In the below screenshot, we can see the above Query in pgAdmin4: ExplanationThe above example shows the table Customer has been created in which Cust_Id, Cust_Name, and Cust_Age columns are specified not to accept the null values. The table StructureAfter executing the select command, we can see the table structure of the Customer table. CHECK ConstraintIn PostgreSQL, the Check constraint can be defined by a separate name. It is used to control the value of columns being inserted. It allows us to verify a condition that the value being stored into a record. If the statement is false, then the data disrupts the constraint and is not saved into the table. For example In the below example, we are creating a new table called Customer2, and this table contains five columns. SQL Query in PgAdmin4ExplanationIn the above example, we add a CHECK with the Cust_SALARY column in table Customer2, where the Cust_Salary column can not contain any value less than or equal to zero (0). The table StructureAfter executing the select command, we can see the table structure of the Customer2 table. Unique ConstraintThe unique constraint is used to maintain the individuality of the values that we store into a field or a column of the table. It is compatible with a group of column constraints or column constraints and a table constraint. When we are using the unique constraint, an index on one or more columns generate automatically. If we add two different null values into a column in different rows, but it does not interrupt the specification of the UNIQUE constraint. For example In the below example, we are creating a new table called Customer3, which has similar five columns as we created in the above tables. SQL Query in PgAdmin4ExplanationThe above example, the Cust_Age column, is set to UNIQUE; therefore, we can avoid the duplicity of two or more persons from having the same age. The table StructureAfter executing the select command, we can see the table structure of the Customer3 table. Primary key ConstraintIt is a field in a table that individually identifies each row or the record in a database table, and it contains a unique value. A primary key does not hold any null value. And for this, we can also say that the primary key is a collection of the unique and not-null constraint of a table. It is used to identify each record in a database table distinctively. Here we can contain further unique columns, but we have only one primary key in a database table, and that includes single or multiple fields. It is the most crucial key while creating the database tables, and it could be a unique ID. It can be signified for one column or group of columns. The working of the primary key is similar to a unique constraint. Still, the significant difference between them is one table can have only one primary key; however, the table can have one or more unique and not-null constraints. Example In the below example, we are creating a new table called Employee, which contains the four columns, such as Emp_Id, Emp_ Name, Emp_Address, and Emp_Age. SQL Query in PgAdmin4ExplanationThe above example, the Employee table, has been created with one primary key as Emp_Id column, which represents the employee id uniquely. The table StructureAfter executing the Select command, we can see the table structure of the Employee table. Foreign key ConstraintIt is a group of columns with values that depend on the primary key benefits from another table. It is used to have the value in a column or group of columns that must be displayed in the same column or combination of columns in another table. In PostgreSQL, the foreign key's values as parallel to actual values of the primary key in the other table; that's why it is also known as Referential integrity Constraint. For example In the below example, we are creating a new table called Employee1, which contains the four columns that are similar to the previous table. In this particular example, we will create one more table called cust, which contains three columns. And, here we create a foreign key as Cust_Id column, which references to the Emp_ID field for the Employee1 table. SQL Query in PgAdmin4The table StructureHere, we can see the table structure of the Cust table, which is a reference to the Employee1 table. EXCLUSION ConstraintIt is used to make sure that any two rows are linked on the specified columns or statements using the defined operators. In any case, one of these operator evaluations will return null or false. For Example In the below example, we are creating a new table called Employee, which contains the five columns. And here, we will use an exclude constraint as well. Now, for exclusion constraints, �we will use the using gist, which is the index, and used for creating and implementation. Note: If we are using the exclusion constraints, we have to run the create extensionbtree_gist command, for one time in a database. And after that, it will connect the btree_gist extension that defines the constraints on basic scalar data types. Now, we will insert some records in the Employee3 table, and we also imposed a similar age. The first two insert commands will be executed successfully. And the records are added into the Employee3 table as we can see in the below screenshot: But for the third insert command, we may encounter the following error: SQL Query in PgAdmin4Dropping ConstraintsIf we want to delete a constraint, then we should remember the name of the constraints as it is easier for us to drop the constraints directly by its name. Otherwise, we will require to identify the system-generated name. In the psql, the following command can be used to find out the names. The syntax for dropping constraints is as follows: Next TopicPostgreSQL Primary key |