DEFAULT constraint in sql server


DEFAULT – This constraint provides a default value when specified none for this column.

Example-
Following create a table PRODUCT, column DTCREATE has default value GETDATE() it will store the system’s date-time by default if the value is not specified for this column.

CREATE TABLE PRODUCT
(
ID INT NOT NULL,
SKU VARCHAR(20) NOT NULL,
TITLE VARCHAR(200) NOT NULL,
PRICE MONEY NOT NULL,
DESCRIPTION VARCHAR(2000)  NULL,
DTCREATE DATETIME DEFAULT GETDATE()
)

If the PRODUCT table has already been created and you want to apply default price in the PRICE column use the following query (for SQL server).


ALTER TABLE PRODUCT ADD CONSTRAINT DF_SomePrice DEFAULT 20.00 FORPRICE

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...