Site icon Technology and Trends

What are Database Constraints?

Database Constraints are rules/restrictions that are applied to the database table columns to limit the type of data that can be persisted in a table. It provides a way to implement business logic and rules in the database. It also prevents errors and can improve the accuracy and reliability of the database as a whole.

Database Constraints can be applied for single or multiple fields in an SQL table during the creation of a table or after creating using the ALTER TABLE command.

Common constraints

Various types of constraints are used to do different checks on the data. Some constraints check null values in the data, whereas some check the integrity of the data. Integrity constraints are used to ensure the accuracy and consistency of data in a relational database.

ConstrainsDefinition
NOT NULLPrevents/Restricts NULL value from being inserted into a column
DEFAULTAutomatically assigns a default value for a column when none is specified
PRIMARY KEYUniquely identifies rows/records within a database table
FOREIGN KEYUniquely identifies rows/records from external database tables
UNIQUEEnsures Unique values are inserted into the field
CHECKCheck values within a column against certain conditions
INDEXIndexes a field providing faster retrieval of records.

Conclusion

In this blog post, we learned about database constraints and how they can be used while creating a table.

Exit mobile version