删除列出现的提示“不是约束”是怎么回事啊??

cyberfoxoooo 2004-05-09 05:17:58
一个删除列的SQL语句:
ALTER TABLE 表名 DROP 列名

这个列是主键,执行这个语句的时候提示 “列名 不是约束”
可是,我看了CHECK约束那个选项卡,没有约束啊,这是怎么回事呢:??
...全文
2784 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
internetcsdn 2004-05-09
  • 打赏
  • 举报
回复
--这样吧,你在查询分析器中执行下述语句,就可以将字段删除了(有约束什么的会自动删除)

既然是这样,就再执行一次就行啦
cyberfoxoooo 2004-05-09
  • 打赏
  • 举报
回复
刚才用
ALTER TABLE 表名 DROP 索引名(在 索引/键 中出现的 ,值是 PK__表名)
执行成功!!!
然后我又执行了 ALTER TABLE 表名 DROP 列名
结果该列被删除!!

可是刚高兴没两分钟,我又用
add id int identity(1,1) primary key 建了一个列,结果这回 索引名 成了 PK__表名__5812160E !!!!!

崩溃,难道同样的语句 add id int identity(1,1) primary key ,竟会出现不同的索引名?? 那么如果用程序怎么删除列呢???
zjcxc 2004-05-09
  • 打赏
  • 举报
回复
--执行这个语句,得到删除主键的语句,然后再执行得到的语句,删除主键:

select 'alter table ['+c.name+'] drop constraint ['+e.name+']'
from sysindexes a
join sysindexkeys b on a.id=b.id and a.indid=b.indid
join sysobjects c on b.id=c.id and c.xtype='U' and c.name<>'dtproperties'
join syscolumns d on b.id=d.id and b.colid=d.colid
join sysobjects e on c.id=e.parent_obj
where a.indid not in(0,255)
and c.name='表名'
and d.name='字段名'
and e.xtype='PK'
cyberfoxoooo 2004-05-09
  • 打赏
  • 举报
回复
啊!! 可是我是要用ADO.NET提交SQL给数据库进行更改的啊, 这样长的语句可能在程序里不方便吧, 创建这个列的语句是 alter table 表 add id int identity(1,1) primary key
原来那个表没有主键,只好添了一个,现在删除这个列应该怎么做呢??
cyberfoxoooo 2004-05-09
  • 打赏
  • 举报
回复
ALTER TABLE 表名 DROP COLUMN 列名

提示“对象PK_表名”依赖于“列名”

zjcxc 2004-05-09
  • 打赏
  • 举报
回复
--这样吧,你在查询分析器中执行下述语句,就可以将字段删除了(有约束什么的会自动删除)

declare tb cursor local for
--默认值约束
select sql='alter table ['+b.name+'] drop constraint ['+d.name+']'
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='表名'
and a.name='字段名'
union all --外键引用
select s='alter table ['+c.name+'] drop constraint ['+b.name+']'
from sysforeignkeys a
join sysobjects b on b.id=a.constid
join sysobjects c on c.id=a.fkeyid
join syscolumns d on d.id=c.id and a.fkey=d.colid
join sysobjects e on e.id=a.rkeyid
join syscolumns f on f.id=e.id and a.rkey=f.colid
where e.name='表名'
and d.name='字段名'
union all --索引
select case e.xtype when 'PK' then 'alter table ['+c.name+'] drop constraint ['+e.name+']'
else 'drop index ['+c.name+'].['+a.name+']' end
from sysindexes a
join sysindexkeys b on a.id=b.id and a.indid=b.indid
join sysobjects c on b.id=c.id and c.xtype='U' and c.name<>'dtproperties'
join syscolumns d on b.id=d.id and b.colid=d.colid
join sysobjects e on c.id=e.parent_obj
where a.indid not in(0,255)
and c.name='表名'
and d.name='字段名'

declare @s varchar(8000)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb

ALTER TABLE 表名 DROP column 字段名
cyberfoxoooo 2004-05-09
  • 打赏
  • 举报
回复
索引/键 这个选项卡中哪一个指明了约束呢:? 我看到有一个 约束 ,不过是灰的。
下面倒是有一个 CLUSTERED 选了,怎么写SQL语句呢??
netcoder 2004-05-09
  • 打赏
  • 举报
回复 3
你的少了个 COLUMN 关键字
netcoder 2004-05-09
  • 打赏
  • 举报
回复 5
ALTER TABLE 表名 DROP column 列名
zjcxc 2004-05-09
  • 打赏
  • 举报
回复
既然是主键列,就要先删除主键约束才行,主键约束在索引/键那项中查看的.

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧