1

我有 3 张桌子:

create table book (book_id int not null primary key, 
                   book_name char (100) unique) 

create table author (author_id int not null primary key,
                     authorname char (100) unique) 

create table bookauthor (book_id int, 
                         author_id int, 
                         CONSTRAINT pk_book_id PRIMARY KEY (book_id,author_id)) 

我想设置

  • book.book_id作为 fk 到bookauthor.book_id
  • author.author_id作为 fk 到bookauthor.author_id

请记住 pk inbookauthor已打开book_id,author_id

4

1 回答 1

1

将您的 bookauther 表更改为

create table bookauther (
    book_id int , 
    auther_id int, 
    CONSTRAINT pk_book_id PRIMARY KEY (book_id,auther_id),
    FOREIGN KEY (book_id) REFERENCES book(book_id),
    FOREIGN KEY (auther_id) REFERENCES auther(auther_id)

)

看看FOREIGN KEY ConstraintSQL FOREIGN KEY Constraint

于 2013-08-13T05:40:42.223 回答