表users(id,name)
id与name都已经存有数据。
其中id,int,但不是主键
-----------------------------------------------
我现在想把id 改成主键int ,递增1。但是name数据不能改动.
而id的数据改成从1开始递增。因为原本的id数据不是从1开始。
请问我要怎么写t-sql小弟sql2005

解决方案 »

  1.   

    如果没看明白的话,举个例子
    id  name
    10  中国
    45  美国
    50  英国
    ---------------
    上面的是修改之前的。下面的时候修改之后的
    id  name
    1  中国
    2  美国
    3  英国
      

  2.   

    select identity(int,1,1) as id,name into tb2 from tb 
      

  3.   

    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (id int,name varchar(4))
    insert into [tb]
    select 10,'中国' union all
    select 45,'美国' union all
    select 50,'英国'select identity(int,1,1) as id,name into tb2 from tb 
    select * from tb2
    /*
    id name
    1 中国
    2 美国
    3 英国
    */
      

  4.   


    --> 数据库版本:
    --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
    --> 测试数据:[TB]
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TB]') 
    AND type in (N'U')) 
    DROP TABLE [TB]
    GO---->建表
    create table [TB]([id] int,[name] varchar(4))
    insert [TB]
    select 10,'中国' union all
    select 45,'美国' union all
    select 50,'英国'
    GOwith yy as
    (select *,ROW_NUMBER()over (order by getdate()) as pp from TB)
    update yy
    set id=pp--> 查询结果
    SELECT * FROM [TB]
    --> 删除表格
    --DROP TABLE [TB]