HOW TO USE DEFAULT CONSTRAINT IN SQL

DEFAULT in SQL

The DEFAULT constraint in SQL is used to set a default value for a column.

Example of DEFAULT constraint creation in SQL server-

CREATE TABLE Students (

ID INT NOT NULL UNIQUE,

FirstName VARCHAR(200),

Fees INT,

City VARCHAR(200) DEFAULT ‘Delhi’

);

Example to create DEFAULT constraint on existing table in SQL server-

ALTER TABLE Students

ADD  CONSTRAINT d_city DEFAULT ‘Delhi’ FOR City;

Example to DROP a UNIQUE constraint in SQL server –

ALTER TABLE Students

ALTER COLUMN City DROP DEFAULT;


Leave a Reply

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