想在一个表的两个属性上建主码,如:create table 班干部
( 职位 char(8) not null primary key,
  就职时间 smalldatetime not null,
  离职时间 smalldatetime,
  学号 char(8),
  primary key(职位,就职时间)
)提示错误:无法向表 '班干部' 中添加多个 PRIMARY KEY 约束不是可以在一个属性组上创建主码吗?

解决方案 »

  1.   

    create table 班干部
    ( 职位 char(8) not null primary key,
      就职时间 smalldatetime not null,
      离职时间 smalldatetime,
      学号 char(8),
      unique(职位,就职时间)  -- 如果是为了保障唯一性, 可以用唯一约束
    )
      

  2.   

    --这样?联合主建?
    create table 班干部
    ( 职位 char(8) not null,
      就职时间 smalldatetime not null,
      离职时间 smalldatetime,
      学号 char(8),
      CONSTRAINT PK_1 PRIMARY KEY(职位,就职时间)
    )