Constraints
Constraints
This indicates that only a particular type of data may be entered into the
database or that only a particular sort of operation can be performed on
the data inside.
Domain Constraints
Key Constraints
Entity Integrity Constraints
Referential Integrity Constraints
Domain Constraints: 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.
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.
In DBMS, there are several key constraint kinds, including −
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.