SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


problem with naming default constraint and using it while adding a not null column


problem with naming default constraint and using it while adding a not null column

Author
Message
Amit kulkarni-491236
Amit kulkarni-491236
Mr or Mrs. 500
Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)

Group: General Forum Members
Points: 540 Visits: 219
i have a table 'staff'... it already contains some 10-15 records
now i want to add a new column StaffCode to the table which is not null.... iam adding a default value otherwise it will throw error

the syntax is:
ALTER TABLE Staff ADD StaffCode varchar(10) DEFAULT ' ' NOT NULL;
GO

iam able to create a default value and add the column but i want to change the name of default constraint... how to do it...

i tried the following steps

ALTER TABLE Staff ADD StaffCode varchar(10)NULL;
GO

ALTER TABLE Staff ADD CONSTRAINT Staff_StaffCode_df DEFAULT ' '
FOR StaffCode
GO

ALTER TABLE Staff ALTER COLUMN StaffCode varchar(10) NOT NULL;
GO

I get the error:-
Msg 4901, Level 16, State 1, Line 1
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'StaffCode' cannot be added to non-empty table 'Staff' because it does not satisfy these conditions.

Please help.... iam able to give default value but not able to give my own constraint name
Ramesh Saive
Ramesh Saive
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3472 Visits: 2643
ALTER TABLE Staff ADD StaffCode varchar(10) NOT NULL CONSTRAINT Staff_StaffCode_df DEFAULT(' ');

--Ramesh


Amit kulkarni-491236
Amit kulkarni-491236
Mr or Mrs. 500
Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)Mr or Mrs. 500 (540 reputation)

Group: General Forum Members
Points: 540 Visits: 219
thanks... i was able to get it

Regards,

Amit kulkarni
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search