无法删除对象 'Student',因为该对象正由一个 FOREIGN KEY 约束引用。

来源:互联网 发布:伊宁关键字排名优化 编辑:程序博客网 时间:2024/05/17 19:57

创建表格

create table Student(    Sno char(9) primary key,    Sname char(20) unique,    Ssex char(2),    Sage smallint,    Sdept char(20));    create table SC(    Sno char(9),    Cno char(4),    grade smallint,    primary key(Sno,Cno),    foreign key (Sno) references Student(Sno),    foreign key(Cno)  references Course(Cno));create table Course(    Cno char(4) primary key,    Cname char(40) not null,    Cpno char(4),    Ccredit smallint,    foreign key(Cpno) REFERENCES Course(Cno));
drop table Student

想要删除Student表格,结果报错:

无法删除对象 ‘Student’,因为该对象正由一个 FOREIGN KEY 约束引用。

执行:

select fk.name,fk.object_id,OBJECT_NAME(fk.parent_object_id) as referenceTableNamefrom sys.foreign_keys as fkjoin sys.objects as o on fk.referenced_object_id=o.object_idwhere o.name='Student'ALTER TABLE dbo.SC DROP CONSTRAINT FK__SC__Sno__1B29035Fdrop table Student

参考:
这里写图片描述

阅读全文
0 0
原创粉丝点击