0 votes
in Sql by
What is a constraint? Tell me about its various levels.

1 Answer

0 votes
by

The constraint is used to specify the rule and regulations that allows or restricts what values/data will be stored in the table. It ensures data accuracy and integrity inside the table. It enforces us to store valid data and prevents us from storing irrelevant data. If any interruption occurs between the constraint and data action, the action is failed. Some of the most commonly used constraints are NOT NULL, PRIMARY KEY, FOREIGN KEY, AUTO_INCREMENT, UNIQUE KEY, etc.

The following syntax illustrates us to create a constraint for a table:

CREATE TABLE table_name (    

    column1 datatype constraint,    

    column2 datatype constraint,   

    .........    

);    

SQL categories the constraints into two levels:

Column Level Constraints: These constraints are only applied to a single column and limit the type of data that can be stored in that column.

Table Level Constraints: These constraints are applied to the entire table and limit the type of data that can be entered.

Related questions

+1 vote
asked Oct 31, 2020 in Azure by rahuljain1
+2 votes
asked Oct 24, 2019 in Interview Question by AdilsonLima
...