这是一个图书馆管理系统。
我有两张表,一张是Books
带字段的BookId, Title, AuthorId, PublisherId, ISBN, ISBN13, PublishedOn, NumberOfPages, etc etc.
其他是BookTransactions
包含向任何学生发行或从任何学生接收的书籍的所有交易,具有字段TransactionId, BookId, IssuedOn, IssuedTo, ReceviedOn, etc. etc.
现在的问题是,假设我有这本书The God Delusion
,并且有 200 份。表中的条目Books
将是这样的
7 The God Delusion 21 32 0618680004 978-0618680009 .....
8 The God Delusion 21 32 0618680004 978-0618680009 .....
9 The God Delusion 21 32 0618680004 978-0618680009 .....
10 The God Delusion 21 32 0618680004 978-0618680009 .....
.
.
.
(200 行)。其他一切都完全相同,只是每本书有 200 个条目,现在我知道我可以有一个名为 的列Quantity
,但是听到这个(这就是问题的原因)
一段时间后,表格BookTransactions
可能看起来像这样
1 9 4/3/2012 ABC __
2 10 4/3/2012 PQR __
3 7 4/3/2012 XYZ 7/3/2012
从而清楚地表明3本《神幻》书是在同一天发行的。好处是我可以检查是否发行了第 7 本书,如果没有,那么我可以发行它,像这样
sqlCommand.CommandText = "SELECT Count(*) FROM BookTransactions WHERE BookId=7 AND ReceivedOn is NULL";
// if a book is received, its in library and can be issued again
if (Int32.Parse(sqlCommand.ExecuteScalar().ToString()) == 0)
bookInstance.IssueThisBook();
else
MessageBox.Show("This book is already issued to someone, please select a different book");
现在有一个列数量的问题是说如果Books
表看起来像这样
7 The God Delusion 21 32 0618680004 978-0618680009 ..... 200
// the last one indicates quantity
现在的条目BookTransaction
将如下所示
1 7 4/3/2012 ABC __
现在如果运行这段代码,
sqlCommand.CommandText = "SELECT Count(*) FROM BookTransactions WHERE BookId=7 AND ReceivedOn is NULL";
// if a book is received, its in library and can be issued again
if (Int32.Parse(sqlCommand.ExecuteScalar().ToString()) == 0)
bookInstance.IssueThisBook();
else
MessageBox.Show("This book is already issued to someone, please select a different book");
它会显示这本书是发给某人的,即使只发行了一本,图书馆里还剩下 199 份。
我知道有很多方法可以克服这个问题,我能想到的一种方法是做这样的事情
sqlCommand.CommandText = "SELECT Count(*) FROM BookTransactions WHERE BookId=7 AND ReceivedOn is NULL";
sqlCommandOther.CommandText = "SELECT Quantity FROM Books WHERE Bookid=7"
// if the count is not equal to total quantity then there are some books availabe in libray
if (Int32.Parse(sqlCommand.ExecuteScalar().ToString()) != Int32.Parse(sqlCommandOther.ExecuteScalar().ToString()))
bookInstance.IssueThisBook();
else
// all books are currently issued
MessageBox.Show("All copies of this book are already issued, please select a different book");
好吧,这是我能想到的唯一方法,但我知道这效率不高,我想知道是否有另一种方法可以消除仅因为有 200 个副本而在书中有 200 行的冗余?(我的意思是除了我在上一段中描述的任何其他方式。)实现我想要做的事情的最佳方式是什么。
我希望我说清楚了。
编辑StarPilot 的观点值得注意。我真的需要 200 条不同的行/记录吗?因为就像他指出的那样,如果某本书(例如 #50)丢失或损坏,我将如何追踪?考虑到这一点,它看起来确实必须有冗余,对吧?