如何在MS SQL中更改外键 [英] How to Alter the foreign key in MS SQL

查看:70
本文介绍了如何在MS SQL中更改外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一个问题.我需要更改在MS SQL Express 2008中创建的表中的外键.

这是我创建的表的sql代码.


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.


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)
)





我需要做的是如下更改外键

1.我需要删除StudentID
2.我需要更改列的引用完整性(具有外键)
例如删除不采取行动

这是我尝试过的代码





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

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



谢谢



Thank you

推荐答案



试试这个
Hi,

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



我假设您的外键名称为marks_studentID_FK,请使用正确的外键名称替换该名称.这可以解决问题.



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.


这篇关于如何在MS SQL中更改外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆