设计一个数据库应用系统:图书馆借还书系统。设计支撑这个应用的多个数据表,例如人员信息、图书信息、借还信息、收费信息等等。图书馆借还书系统:图书信息表支持购入同一个ISBN的书籍多本,支持按ISBN、图书名、出版社、作者、图书分类、出版年份进行书籍登记,可查询和删除(这是基本界面功能) 支持对内对外借阅;内部人员包括学生、学校职工;外部人员需登记身份证号、姓名等,需要押金;外部人员在借书数量限制2本;内部限制为5本;超期外部人员3元/天,内部1元/天,有超期的书籍,不让继续借新的。给出具体的sql和python源码
时间: 2023-06-10 17:07:09 浏览: 206
首先,我们需要设计以下数据表:
1. 人员信息表(person_info):包含人员ID、姓名、身份证号、职业等字段。
2. 图书信息表(book_info):包含书籍ID、书名、ISBN、作者、出版社、出版年份、图书分类、价格等字段。
3. 借还信息表(borrow_info):包含借阅ID、人员ID、书籍ID、借阅时间、应还时间、实际归还时间、借阅状态等字段。
4. 收费信息表(fee_info):包含收费ID、借阅ID、超期天数、应收费用、实际收费等字段。
接下来,我们可以使用以下 SQL 语句创建这些表:
```
-- 创建人员信息表
CREATE TABLE person_info (
person_id INT PRIMARY KEY,
name VARCHAR(50),
id_card VARCHAR(50),
profession VARCHAR(50)
);
-- 创建图书信息表
CREATE TABLE book_info (
book_id INT PRIMARY KEY,
book_name VARCHAR(50),
isbn VARCHAR(50),
author VARCHAR(50),
publisher VARCHAR(50),
publish_year INT,
category VARCHAR(50),
price FLOAT
);
-- 创建借还信息表
CREATE TABLE borrow_info (
borrow_id INT PRIMARY KEY,
person_id INT,
book_id INT,
borrow_time DATETIME,
due_time DATETIME,
return_time DATETIME,
status INT,
FOREIGN KEY (person_id) REFERENCES person_info(person_id),
FOREIGN KEY (book_id) REFERENCES book_info(book_id)
);
-- 创建收费信息表
CREATE TABLE fee_info (
fee_id INT PRIMARY KEY,
borrow_id INT,
overdue_days INT,
due_fee FLOAT,
actual_fee FLOAT,
FOREIGN KEY (borrow_id) REFERENCES borrow_info(borrow_id)
);
```
接下来,我们可以使用 Python 编写一个简单的图书馆借还书系统。以下是一个基本的示例:
```python
import sqlite3
from datetime import datetime, timedelta
# 连接到数据库
conn = sqlite3.connect('library.db')
# 获取游标
c = conn.cursor()
# 插入人员信息
c.execute("INSERT INTO person_info VALUES (1, '张三', '1234567890', '学生')")
c.execute("INSERT INTO person_info VALUES (2, '李四', '0987654321', '教师')")
# 插入图书信息
c.execute("INSERT INTO book_info VALUES (1, 'Python编程入门', '9787115461360', '乔伊斯', '人民邮电出版社', 2020, '计算机', 39.8)")
c.execute("INSERT INTO book_info VALUES (2, '深入浅出MySQL', '9787121317996', '周楷文', '电子工业出版社', 2019, '数据库', 49.8)")
c.execute("INSERT INTO book_info VALUES (3, 'Java核心技术', '9787111596669', 'Cay S. Horstmann', '机械工业出版社', 2019, '计算机', 119.0)")
# 提交更改
conn.commit()
# 从图书信息表中查询书籍信息
def search_book(query):
c.execute("SELECT * FROM book_info WHERE book_name LIKE ? OR isbn LIKE ? OR author LIKE ? OR publisher LIKE ? OR category LIKE ? OR publish_year = ?",
('%'+query+'%', '%'+query+'%', '%'+query+'%', '%'+query+'%', '%'+query+'%', query))
books = c.fetchall()
return books
# 借书
def borrow_book(person_id, book_id):
# 查询人员信息
c.execute("SELECT * FROM person_info WHERE person_id = ?", (person_id,))
person = c.fetchone()
# 查询书籍信息
c.execute("SELECT * FROM book_info WHERE book_id = ?", (book_id,))
book = c.fetchone()
# 查询借阅信息
c.execute("SELECT * FROM borrow_info WHERE person_id = ? AND book_id = ? AND status = 0", (person_id, book_id))
borrow = c.fetchone()
if person and book and not borrow:
# 判断借书数量限制
c.execute("SELECT COUNT(*) FROM borrow_info WHERE person_id = ? AND status = 0", (person_id,))
count = c.fetchone()[0]
if person[3] == '外部人员' and count >= 2:
return '外部人员最多只能借阅2本书籍'
elif person[3] == '内部人员' and count >= 5:
return '内部人员最多只能借阅5本书籍'
# 更新图书信息
c.execute("UPDATE book_info SET stock = stock - 1 WHERE book_id = ?", (book_id,))
# 插入借阅信息
borrow_time = datetime.now()
due_time = borrow_time + timedelta(days=30)
c.execute("INSERT INTO borrow_info (person_id, book_id, borrow_time, due_time, status) VALUES (?, ?, ?, ?, 0)",
(person_id, book_id, borrow_time, due_time))
conn.commit()
return '借阅成功'
elif borrow:
return '该图书已经被借阅'
else:
return '借阅失败'
# 还书
def return_book(person_id, book_id):
# 查询借阅信息
c.execute("SELECT * FROM borrow_info WHERE person_id = ? AND book_id = ? AND status = 0", (person_id, book_id))
borrow = c.fetchone()
if borrow:
# 更新图书信息
c.execute("UPDATE book_info SET stock = stock + 1 WHERE book_id = ?", (book_id,))
# 更新借阅信息
return_time = datetime.now()
c.execute("UPDATE borrow_info SET return_time = ?, status = 1 WHERE borrow_id = ?", (return_time, borrow[0]))
# 计算超期天数和费用
due_time = borrow[4]
overdue_days = (return_time - due_time).days
if overdue_days > 0:
due_fee = overdue_days * 3 if borrow[2] == '外部人员' else overdue_days
c.execute("INSERT INTO fee_info (borrow_id, overdue_days, due_fee, actual_fee) VALUES (?, ?, ?, 0)",
(borrow[0], overdue_days, due_fee))
conn.commit()
return '还书成功'
else:
return '还书失败'
# 关闭数据库连接
conn.close()
```
这个示例提供了几个基本功能,包括查询图书信息、借书、还书等。你可以根据实际需求进行修改和扩展。
阅读全文