I have a simple question related to database schema, how can I check whether a student has borrowed a particular book, let say I have a table Borrowtable and I have Student_id,Book_id…. as attributes of that table, what else i should add in order to keep track whether the student_id has borrowed a book ???
I am thinkin to add another field like Status as INT field and values like 1 for borrowed and 0 not borrow. Is this good idea or there is another way of doing this?
Thankx
Lulzim.
Edited for two scenarios
Scenario 1: not storing a borrowing history:
This applies if you have no need to keep track of the user’s borrowing history. Borrowed books are one-off events, and are not remembered.
If you already have a
Borrowtablecontaining a map betweenStudent_idandBook_id, there is no need for another column to indicate borrowed or not borrowed.Instead, you can simply rely on the presence of the row containing the student and book. If the row exists, the book has been
borrowed. If the row has been deleted or does not exist, the book has not been borrowed.
To test if a book is available for borrowing by any user, simply check if the
Book_idexists anywhere in the table. If it does, the book is unavailable.Scenario 2: store a user’s borrowing history:
If you need to keep track of what a user has borrowed, add two more columns, each of
DATETIMEtype to track the borrowed date and the returned date for the book.To test if a book is available for borrowing, use a query like:
If any row returns, then the book is currently borrowed and unavailable for use.