用sql系统编写一个图书借阅管理系统 ⑴功能要求 ①实现图书信息、类别、出版社等信息的管理; ②实现读者信息、借阅信息的管理; ③实现图书的借阅、续借、归还管理; ④实现超期罚款管理、收款管理; ⑵数据要求 ①创建视图实现查询功能; ②创建触发器,实现对基本表进行插入、修改或删除操作会使相应触发器运行,以保证操作不会破坏数据的完整性; ③创建存储过程; ④实现查询操作,指定查询统读者的借书量; ⑤建立数据库相关表之间的参照完整性约束; ⑶具体要求 ①图书信息管理:图书编号、书名、图书类型、作者、出版社、价格 ②图书类型:图书分类号、图书分类名称、描述信息 ③读者信息管理:读者证号、姓名、证件状态、联系方式、系部、专业班级 ④借阅管理:借书日期、应还日期、归还日期、罚款金额
时间: 2023-09-14 18:08:26 浏览: 262
很高兴为您回答这个问题。以下是一个简单的 SQL 代码示例,用于创建一个基本的图书借阅管理系统:
创建图书信息表:
CREATE TABLE book_info (
book_id INT AUTO_INCREMENT PRIMARY KEY,
book_name VARCHAR(50) NOT NULL,
book_type VARCHAR(50) NOT NULL,
author VARCHAR(50) NOT NULL,
publisher VARCHAR(50) NOT NULL,
price DECIMAL(5,2) NOT NULL
);
创建图书类型表:
CREATE TABLE book_type (
type_id INT AUTO_INCREMENT PRIMARY KEY,
type_name VARCHAR(50) NOT NULL,
type_desc VARCHAR(255) NOT NULL
);
创建读者信息表:
CREATE TABLE reader_info (
reader_id INT AUTO_INCREMENT PRIMARY KEY,
reader_name VARCHAR(50) NOT NULL,
id_status VARCHAR(50) NOT NULL,
contact VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
major_class VARCHAR(50) NOT NULL
);
创建借阅信息表:
CREATE TABLE borrow_info (
borrow_id INT AUTO_INCREMENT PRIMARY KEY,
book_id INT NOT NULL,
reader_id INT NOT NULL,
borrow_date DATETIME NOT NULL,
return_date DATETIME NOT NULL,
actual_return_date DATETIME,
fine DECIMAL(5,2),
FOREIGN KEY (book_id) REFERENCES book_info(book_id),
FOREIGN KEY (reader_id) REFERENCES reader_info(reader_id)
);
创建视图:
CREATE VIEW reader_borrow_count AS
SELECT reader_id, COUNT(*) AS borrow_count
FROM borrow_info
GROUP BY reader_id;
创建触发器:
CREATE TRIGGER check_borrow_date
BEFORE INSERT ON borrow_info
FOR EACH ROW
BEGIN
IF NEW.borrow_date > NEW.return_date THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Borrow date cannot be later than return date!';
END IF;
END;
创建存储过程:
CREATE PROCEDURE calculate_fine(IN borrow_id INT)
BEGIN
DECLARE fine_amount DECIMAL(5,2);
DECLARE days_late INT;
SELECT DATEDIFF(actual_return_date, return_date) INTO days_late FROM borrow_info WHERE borrow_id = borrow_id;
IF days_late > 0 THEN
SET fine_amount = days_late * 0.5;
UPDATE borrow_info SET fine = fine_amount WHERE borrow_id = borrow_id;
END IF;
END;
查询操作:
SELECT reader_name, COUNT(*) AS borrow_count
FROM reader_info
INNER JOIN borrow_info ON reader_info.reader_id = borrow_info.reader_id
GROUP BY reader_name;
添加参照完整性约束:
ALTER TABLE borrow_info
ADD CONSTRAINT fk_book_id
FOREIGN KEY (book_id) REFERENCES book_info(book_id)
ON DELETE CASCADE;
ALTER TABLE borrow_info
ADD CONSTRAINT fk_reader_id
FOREIGN KEY (reader_id) REFERENCES reader_info(reader_id)
ON DELETE CASCADE;
希望这个示例对您有所帮助!
阅读全文