What are the Constraints of DBMS?
Constraints in DBMS relate to restrictions put on data or data processing. This means that only a specific kind of data may be stored in the database or that only a specific kind of action can be carried out on the data there.
Thus, constraints in a database management system (DBMS) ensure data correctness.
The following can be guaranteed via constraints
Data Accuracy −Constraints, which ensure that only real data is entered into a database, guarantee data correctness. A restriction, for instance, may prevent someone from entering a negative integer into a field that only allows positive ones.
Data Consistency − Constraints can be used to maintain the consistency of the data in a database. These restrictions can make sure that the foreign key value in one table comes after the main key value in another.
Data integrity − Constraints make a guarantee that the data in a database is accurate and full. A restriction, for instance, would prevent someone from entering a null value into a field that demands one.
Types of Constraints in DBMS
- Domain Constraints
- Key Constraints
- Entity Integrity Constraints
- Referential Integrity Constraints
- Tuple Uniqueness Constraints
Domain Constraints
Domain constraints in a database table are rules that outline the permitted values for a particular property or field. These limitations ensure data consistency and help stop the database from being filled with erroneous or inconsistent data. Examples of domain limitations in a DBMS include the following:
Data type constraints −The types of data that can be stored in a column are limited by these restrictions. While a column designated as INTEGER may only accept integer values, a column established as VARCHAR can accept string values as well.
Length Constraints − The types of data that can be stored in a column are limited by these restrictions. While a column designated as INTEGER may only accept integer values, a column established as VARCHAR can accept string values as well.
Range constraints − Range limitations define a column’s permissible range of values. For instance, a column marked as DECIMAL(5,2) may only accept decimal values up to 5 digits long with 2 decimal places.
Nullability constraints − Nullability restrictions are limitations on a column’s ability to accept NULL values. A column with the NOT NULL definition, for instance, cannot accept NULL values.
Unique constraints − Unique constraints are constraints that demand the presence of singular values in a column or collection of columns. For instance, in a column with the UNIQUE specification, duplicate values are not permitted.
Check constraints − Data checking constraints: These restrictions specify a condition that must be true for any data entered into the column. An age larger than zero can only be accepted for a column with the definition CHECK (age > 0).
Default constraints − Constraints by default: If no value is specified, default constraints will automatically assign a value to a column. For instance, if no additional value is supplied, the default value for a column will be 0, as in the example above.
Key Constraints
Key constraints are guidelines that a DBMS uses to guarantee data consistency and correctness in a database. To ensure that the data is accurate, they specify how the values in a table’s one or more columns link to the values in other tables.
In DBMS, there are several key constraint kinds, including −
Primary Key Constraint − An individual identifier for each entry in a database is known as a primary key constraint. As its means of identification, it ensures that each database record has a single, unique value—or a pair of values—that cannot be null.
Foreign Key Constraint − A foreign key constraint refers to the main key in another table. It makes sure that a column or group of columns’ values in one table match the primary key column(s) in another table.
Unique Constraint − A foreign key constraint is a reference to a primary key in a different table. It makes sure that the primary key column(s) in one table corresponds to the values of a column or group of columns in the other table.
Entity Integrity Constraints
Entity integrity constraints (EICs) are used by database management systems to impose regulations that ensure a table’s main key is distinct and not null. EICs, which were developed to prevent the generation of duplicate or incomplete entries, are responsible for maintaining the consistency and integrity of the data in a database.
One or more fields known as the primary key uniquely identify each item in a table in a relational database. EICs ensure that the primary key value for each row is unique and not null. Let’s look at the “Employees” database, which has the columns “EmployeeID” and “Name.” The EmployeeID column serves as the table’s main key. In this database, an EIC would confirm that the distinct EmployeeID value for each entry is there and that it is not null.
Referential Integrity Constraints
Referential integrity constraints (RICs) are used by database management systems to maintain the consistency and integrity of connections between tables. RICs ensure that the data in a database is constantly consistent by preventing connections between entries that don’t exist from being generated and by deleting records that have related records in other tables.
Take as an example the “Orders” and “Customers” tables. The “Customers” database’s primary key column matches the “CustomerID” foreign key field in the “Orders” dataset. Each value in the “CustomerID” column of the “Orders” database must exist in the “Customers” table’s main key column in order for a RIC on this connection to function.
The database management system would reject the insertion and let the user know there was a problem if a record was attempted to be inserted into the “Orders” table with a non-existent “CustomerID” value.
Tuple Uniqeness Contraints
To ensure that each item or tuple in a table is unique, database management systems employ restrictions referred to as tuple uniqueness constraints (TUCs). Unlike Entity Integrity Constraints (EICs), which only require uniqueness on certain columns or sets of columns, TUCs impose uniqueness on the whole row or tuple.
In order to prevent this, TUCs ensure that no two rows in a table have the same values for any two columns. In situations when it is crucial to prevent the generation of duplicate entries, even if the individual column values are not unique, this might be useful.
As an example, have a look at the “Sales” database, which has the columns “TransactionID,” “Date,” “CustomerID,” and “Amount.” A TUC on this table would make it impossible to duplicate specific column values.