The tables
Ten tables in four groups: the catalogue, the shelves, the people and the lending.
dbo.BookOne row per title. The ISBN is unique; the publisher may be unknown.dbo.AuthorOne row per person who wrote a book.dbo.BookAuthorWhich authors wrote which book; its key is the pair.dbo.PublisherPublishers by name, each name once.dbo.BookCopyOne row per physical copy, with its barcode and shelf.dbo.MemberLibrary members, found by card number.dbo.StaffThe librarians who lend books.dbo.LoanA copy lent to a member: lent, due and returned dates.dbo.ReservationA member waiting for a title, not a particular copy.dbo.FineAt most one fine per late loan, and when it was paid.
How the tables relate
- A book has zero or more copies , and each copy is exactly one book:
BookIdis NOT NULL, so the line has nothing at the book’s end . - Books and authors are many-to-many:
dbo.BookAuthorholds one row per pair, keyed by both columns, a composite key. - A loan is one copy lent to one member by one member of staff.
- A book’s publisher is zero or one:
PublisherIdallows NULL, so the line has a ring alone at the publisher’s end . - A loan has zero or one fine: the fine’s primary key is the loan’s key, so a second fine for the same loan fails.
- A reservation names a book, not a copy: any copy that comes back can serve it.
Why it is built this way
Titles and copies are separate tables
The catalogue describes a book once; the shelves hold copies. A loan names the copy that left the building, a reservation the title the member wants.
A copy is out once at a time
A filtered unique index on dbo.Loan (BookCopyId) WHERE ReturnedOn IS NULL lets a copy have many past loans and one open loan. A second open loan fails with Msg 2601.
A loan is closed, never deleted
ReturnedOn stays NULL while the book is out. Overdue books are the open loans past their date:
SELECT l.LoanId, m.CardNumber, l.DueOn FROM dbo.Loan AS l JOIN dbo.Member AS m ON m.MemberId = l.MemberId WHERE l.ReturnedOn IS NULL AND l.DueOn < CAST(SYSDATETIME() AS date);
Members have a number and a card
MemberId is a surrogate key the other tables carry; the card number is an alternate key, so a lost card is one changed row.
The SQL Server script
-- Table [dbo].[Author] CREATE TABLE [dbo].[Author] ( [AuthorId] INT IDENTITY (1, 1) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, CONSTRAINT [PK_Author] PRIMARY KEY CLUSTERED ([AuthorId] ASC) ); GO -- Table [dbo].[Member] CREATE TABLE [dbo].[Member] ( [MemberId] INT IDENTITY (1, 1) NOT NULL, [CardNumber] VARCHAR (20) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, [Email] NVARCHAR (254) NULL, [JoinedOn] DATE NOT NULL, CONSTRAINT [PK_Member] PRIMARY KEY CLUSTERED ([MemberId] ASC), CONSTRAINT [AK_Member_CardNumber] UNIQUE NONCLUSTERED ([CardNumber] ASC) ); GO -- Table [dbo].[Publisher] CREATE TABLE [dbo].[Publisher] ( [PublisherId] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (200) NOT NULL, CONSTRAINT [PK_Publisher] PRIMARY KEY CLUSTERED ([PublisherId] ASC), CONSTRAINT [AK_Publisher_Name] UNIQUE NONCLUSTERED ([Name] ASC) ); GO -- Table [dbo].[Staff] CREATE TABLE [dbo].[Staff] ( [StaffId] INT IDENTITY (1, 1) NOT NULL, [FullName] NVARCHAR (200) NOT NULL, CONSTRAINT [PK_Staff] PRIMARY KEY CLUSTERED ([StaffId] ASC) ); GO -- Table [dbo].[Book] CREATE TABLE [dbo].[Book] ( [BookId] INT IDENTITY (1, 1) NOT NULL, [Isbn] CHAR (13) NOT NULL, [Title] NVARCHAR (300) NOT NULL, [PublisherId] INT NULL, [PublishedYear] SMALLINT NULL, CONSTRAINT [PK_Book] PRIMARY KEY CLUSTERED ([BookId] ASC), CONSTRAINT [FK_Book_Publisher_PublisherId] FOREIGN KEY ([PublisherId]) REFERENCES [dbo].[Publisher] ([PublisherId]), CONSTRAINT [AK_Book_Isbn] UNIQUE NONCLUSTERED ([Isbn] ASC) ); GO -- Table [dbo].[BookAuthor] CREATE TABLE [dbo].[BookAuthor] ( [BookId] INT NOT NULL, [AuthorId] INT NOT NULL, CONSTRAINT [PK_BookAuthor] PRIMARY KEY CLUSTERED ([BookId] ASC, [AuthorId] ASC), CONSTRAINT [FK_BookAuthor_Author_AuthorId] FOREIGN KEY ([AuthorId]) REFERENCES [dbo].[Author] ([AuthorId]), CONSTRAINT [FK_BookAuthor_Book_BookId] FOREIGN KEY ([BookId]) REFERENCES [dbo].[Book] ([BookId]) ); GO -- Table [dbo].[BookCopy] CREATE TABLE [dbo].[BookCopy] ( [BookCopyId] INT IDENTITY (1, 1) NOT NULL, [BookId] INT NOT NULL, [Barcode] VARCHAR (20) NOT NULL, [Shelf] VARCHAR (20) NOT NULL, CONSTRAINT [PK_BookCopy] PRIMARY KEY CLUSTERED ([BookCopyId] ASC), CONSTRAINT [FK_BookCopy_Book_BookId] FOREIGN KEY ([BookId]) REFERENCES [dbo].[Book] ([BookId]), CONSTRAINT [AK_BookCopy_Barcode] UNIQUE NONCLUSTERED ([Barcode] ASC) ); GO -- Table [dbo].[Reservation] CREATE TABLE [dbo].[Reservation] ( [ReservationId] INT IDENTITY (1, 1) NOT NULL, [BookId] INT NOT NULL, [MemberId] INT NOT NULL, [ReservedAt] DATETIME2 (0) NOT NULL, CONSTRAINT [PK_Reservation] PRIMARY KEY CLUSTERED ([ReservationId] ASC), CONSTRAINT [FK_Reservation_Book_BookId] FOREIGN KEY ([BookId]) REFERENCES [dbo].[Book] ([BookId]), CONSTRAINT [FK_Reservation_Member_MemberId] FOREIGN KEY ([MemberId]) REFERENCES [dbo].[Member] ([MemberId]) ); GO -- Table [dbo].[Loan] CREATE TABLE [dbo].[Loan] ( [LoanId] INT IDENTITY (1, 1) NOT NULL, [BookCopyId] INT NOT NULL, [MemberId] INT NOT NULL, [StaffId] INT NOT NULL, [LoanedOn] DATE NOT NULL, [DueOn] DATE NOT NULL, [ReturnedOn] DATE NULL, CONSTRAINT [PK_Loan] PRIMARY KEY CLUSTERED ([LoanId] ASC), CONSTRAINT [CK_Loan_DueOn] CHECK (DueOn >= LoanedOn), CONSTRAINT [FK_Loan_BookCopy_BookCopyId] FOREIGN KEY ([BookCopyId]) REFERENCES [dbo].[BookCopy] ([BookCopyId]), CONSTRAINT [FK_Loan_Member_MemberId] FOREIGN KEY ([MemberId]) REFERENCES [dbo].[Member] ([MemberId]), CONSTRAINT [FK_Loan_Staff_StaffId] FOREIGN KEY ([StaffId]) REFERENCES [dbo].[Staff] ([StaffId]) ); GO CREATE UNIQUE NONCLUSTERED INDEX [UX_Loan_BookCopyId_Open] ON [dbo].[Loan]([BookCopyId] ASC) WHERE (ReturnedOn IS NULL); GO -- Table [dbo].[Fine] CREATE TABLE [dbo].[Fine] ( [LoanId] INT NOT NULL, [Amount] DECIMAL (8, 2) NOT NULL, [PaidOn] DATE NULL, CONSTRAINT [PK_Fine] PRIMARY KEY CLUSTERED ([LoanId] ASC), CONSTRAINT [CK_Fine_Amount] CHECK (Amount > 0), CONSTRAINT [FK_Fine_Loan_LoanId] FOREIGN KEY ([LoanId]) REFERENCES [dbo].[Loan] ([LoanId]) ); GO
Questions
What tables does a library management system need?
Books, their physical copies, authors, members and loans at the least. Reservations, fines, publishers and staff come next. This design has all ten.
How do I find overdue books?
Select the open loans whose due date has passed: ReturnedOn is NULL and DueOn is before today. The full query is under "Why it is built this way".
Can I change the design?
Yes. Open it in ERDsmith, free in the browser: add a table, rename a column, and the keys and lines follow.
Does it work with MySQL or PostgreSQL?
The script is written for SQL Server. The tables, keys and relationships carry over to other databases; the types and the filtered index need that database's own syntax.
