Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hello ,
I have a problem.I need to alter a foreign key in a table which I have created in MS SQL express 2008.

Here is the sql code for the table I have created.


SQL
CREATE TABLE marks
(
id idType identity not null primary key,
studentID int not null FOREIGN KEY REFERENCES student(id) ON UPDATE CASCADE,
marks int CHECK(marks >=0 and marks <= 100)
)




What i Need to do is Alter foreign key as follows

1.I need to drop studentID
2.I need change referential integrity of the column (which has foreign key)
Eg ON DELETE NO ACTION

Here is the code I tried

SQL
ALTER TABLE marks
ALTER COLUMN studentID ON DELETE NO ACTION
AlTER TABLE marks
DROP FOREIGN KEY studentID


Thank you
Posted

1 solution

Hi,

Try This
SQL
ALTER TABLE marks DROP CONSTRAINT [marks_studentID_FK];
ALTER TABLE marks DROP COLUMN studentID ;


I assume your foreign key name is marks_studentID_FK, Please replace the name with the correct foreign key name.This should do the trick.
 
Share this answer
 
Comments
Nadunwow 1-Feb-11 2:40am    
Thank you for the quick answer.
The problem is I have not used CONSTRAINT here.

if I have used CONSTRAINT here we can do that method.

Any way use method gives me this error message

Msg 3728, Level 16, State 1, Line 1
'studentID' is not a constraint.
Msg 3727, Level 16, State 0, Line 1
Could not drop constraint. See previous errors.
Shahriar Iqbal Chowdhury/Galib 2-Feb-11 2:26am    
hi,
I assume the constraint name, you have to find the correct constraint name and put that on query i place of marks_studentID_FK
then it should work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900