![]() |
VOOZH | about |
Constraints are used in relational databases to follow the standards of the database and guarantee that the data is valid. Users are usually confused about various kinds of constraints most of which include entity constraints, referential constraints and semantic constraints.
In this article, We will learn about the Difference between Entity constraints, Referential constraints and Semantic constraints and so on.
These constraints are given within one table. The entity constraints are primary key, foreign key, unique. Entity constraints are those constraints that limit the data entered in any one cell or entity of the table at a time.
These constraints mostly revolve around the issue of variance, which is a measure of the distinctive authenticity of the data.
Example:
create table student (rollnumber int primary key, name varchar2(30), course varchar2(10));
Insert into student values(111, 'ABC', 'Chemical');
Insert into student values(112, 'JJP', 'Mech');
Output:
| Rollnumber | Name | Course |
|---|---|---|
| 111 | ABC | Chemical |
| 112 | JJP | Mech |
These values are inserted in the table. Suppose a value given below is inserted :
Insert into student values(111, 'MAB', 'EEE');
It gives an error as the roll-number is enforced a primary key constraint that refrains from duplication. These constraints ensures to maintain uniqueness in the tables to avoid duplication's.
These constraints are used for referring other tables to enforce conditions on the data. The widely used referential constraint is foreign key. Referential integrity guarantees referential integrity, that is, the referential link between tables is consistent.
They keep the relationship between tables intact as keys in one table refer to keys in other tables at the same time.
create table marks (rollnumber int, name varchar2(30), course varchar2(30)
references student, marks int);
A table is created with a constraint that the marks should be rewarded to those students that are pursuing courses stated in the student table only. If a user tries to enter a value that doesn't exist, it returns an error.
Datatypes are the semantic constraints enforced in a table. Datatypes help the data segregate according to its type. Semantic constraints are rules that capture the meaning or the context of the data. Another type of business rules they implement are not structure related rather they are logical correctness of data.
A name is a combination of different letters. We can place the name column in the char datatype yet char doesn't satisfy the condition thereby varchar is preferably used for name.
name varchar2(30);| Characteristics | Entity constraints | Referential constraints | Semantic constraints |
|---|---|---|---|
| Definition | Entity constraints are posed within a table. | Referential constraints are enforced with more than one table. | Semantic constraints are enforced on the values of a specific attribute . |
| Kinds | The entity constraints are: unique, primary key, NULL. | The referential constraints are foreign key. | The semantic constraints are the datatypes. |
| Description | These constraints are used to enforce uniqueness in a table( while NULL is used to define no value) | These constraints are used for referring to another table for analysis of the data. | These constraints are used to divide a set of particular value based on a category. |
| Functions | These constraints ensure non duplicate's in a database. | These constraints ensure that consistency of a database. | These constraints ensure that values are categorized accordingly to avoid confusions. |
| Syntax | Primary key: create table( column1 datatype1 primary key...) | Foreign key: create table( column1 datatype1 references tablename...) | column1 varchar2(30) |
| Examples | No two students can be designated the same rollnumber. | Rollnumber being referred to the marks table. | Name is assigned to varchar2 with a precision of 50. |
Entity, referential and semantic integrity constraints are very important in maintaining integrity, validity and consistency of data within the context of a relational database. Whereas entity constraints are placed on the individual data items, referential constraints are applied to the relation between the table and semantic constraints are applied based on the meaning of the data. Knowledge of and adherence to such bounds contribute to the enhancement of the reliability of a database.