SQL Constraints with Examples – Database Constraints – How to add Constraints in SQL – Intellipaat

SQL Constraints with Examples –  Database Constraints –  How to add Constraints in SQL – Intellipaat


Hey guys welcome back to this SQL
tutorial series. In the session we will learn about constraints. So constraints
are basically used to specify some rules on the data and limit the type of data
that can go into a table and in this session we will look at these four
constraints not null, default, unique and primary key. Let’s start with a not null
constraint. So by default a column can hold null values and with the help of a
not constraint we can ensure that a column does not have null values. So the
not null constraint basically ensures that a field always contains a value for
example if we assign the not null constraint to this e salary column over
here then every single entry of this column should have a value then we have the
default constraint. So the default constraint is used to provide a default
value for a column. So the default value will be added to all the new records if
no other value is specified for example if we assign the default constraint to
the e_age column and set a default value of 25 then all the entries of this
column will have the default value of 25 if no other value is specified during
insertion. Then we have the unique constraint which ensures that all the
values in a column are different for example if we assign the unique
constraint to this e_name column then every entry of this column should have a
unique value and finally we have the primary key constraint so the primary
key constraint uniquely identifies each record in a table or simply put it is
just a combination of the not null constraint and the unique constraint. So
when we assign the primary key constraint to a column then that column
shouldn’t have any null values and all the entries should also be unique. For
example if we assign the primary key constraint to the e_id column
then there should be no null values in this column and all the entries should
also be unique. So these were some of the constraints in
SQL. Thanks for attending the session and let’s meet in the next class

One comment

  1. Got a question on SQL Constraints? Do write it in the comment and you will get a response immediately by our expert. For MS SQL Server training & certification, call us at US: 1800-216-8930 (Toll Free) or India: +917022374614. You can also write us at [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *