How I Add a column with a default value to an existing table in SQL?

Forums SQLHow I Add a column with a default value to an existing table in SQL?
Staff asked 2 years ago

Answers (2)

Add Answer
himani vaghasiya Marked As Accepted
Staff answered 2 years ago

Using this

ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
WITH VALUES

 

Staff answered 2 years ago

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

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    City varchar(255) DEFAULT 'Sandnes'
);

 

Subscribe

Select Categories