create table User_Book( 
  UserID int not null, 
  BookID int not null, 
  BookAlias varchar(10) not null, 
  foreign key(UserID) references User_info(ID), 
  foreign key(BookID) references Book_info(ID), 
  primary key(/*怎么填*/) 
); 一个用户对于同一本书只能加一次,一个用户不能让两个不同的书取相同的别书。 即 
   insert into User_Book values('1','1','xx'); 
  insert into User_Book values('1','1','yy'); 
不行    insert into User_Book values('1','1','xx'); 
  insert into User_Book values('1','2','xx'); 
不行 insert into User_Book values('1','1','xx'); 
insert into User_Book values('2','1','xx'); 
可以 primary key(UserID,DvrID); 
primary key(UserID,BookAlias) 
就行,但不能这样定义两个主键! 请教大家!

解决方案 »

  1.   

    create table User_Book( 
    UserID int not null, 
    BookID int not null, 
    BookAlias varchar(10) not null, 
    foreign key(UserID) references User_info(ID), 
    foreign key(BookID) references Book_info(ID), 
    primary key(UserID,BookID) 
    ); 
      

  2.   

    上面贴错了,要把UNIQUE也加上。
    create table User_Book( 
    UserID int not null, 
    BookID int not null, 
    BookAlias varchar(10) not null, 
    foreign key(UserID) references User_info(ID), 
    foreign key(BookID) references Book_info(ID), 
    primary key(UserID,BookID),
    UNIQUE key (UserID,BookAlias)
    ); 
      

  3.   

    好强啊!ACMAIN_CHM !谢谢!!