Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default Constraint

Status
Not open for further replies.
Oct 17, 2006
227
HI

Was looking at creating a table where to use the default constraint.

CREATE TABLE Customer
(
CustomerID INT CONSTRAINT pk_customer_cid PRIMARY KEY,
CustomerName VARCHAR(30),
CustomerAddress VARCHAR(50) CONSTRAINT df_customer_Add DEFAULT 'UNKNOWN'
)


If I insert

Insert into Customer
Values ( 113, NULL,NULL)


select * from Customer

The customer adress comes up as NULL as opposed to 'UNKNOWN'


I thought about specifying


CustomerAddress VARCHAR(50) CONSTRAINT df_customer_Add NULL DEFAULT 'UNKNOWN'

but still does not work. Am i doing something wrong?

 
To clarify why Frederico's solution will work:
A default constraint is used when no value is specified. You had explicitly indicated you wanted a NULL value.

I've found default constraints seem to be designed for "extending" your database, without extending your code base. The code doesn't know about the new field, so doesn't try to put anything in, and then gets the default value.

Lodlaiden

You've got questions and source code. We want both!
Here at tek tips, we provide a hand up, not a hand out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top