我用这个,但无法取消自动加1的属性
ALTER TABLE aaa DROP CONSTRAINT PK_aaa  
还有修改主键 如何增加自动加1的属性
ALTER   TABLE  aaa  ADD   Constraint   PK_aaa Primary key(id)

解决方案 »

  1.   

    自动加1的属性是要设置identity属性
      

  2.   

    我用这个,但无法取消自动加1的属性 
    ALTER TABLE aaa DROP CONSTRAINT PK_aaa  
    ---------------------你删除了主键后.如果还要取消自动加1 的属性,还得把标识列转成普通列
      

  3.   

    我还是个新手,还不怎么熟悉,如果是新建主键,那我是用下面这个的,但我现在需要的是修改
    ALTER TABLE aaa ADD id int identity(1,1) primary key 
      

  4.   

    我用这个,但无法取消自动加1的属性
    ALTER TABLE aaa DROP CONSTRAINT PK_aaa
    -----------------
    主键和自动加1的属性不是一码事,取消自动加1属性用界面操作:设计表,标识改为No。
    还有修改主键 如何增加自动加1的属性
    ALTER  TABLE  aaa  ADD  Constraint  PK_aaa Primary key(id)
    -----------------
    同上。
      

  5.   

    --创建测试表
    CREATE TABLE t1(ID int IDENTITY,A int)
    GO
    --插入记录
    INSERT t1 VALUES(1)
    GO--1. 将IDENTITY(标识)列变为普通列
    ALTER TABLE t1 ADD ID_temp int
    GOUPDATE t1 SET ID_temp=ID
    ALTER TABLE t1 DROP COLUMN ID
    EXEC sp_rename N't1.ID_temp',N'ID',N'COLUMN'
    INSERT t1 VALUES(100,9)
    GO--2. 将普通列变为标识列
    CREATE TABLE t1_temp(ID int,A int IDENTITY)
    SET IDENTITY_INSERT t1_temp ON
    INSERT t1_temp(ID,A) SELECT * FROM t1
    SET IDENTITY_INSERT t1_temp OFF
    DROP TABLE T1
    GOEXEC sp_rename N't1_temp',N't1'
    INSERT t1 VALUES(109999)
    GO--显示处理结果
    SELECT * FROM t1
    /*--结果:
    ID          A 
    ----------------- ----------- 
    1           1
    100         9
    109999      10
    --*/只能加列互相换,不能直接修改
      

  6.   

    SQL修改约束  
    --删除约束
    ALTER TABLE CustomData DROP CONSTRAINT FK_CustomData_CustomFormatInfo
    GO
    --关闭递增插入属性
    SET IDENTITY_INSERT OFF
    --打开递增插入属性
    SET IDENTITY_INSERT ON
    --修改增加约束
    ALTER TABLE [CustomFormatInfo] WITH NOCHECK ADD 
        CONSTRAINT [PK_CustomFormatInfo] PRIMARY KEY  CLUSTERED 
        (
            [CustomFormatID]
        )  ON [PRIMARY] 
    GO
    --修改表增加外键约束
    ALTER TABLE [CustomData] ADD 
        CONSTRAINT [FK_CustomData_CustomFormatInfo] FOREIGN KEY 
        (
            [CustomFormatID]
        ) REFERENCES [CustomFormatInfo] (
            [CustomFormatID]
        )
    GO
      

  7.   

    选择表-->右击"设计表"-->选择你要取消的字段-->下面有个列的说明.把"标识"这里改成"否"
      

  8.   


    --假设 t_t01是你的表,一共2列,id和id2,现在要将id设置成自增长
    select top 0 [id]=identity(bigint,1,1),[id2] into [tmp_t_t01_bak] from [t_t01] 
    set identity_insert [tmp_t_t01_bak] on 
    insert into [tmp_t_t01_bak]([id],[id2]) select [id],[id2] from [t_t01] 
    set identity_insert [tmp_t_t01_bak] off 
    drop table t_t01 
    exec sp_rename 'tmp_t_t01_bak','t_t01'
      

  9.   

    --关闭递增插入属性 
    SET IDENTITY_INSERT OFF 
    --打开递增插入属性 
    SET IDENTITY_INSERT ON 这两个是对所有表起作用的吧?
      

  10.   

    SET IDENTITY_INSERT t1_temp ON这个是什么意思?不写这个不行么?
      

  11.   

    这个标识只是针对t1_temp表作用
      

  12.   

    SET IDENTITY_INSERT (Transact-SQL)http://technet.microsoft.com/zh-cn/library/ms188059.aspx
      

  13.   

    MSDN的例子:
    USE AdventureWorks;
    GO
    -- Create tool table.
    CREATE TABLE dbo.Tool(
       ID INT IDENTITY NOT NULL PRIMARY KEY, 
       Name VARCHAR(40) NOT NULL
    )
    GO
    -- Inserting values into products table.
    INSERT INTO dbo.Tool(Name) VALUES ('Screwdriver')
    INSERT INTO dbo.Tool(Name) VALUES ('Hammer')
    INSERT INTO dbo.Tool(Name) VALUES ('Saw')
    INSERT INTO dbo.Tool(Name) VALUES ('Shovel')
    GO-- Create a gap in the identity values.
    DELETE dbo.Tool 
    WHERE Name = 'Saw'
    GOSELECT * 
    FROM dbo.Tool
    GO-- Try to insert an explicit ID value of 3;
    -- should return a warning.
    INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel')
    GO
    -- SET IDENTITY_INSERT to ON.
    SET IDENTITY_INSERT dbo.Tool ON
    GO-- Try to insert an explicit ID value of 3.
    INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel')
    GOSELECT * 
    FROM dbo.Tool
    GO
    -- Drop products table.
    DROP TABLE dbo.Tool
    GO
      

  14.   

    --关闭递增插入属性 
    SET IDENTITY_INSERT OFF 
    --打开递增插入属性 
    SET IDENTITY_INSERT ON 这两个是对所有表起作用的吧? 
      

  15.   

    当 IDENTITY_INSERT 设置为 OFF 时,不能向表 't1_temp' 中的标识列插入显式值。原来是必须这样写啊!学习了
      

  16.   

    可以这样弄,分三步
    --修改主键
    1.删除主键约束
    alter table [T_FWHDCLXX]  DROP CONSTRAINT PK_T_FWHDCLXX
    2.设置字段不允许为空
    alter table T_FWHDCLXX alter column DPH VC_DPH not null
    3.添加主键
    alter table T_FWHDCLXX add primary key(HDBH,DPH)