Constraits
Constraits
DBMSDatabaseData Storage
Data Accuracy − Data accuracy is guaranteed by constraints, which make sure that
only true data is entered into a database. For example, a limitation may stop a
user from entering a negative value into a field that only accepts positive
numbers.
Data integrity − The accuracy and completeness of the data in a database are
ensured by constraints. For example, a constraint can stop a user from putting a
null value into a field that requires one.
• Domain Constraints
• Key Constraints
• Entity Integrity Constraints
• Referential Integrity Constraints
• Tuple Uniqueness Constraints
Domain Constraints
In a database table, domain constraints are guidelines that specify the acceptable
values for a certain property or field. These restrictions guarantee data
consistency and aid in preventing the entry of inaccurate or inconsistent data into
the database. The following are some instances of domain restrictions in a DBMS
−
• Data type constraints − These limitations define the kinds of data that can be kept in a
column. A column created as VARCHAR can take string values, but a column specified as
INTEGER can only accept integer values.
• Length Constraints − These limitations define the largest amount of data that may be
put in a column. For instance, a column with the definition VARCHAR(10) may only take
strings that are up to 10 characters long.
• Range constraints − The allowed range of values for a column is specified by range
restrictions. A column designated as DECIMAL(5,2), for example, may only take decimal
values up to 5 digits long, including 2 decimal places.
• Nullability constraints − Constraints on a column's capacity to accept NULL values are
known as nullability constraints. For instance, a column that has the NOT NULL definition
cannot take NULL values.
• Unique constraints − Constraints that require the presence of unique values in a column
or group of columns are known as unique constraints. For instance, duplicate values are
not allowed in a column with the UNIQUE definition.
• Check constraints − Constraints for checking data: These constraints outline a
requirement that must hold for any data placed into the column. For instance, a column
with the definition CHECK (age > 0) can only accept ages that are greater than zero.
• Default constraints − Constraints by default: Default constraints automatically assign a
value to a column in case no value is provided. For example, a column with a DEFAULT
value of 0 will have 0 as its value if no other value is specified.
Key Constraints
Key constraints are regulations that a DBMS uses to ensure data accuracy and
consistency in a database. They define how the values in a table's one or more
columns are related to the values in other tables, making sure that the data
remains correct.
• Primary Key Constraint − A primary key constraint is an individual identifier for each
record in a database. It guarantees that each database entry contains a single, distinct
value—or a pair of values—that cannot be null—as its method of identification.
• Foreign Key Constraint − Reference to the primary key in another table is a foreign key
constraint. It ensures that the values of a column or set of columns in one table
correspond to the primary key column(s) in another table.
• Unique Constraint − In a database, a unique constraint ensures that no two values
inside a column or collection of columns are the same.
If you try to insert an entry with a duplicate or null EmployeeID, the database
management system will reject the insertion and produce an error. This
guarantees that the information in the table is correct and consistent.
EICs are a crucial component of database architecture and assist guarantee the
accuracy and dependability of the data contained in a database.
Referential Integrity Constraints
A database management system will apply referential integrity constraints (RICs)
in order to preserve the consistency and integrity of connections between tables.
By preventing links between entries that don't exist from being created or by
removing records that have related records in other tables, RICs guarantee that
the data in a database is always consistent.
By the use of foreign keys, linkages between tables are created in relational
databases. A column or collection of columns in one table that is used as a foreign
key to access the primary key of another table. RICs make sure there are no
referential errors and that these relationships are legitimate.
Consider the "Orders" and "Customers" tables as an illustration. The primary key
column in the "Customers" database corresponds to the foreign key field
"CustomerID" in the "Orders" dataset. A RIC on this connection requires that each
value in the "CustomerID" column of the "Orders" database exist in the
"Customers" table's primary key column.
If an attempt was made to insert a record into the "Orders" table with a non-
existent "CustomerID" value, the database management system would reject the
insertion and notify the user of an error.
Similar to this, the database management system would either prohibit the
deletion or cascade the deletion in order to ensure referential integrity if a record
in the "Customers" table was removed and linked entries in the "Orders" table.
TUCs, then, make sure that no two rows in a table have the same values for every
column. Even if the individual column values are not unique, this can be helpful
in cases when it is vital to avoid the production of duplicate entries.
Consider the "Sales" table, for instance, which has the columns "TransactionID,"
"Date," "CustomerID," and "Amount." Even if individual column values could be
duplicated, a TUC on this table would make sure that no two rows have the same
values in all four columns.
The database management system would reject the insertion and generate an
error if an attempt was made to enter a row with identical values in each of the
four columns as an existing entry. This guarantees the uniqueness and accuracy
of the data in the table.
TUCs may be a helpful tool for ensuring data correctness and consistency overall,
especially when it's vital to avoid the generation of duplicate entries.
Conclusion
Constraints are a crucial part of every database management system, and
creating and maintaining high-quality databases requires a grasp of how to apply
them effectively. To guarantee data's correctness, consistency, and integrity,
constraints in DBMS apply rules to the data. They stop data from being added,
altered, or removed that is incorrect or incomplete. EICs, RICs, TUCs, and Check
Constraints are a few different kinds of constraints. Building and maintaining
high-quality databases that allow for informed business decisions requires
constraints.