Search code examples
databaseoracleforeign-keysconstraintsconcatenation

How to concat three columns on a foreign key?


I've got 3 entities. Books, Students and "Borrow" (I don't know if this have any sense in english).

On Books, I need to insert a column that contains an id from borrow, the date and the student who take these book. So, this column is a foreign key, but, how can I concat these three columns in one? is that possible?

I think something like that, but I know it's not correct:

LOCALIZADOR     VARCHAR2(?),
CONSTRAINT  “PK_BOOK” PRIMARY KEY(ISBN),
CONSTRAINT “FK_BOOK.CODEDATE”   FOREIGN KEY(LOCALIZADOR CODIGO  DATE_BORROW)               
                                    REFERENCES(BORROW, STUDENT, BORROW)
                                    ON DELETE CASCADE

Solution

  • You need a foreign key to establish a relationship usually between one table and (usually) the primary key in another table. So it sounds like you'll want one foreign key from LOCALIZADOR to BOOKS to make sure it's a valid book id and another to STUDENTS to make sure it's a valid student id. I don't think you'd have the borrowed date as part of the foreign key, since it's not part of a different table.

    You only showed us part of one table, so I'm not sure what the others look like. I think you want something like:

    constraint fk_book foreign key ( book_id )
        references books.book_id
    constraint fk_student foreign key ( student_id )
        references students.student_id