简单的问题,在线等待!

zsf0615020117 2008-11-07 11:00:06
想删除主键

alter table s_t drop primary (id) ;

运行时提示缺失关键字 为什么?
...全文
131 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
等不到来世 2008-11-08
  • 打赏
  • 举报
回复
create table tb(id int primary key,c int)
insert tb values(1,2)
select * from tb

--查主键约束名
exec sp_pkeys 'tb' -- PK_NAME = 'PK__tb__5A1A5A11'
--删除主键约束
alter table tb drop constraint PK__tb__5A1A5A11
--删除主键
alter table tb drop column id

--结果:
select * from tb
/*
c
-----------
2

(1 row(s) affected)

*/
pengxuan 2008-11-08
  • 打赏
  • 举报
回复

alter table s_t drop 主键的名字
zsf0615020117 2008-11-07
  • 打赏
  • 举报
回复

在执行5楼的代码时它还是提示出错
那位高手知道为什么?

水族杰纶 2008-11-07
  • 打赏
  • 举报
回复
--查出主键约束名称
EXEC sp_pkeys @table_name='表'
--删除约束
ALTER TABLE tablename
Drop CONSTRAINT 主键约束名称
zsf0615020117 2008-11-07
  • 打赏
  • 举报
回复
我是用语句
alter table s_t add primary key(id);
填加主键的
又应该怎么删除呢
dawugui 2008-11-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 zsf0615020117 的帖子:]
想删除主键

alter table s_t drop primary (id) ;

运行时提示缺失关键字 为什么?
[/Quote]
alter table s_t drop primary_name
水族杰纶 2008-11-07
  • 打赏
  • 举报
回复
--参考删除约束
1)禁止所有表约束的SQL
select 'alter table '+name+' nocheck constraint all' from sysobjects where type='U'

2)删除所有表数据的SQL
select 'TRUNCATE TABLE '+name from sysobjects where type='U'

3)恢复所有表约束的SQL
select 'alter table '+name+' check constraint all' from sysobjects where type='U'

4)删除某字段的约束
declare @name varchar(100)
--DF为约束名称前缀
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='字段名' and b.name like 'DF%'
--删除约束
alter table 表名 drop constraint @name
--为字段添加新默认值和约束
ALTER TABLE 表名 ADD CONSTRAINT @name DEFAULT (0) FOR [字段名]对字段约束进行更改
--删除约束
ALTER TABLE tablename
Drop CONSTRAINT 约束名
--修改表中已经存在的列的属性(不包括约束,但可以为主键或递增或唯一)
ALTER TABLE tablename
alter column 列名 int not null
--添加列的约束
ALTER TABLE tablename
ADD CONSTRAINT DF_tablename_列名 DEFAULT(0) FOR 列名
--添加范围约束
alter table tablename add check(性别 in ('M','F'))
  • 打赏
  • 举报
回复
create table test2( id int,name varchar(10),constraint pk_id primary key(id))
alter table test2 drop pk_id

34,590

社区成员

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

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