我有一个id字段  已经设置了 identity 但我现在需要给该字段 添加主健用sql语句该如何写

解决方案 »

  1.   

    alter table tb
    add constraint PK_id primary key(id)
      

  2.   

    我没懂你的identity,不知道你想要的是不是这个
    column    type     primary key [,...]
    ------------------------
    constrain column_PK
    primary key (column,..l)
      

  3.   

    设置默认值alter table <表名> add <列名> <默认值类型>  default '<默认值>'
    设置主键alter table <表名> add constraint <主键名> primary key(<列名>)
    设置联合主键alter table <表名> add constraint <主键名> primary key(<列名1>,<列名2>,...,<列名N>)
    约束alter table <表名> add constraint <约束名> check (<约束语句>)在输入语句的时候<>是不需要的 
      

  4.   

    不要add啊 我只想修改   也就是给id字段加个 primarykey上去 不能重新添加的
      

  5.   

    alter table #tb
    add constraint id_PK primary key(id);
      

  6.   

    --创建测试表test_tb
    create table test_tb(id int identity,col int)
    --定义test_tb表的ID字段为主键
    alter table test_tb add primary key(id)
    --查询test_tb表有哪些约束
    sp_helpconstraint test_tb
    /*
    Object Name
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    test_tb 
    constraint_type                                                                                                                                    constraint_name                                                                                                                  delete_action update_action status_enabled status_for_replication constraint_keys
    -------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------------- ------------- -------------- ---------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    PRIMARY KEY (clustered)                                                                                                                            PK__test_tb__3213E83F20C1E124                                                                                                    (n/a)         (n/a)         (n/a)          (n/a)                  id
    */