Question

I am creating a library management system.

In my program, each user can borrow some books, then i should save that books information( as book name, book isbn , borrow date) into a table in my database.

Each user in my system has a primary key(ID), I want to use this approach:

When a user want to borrow books, that books information should save into a table in my database, But now, the ID number in my userBooksTable each row is duplicate.

Is there any better solution?

Was it helpful?

Solution

Your table layout might look similar to this:

books
-----
id (pk)
isbn
name

user
----
id (pk)
name
...

borrow
------
userId (pk, fk)
bookId (pk, fk)
date (pk)

So the main table would be borrow which contains the data which user borrowed which book at which date. All columns would be part of the primary key, since a user could borrow one book multiple times at different dates (provided it is returned in between ;) ).

You might need more information (e.g. if the user borrowed multiple copies at once or when the book was returned etc.), but I'll leave that for you.

Alternatively, you could use the isbn as the book's id, depending on whether it is unique or not, e.g. you could have a bookinfo table containing isbn, name etc. on the book and a books table containing data on one concrete copy of that book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top