How to Create Column Constraint and Table Constraint in SQL

In this article I am going to explain about Column Constraint and Table Constraint in SQL.
  • 2551

Introduction 

In SQL you can set constraint on column or table according to need. Column constraint only applies on specific column. A table constraint is used when you want to set constraint on more than one column in a table.  Table constraints must be used when you need to put more than one column  into a single constraint.

Example of column Constraint

Statement that create primary key

create table empinfo

(

id int primary key,

name varchar(15),

age int,

city varchar(15),

salary money

)

Example of Table Constraint

In this example we are creating a primary key constraint using  empid and emp_panno. It is example of table constarint.

Statement that create Primary key constraint using two attribute in empinfo table

create table empinfo

empid int,

emp_panno int,

name varchar(15),

age int,

city varchar(15),

salary money

CHECK(salary >=10000 and salary <=1000000)

constraint p_K primary key(empid,emp_panno)

)


© 2020 DotNetHeaven. All rights reserved.