14.4.10

Defaults in sql server

Default:
Default is one of the nice concept in SQL Server. It enables us to assign a default value to the table. While inserting a record in the table, if we didnt specify any values to a particular column the data will be null (If the column is a nullable column). If there is any default value for this column, it will assigned to that column.

Scenario:

I am having a scenario like, Instead of a null value, empty( '') should be populated. Is it possible?

Script:

DROP TABLE VENKATTABLE
CREATE TABLE VENKATTABLE(ID INT, VAL VARCHAR(100))
ALTER TABLE VENKATTABLE
ADD CONSTRAINT VENKATDEFAULT_VAL
DEFAULT '' FOR VAL
INSERT INTO VENKATTABLE (ID) VALUES (1)
SELECT * FROM VENKATTABLE


Instead of altering the table, we can provide the default value in the table creation itself.


DROP TABLE VENKATTABLE
CREATE TABLE VENKATTABLE(ID INT, VAL VARCHAR(100) DEFAULT '')


Cheers,
Venkatesan Prabu .J

No comments:

Post a Comment