Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 declare default constraint in table

Author  Topic 

frage2010
Starting Member

2 Posts

Posted - 2010-01-25 : 10:32:04
hello to everyone,

i have a problem and hope you can help me.

I would like to create a table with many constraints.
At first I would like to write the column defintion and after the last one I write the constraints.
My problemist that I couldn't find the right syntax for the default constraint.
Here is a sample: This one works but I want the other way

create tabel kunde
(kundennr int primary key,
Name varchar(20),
zahlart varchar(20) default 'Bar',
etc....
);


Like this way:
create table kunde
(kundennr int,
name varchar(20),
zahlart varchar(20),
datum datetime,
constraint kunde_kundennr_pk primary key(kundennr),
constraint kunde_zahlart zahlart default 'Bar' <-- this dosen't work's.
);


I hope you can help me and sorry for my english skills
frage2010

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-25 : 10:35:40
it should be zahlart varchar(20) constraint kunde_zahlart default 'Bar'
Go to Top of Page

frage2010
Starting Member

2 Posts

Posted - 2010-01-25 : 10:45:50
Thank's for you fast answer,

but thgis solution ist to declare the constraint with the column together.

I would like to declare the constraint after all columns with the other at the end of the table code.

frage2010
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-25 : 10:48:42
quote:
Originally posted by frage2010

Thank's for you fast answer,

but thgis solution ist to declare the constraint with the column together.

I would like to declare the constraint after all columns with the other at the end of the table code.

frage2010


then use

alter table kunde add constraint kunde_zahlart default ('Bar) for zahlart
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-01-25 : 10:59:02
unfortunately, can't be done the way you want -- it is incorrect syntax. either use visakh16's alter table sample, or define the constraint inline with the column definition.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-01-25 : 12:01:31
Can you explain WHY you don't want to do it on the table Declaration?

And does anyone know if England has a July 4th?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

SQLJames
Starting Member

35 Posts

Posted - 2010-01-25 : 12:40:30
quote:

And does anyone know if England has a July 4th?



Oh yeah, and good times are had by all! Especially the Pin-the-Tail-on-the-Benjamin-Franklin
Go to Top of Page
   

- Advertisement -