Sql Tutorial Class


Check constraint in sql

Check constraint in sql.


Check Constraint is used to check the validate data, data format etc.

For Example: 
Customer age must be greater than 18.
Customer phone number be in this format XX-XXXXXX


Check Constraint Example:


Create table NewOrder
(
CID varchar(10)not null,
Name varchar(20)not null,
Age int not null Constraint chkAge Check(Age>=18),
Address varchar(20)not null,
Phone varchar(10)Constraint chkphone check (Phone like '[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]'
)
Insert into NewOrder Values('001','Smit','18','234','23-232434')
Insert into NewOrder Values('002','Anu','18','234','01-238434')


ALTER/DROP CHECK constraint


Alter table NewOrder drop Constraint chkAge

Alter table NewOrder drop constraint chkphone




ALTER / ADD Check constraint


alter table NewOrder add Constraint chkAge check(Age>=18)

alter table NewOrder add Constraint chkphone check (Phone like '[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]')




CHECK constraint in video






CHECK constraint in Image

Check constraint in sql

No comments:

Post a Comment