您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页SQLServer设置主键自增长列(使用sql语句实现)

SQLServer设置主键自增长列(使用sql语句实现)

来源:意榕旅游网

1.新建一数据表,里面有字段id,将id设为为主键
代码如下:
create table tb(id int,constraint pkid primary key (id))
create table tb(id int primary key )

2.新建一数据表,里面有字段id,将id设为主键且自动编号
代码如下:
create table tb(id int identity(1,1),constraint pkid primary key (id))
create table tb(id int identity(1,1) primary key )

3.已经建好一数据表,里面有字段id,将id设为主键
代码如下:
alter table tb alter column id int not null
alter table tb add constraint pkid primary key (id)

4.删除主键
代码如下:
Declare @Pk varChar(100);
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('tb') and xtype='PK';
if @Pk is not null
exec('Alter table tb Drop '+ @Pk)

您可能感兴趣的文章:

  • sqlserver2005自动创建数据表和自动添加某个字段索引
  • SQL Server 打开或关闭自增长
  • SqlServer Mysql数据库修改自增列的值及相应问题的解决方案
  • SQL Server 2008怎样添加自增列实现自增序号
  • SQL Server修改标识列方法 如自增列的批量化修改
  • Oracle 实现类似SQL Server中自增字段的一个办法
  • SQL SERVER 自增列
  • SQL Server 中调整自增字段的当前初始值
  • SQL Server数据表字段自定义自增数据格式的方法
  • Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2

    违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

    本站由北京市万商天勤律师事务所王兴未律师提供法律服务