3

I'm trying to add a constraint to a table so that it displays one of the columns as the current date plus 10 days. Here's what I've tried so far (I'm very new to SQL):

ALTER TABLE         orders
ADD CONSTRAINT  default_date
DEFAULT         DATEADD (DAY,10,required_date) FOR required_date

Halp!

Edit: I've also tried this now:

ALTER TABLE         orders
ALTER COLUMN        required_date
ADD CONSTRAINT      required_date_plus_ten
DEFAULT             DATEADD (DAY,10,required_date)

Edit: Thanks to ypercube & my classmate. The final code is:

ALTER TABLE       orders
ADD CONSTRAINT    default_date
DEFAULT           (DATEADD (DAY,10,'required_date')) FOR required_date;
isolatedhowl
  • 171
  • 2
  • 6
  • 17
  • Which DBMS are you using? Oracle? PostgreSQL? –  Nov 23 '12 at 22:30
  • 2
    Are you sure your want a constraint? Perhaps you want a calculated column in a view (returning some existing column plus 10 days)... – BellevueBob Nov 23 '12 at 22:44
  • I'm using the Microsoft SQL management server. I'm doing a school project and it says to add the constraint: 'required date' (the column name I'm adding the constraint to) should default to today's date plus 10 days. – isolatedhowl Nov 23 '12 at 22:48
  • then yes the term is being used very loosely and it's actually a calculated field. Also the constraint can't actually be against the 'current' date or it will never be reached. – Michael Durrant Nov 23 '12 at 23:11

1 Answers1

3

The syntax in SQL-Server, for adding a DEFAULT value to an existing column is:

ALTER TABLE     orders
ADD CONSTRAINT  required_date_plus_ten
DEFAULT         DATEADD(day, 10, GETDATE())
FOR             required_date ;

Tested in SQL-Fiddle

ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235