求SQL语句:如何增加和删除一个列的unique约束?

anzfox 2006-08-27 08:40:42
假如有表student 列sname
alter table...后面怎么写来增加unique约束 及删除unique约束?

我看书上有写alter table student drop unique(sname) 在查询分析器中执行不成功!
...全文
1826 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyxfly 2006-08-28
  • 打赏
  • 举报
回复
sp_help aaa拿掉,因为本来我没加约束名,用那个来看了一下约束名 ^_^
xyxfly 2006-08-28
  • 打赏
  • 举报
回复
create table aaa
(
uniq varchar(10),
constraint UQ__aaa__09B45E9A unique(uniq)--非要那样用的话,在这里加上约束名
)
insert aaa
select 'aa'
select * from aaa
go
sp_help aaa
go
alter table aaa drop unique(uniq,UQ__aaa__09B45E9A)
go
drop table aaa
xyxfly 2006-08-28
  • 打赏
  • 举报
回复
alter table student drop constraint 约束名
anzfox 2006-08-28
  • 打赏
  • 举报
回复
那在sql server中要删除一个字段的unique约束应该怎么写啊??

我写alter table student add unique(sname)都没有问题!
xyxfly 2006-08-28
  • 打赏
  • 举报
回复
alter table student drop unique(sname)

sql server不支持这种写法
anzfox 2006-08-28
  • 打赏
  • 举报
回复
sname不是主键啊 我就alter table student drop unique(sname)
然后就提示错误:
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'unique' 附近有语法错误。

大虾们帮帮忙啊!!
polestarxu 2006-08-27
  • 打赏
  • 举报
回复
up
BS打广告的
digi_hb_002 2006-08-27
  • 打赏
  • 举报
回复
up

---------------------------------------------------------------------------------------------
腰缠70元到月入近10万
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=453

IT工程师 毕业4年我年薪涨到30万
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=511

26岁青年坐拥千万域名资产 从小玩家变成CEO
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=515

程序员的酸甜苦辣:告别Coding
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=341

从月薪3500到700万(一)
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=170

网络草根月赚3000的十种方法
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=517

如果我是女的 我肯定不会嫁给做网站的
http://www.hunbei.com.cn/Article/ArticleShow.asp?ArticleID=512
fish_yht 2006-08-27
  • 打赏
  • 举报
回复
楼上正解
xyxfly 2006-08-27
  • 打赏
  • 举报
回复
alter table student drop constraint 约束名
turenjie 2006-08-27
  • 打赏
  • 举报
回复
alter table student drop unique(sname) 在查询分析器中执行不成功!??
你要改的字段是否为该表的主键,如果为主键应先cancel掉主键的头衔!

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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