您现在的位置是:网站首页> 编程资料编程资料
SQL Server设置主键自增长列(使用sql语句实现)_MsSql_
2023-05-26
823人已围观
简介 SQL Server设置主键自增长列(使用sql语句实现)_MsSql_
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)
复制代码 代码如下:
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)
您可能感兴趣的文章:
相关内容
- 常用SQL语句(嵌套子查询/随机等等)详细整理_MsSql_
- SQL语句的各个关键字的解析过程详细总结_MsSql_
- 存储过程实现(可带查询条件/万能分页/通用)_MsSql_
- 数据库中两张表之间的数据同步增加、删除与更新实现思路_MsSql_
- SQL Server数据库中批量导入数据的四种方法总结_MsSql_
- sqlserver数据库出现置疑的解决思路_MsSql_
- SQL Server游标的使用/关闭/释放/优化小结_MsSql_
- MSSQL 2005/2008 日志压缩清理方法小结_MsSql_
- Sql学习第四天——SQL 关于with cube,with rollup和grouping解释及演示_MsSql_
- sql server 还原数据库时提示数据库正在使用,无法进行操作的解决方法_MsSql_
