SQL 主键 自动编号 主键自增
2014-06-18 16:06
211 查看
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)
相关文章推荐
- M| SQL 导入导出的时候数据库表的主键和自动编号丢失 怎么办
- Access转成Sql 2008步骤,同时解决自动编号问题,主键,id数值不重置。
- SQL创建自动编号的主键
- 小议SQL_Server主键和自动编号问题
- Access转成Sql 2008步骤,同时解决自动编号问题,主键,id数值不重置。
- 怎样在Access和MS Sql Server中使一列字段自动编号增加(如 id int 主键 这一列自动增加)
- Access转成Sql 2008步骤,同时解决自动编号问题,主键,id数值不重置
- SQL 2000 Insert返回自动编号id三种方法比较
- Insert怎样返回该条纪录的主键(自动编号)的值?
- 推荐SQL Server 重新恢复自动编号列的序号的sql代码
- sql 自动增长的主键
- MS SQL SERVER中自动编号问题,以ASP为例
- SQL中某字段设为自动编号及清除后ID重新从1开始计算的方法
- 如何用JET SQL DDL创建自动编号GUID字段
- sql 设置主键自动加1
- 三种获得自动生成主键的方法,getGeneratedKeys,专用SQL和可更新的结果集
- [学习]利用SqlDataAdapter Insertcommand 获取刚新增的自动编号ID值
- 利用 JdbcTemplate 自动返回 MS SQL SERVER 2005 自增主键值
- SQL怎样自动生成编号格式如:BV+YYYYMM+编号
- 同一表内,各部份行自动重新编号T-SQL处理代码