现有投票表Votes,投票条目表VoteItems。
在VoteItems里,VoteId作为主键的一部分,另一部分是“自增”的ItemId。
这该怎么办?MSSQL自增列主键联合主键

解决方案 »

  1.   

    if object_id('tempdb..#t1') is not null                              
    drop table #t1
    create table #t1
    (
    ItemId int  identity(0,1),
    a varchar(20),
    b varchar(30)
    )   
    insert into #t1(a,b)
    select 'a','b'
    union all
    select 'aa','bb'
    union all
    select 'aaa','bbb'
    union all
    select 'aaaa','bbbb'
    if object_id('tempdb..#t2') is not null                              
    drop table #t2
    create table #t2
    (
    ItemId int,
    Id int  identity(1,1),
    a varchar(20),
    b varchar(30)
    )      
    insert into #t2(ItemId,a,b)
    select *  from #t1
    select * from #t2
      

  2.   

    2L哪管你写一个primary key呀……
    Votes与VoteItems是一对多的,有人会么?
      

  3.   

    多字段的组合定义为主键前提条件:1、字段不接受null值
                                    2、多个字段组合的值不能重复,单个字段内的数据允许重复定义主键方法:按住ctrl键,选择你想选的字段,右键设为主键即可