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

Fatal Error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails.

Problem: Sometimes, we get the following error when trying to make a connection in Rails.  FATAL ERROR: Peer authentication failed for us...