sql 主键 标识 默认值

本文介绍了SQL中主键及标识列的操作方法,包括设置标识插入、修改标识序列、查看和管理主键约束,以及如何添加和删除字段的默认值等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

SET IDENTITY_INSERT [dbo].[t_message] ON

INSERT INTO [dbo].[t_message]   (f_mid )

VALUES (55)

SET IDENTITY_INSERT [dbo].[t_message] OFF
GO select * from [dbo].[t_message] order by f_mid desc 标识序列 dbcc checkident('t_member',reseed,@@IDENTITY)

 主键

View Code
select name from sysobjects 
where parent_obj=object_id('abc') and  xtype='pk'

alter table abc add constraint PK_bbb primary key(bbb)
alter table abc drop constraint PK_bbb

 

 标识修改

View Code
四个步骤:
1、增加一个int类型的列
   alter table add xinglie int
 2、更新新列的数据为自增列的数据
   update tb set xinglie=自增列名
3、删除自增列
   alter table tb drop column 自增列
4、更改新列名为原自增列的列名
   exec sp_rename N'tb.xinglie','原自增列名',N'column'  

 默认值

--说明:删除表的字段的原有约束  
alter table abc drop constraint DF_bbb      
--说明:添加一个表的字段的约束并指定默认值
alter table abc add constraint DF_ccc DEFAULT 5 for ccc
--查表字段的默认值
select b.text as df  
from syscolumns a left join syscomments b on a.cdefault=b.id   
where a.id=object_id('abc') and a.name='ccc' 
--查表字段的默认值的约束
select b.name,a.* from sysobjects a,syscolumns b
where a.id=b.cdefault and a.parent_obj=object_id('abc') and  a.xtype='D' and b.name='ccc'

 

 

 

posted on 2013-04-03 14:15 Spider024 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/spider024/archive/2013/04/03/2997832.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值