SQL Server Constraint- Check , Default

We have covered the below SQL Server Constraints in the previous blog

Unique and Not Null Constraints

http://oracleappshelp.com/2020/04/22/sql-server-unique-not-null-constraint-in-table/

Primary Key , Foreign Key Constraints

http://SQL Server Check Constraint,SQL Server Default Constraint

SQL Server Check Constraint

Lets consider we need to put some validation on some columns like records should be inserted

if the Age is >= 18

if City =’New Delhi’

if Status =’New’

The SQL Server Check Constraint allows you to add these condition on the specific column .

CREATE TABLE oracleappshelpUsers
(
    UserId				INT 				NOT NULL UNIQUE,
    GivenName 			VARCHAR(128) 		NOT NULL,
    MiddelName 			VARCHAR(10)					,
    FamilyName 			VARCHAR(128) 		NOT NULL,
	Age 				INT 				CHECK (Age>=18)				
);

SQL Server Default Constraint

It is sometimes required to setup the DEFAULT value for a particular Column .

CREATE TABLE oracleappshelpUsers
(
    UserId				INT 				NOT NULL UNIQUE,
    GivenName 			VARCHAR(128) 		NOT NULL,
    MiddelName 			VARCHAR(10)					,
    FamilyName 			VARCHAR(128) 		NOT NULL,
	Age 				INT 				CHECK (Age>=18),
	Status 				VARCHAR(20)			NOT NULL	DEFAULT 'NEW'	
);

Bookmark and Share