建立超期未归还书籍的视图,显示图书编号和名称,以及借书人姓名和电话。
时间: 2024-04-30 10:25:09 浏览: 234
假设我们有以下的数据库表格:
- `books`:包含图书编号和名称等信息
- `borrowers`:包含借书人信息,包括姓名和电话等
- `borrowed_books`:记录借书信息,包括借书日期和归还日期等
我们可以通过以下的 SQL 语句来创建视图:
```sql
CREATE VIEW overdue_books AS
SELECT books.book_id, books.book_name, borrowers.borrower_name, borrowers.borrower_phone
FROM books
INNER JOIN borrowed_books ON books.book_id = borrowed_books.book_id
INNER JOIN borrowers ON borrowed_books.borrower_id = borrowers.borrower_id
WHERE borrowed_books.return_date IS NULL AND borrowed_books.due_date < CURDATE();
```
这个视图会返回所有超期未归还的书籍信息,包括图书编号和名称,以及借书人的姓名和电话。我们使用了 `INNER JOIN` 连接了三个表格,并使用了 `WHERE` 子句过滤了超期未归还的书籍。
相关问题
用sql系统编写一个图书借阅管理系统 ⑴功能要求 ①实现图书信息、类别、出版社等信息的管理; ②实现读者信息、借阅信息的管理; ③实现图书的借阅、续借、归还管理; ④实现超期罚款管理、收款管理; ⑵数据要求 ①创建视图实现查询功能; ②创建触发器,实现对基本表进行插入、修改或删除操作会使相应触发器运行,以保证操作不会破坏数据的完整性; ③创建存储过程; ④实现查询操作,指定查询统读者的借书量; ⑤建立数据库相关表之间的参照完整性约束; ⑶具体要求 ①图书信息管理:图书编号、书名、图书类型、作者、出版社、价格 ②图书类型:图书分类号、图书分类名称、描述信息 ③读者信息管理:读者证号、姓名、证件状态、联系方式、系部、专业班级 ④借阅管理:借书日期、应还日期、归还日期、罚款金额
很好,下面是一个简单的图书借阅管理系统的 SQL 实现:
首先创建基本表:
```
CREATE TABLE book (
book_id INT PRIMARY KEY,
title VARCHAR(255),
type_id INT,
author VARCHAR(255),
publisher VARCHAR(255),
price DECIMAL(10, 2)
);
CREATE TABLE book_type (
type_id INT PRIMARY KEY,
type_name VARCHAR(255),
description VARCHAR(255)
);
CREATE TABLE reader (
reader_id INT PRIMARY KEY,
name VARCHAR(255),
id_status VARCHAR(255),
contact VARCHAR(255),
department VARCHAR(255),
major_class VARCHAR(255)
);
CREATE TABLE borrowing (
borrowing_id INT PRIMARY KEY,
reader_id INT,
book_id INT,
borrow_date DATE,
due_date DATE,
return_date DATE,
fine DECIMAL(10, 2),
FOREIGN KEY (reader_id) REFERENCES reader(reader_id),
FOREIGN KEY (book_id) REFERENCES book(book_id)
);
```
然后创建视图:
```
CREATE VIEW book_info AS
SELECT book.book_id, book.title, book_type.type_name, book.author, book.publisher, book.price
FROM book
JOIN book_type ON book.type_id = book_type.type_id;
CREATE VIEW reader_borrowing AS
SELECT reader.reader_id, reader.name, COUNT(borrowing.borrowing_id) AS borrowing_count
FROM reader
LEFT JOIN borrowing ON reader.reader_id = borrowing.reader_id
GROUP BY reader.reader_id, reader.name;
```
接下来创建触发器:
```
CREATE TRIGGER check_borrowing_insert
BEFORE INSERT ON borrowing
FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM borrowing WHERE reader_id = NEW.reader_id AND return_date IS NULL) >= 5 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This reader has borrowed too many books';
END IF;
END;
CREATE TRIGGER check_borrowing_update
BEFORE UPDATE ON borrowing
FOR EACH ROW
BEGIN
IF NEW.return_date IS NOT NULL AND NEW.return_date > OLD.due_date THEN
SET NEW.fine = (NEW.return_date - OLD.due_date) * 0.1;
END IF;
END;
CREATE TRIGGER check_borrowing_delete
BEFORE DELETE ON borrowing
FOR EACH ROW
BEGIN
IF OLD.return_date IS NULL AND OLD.due_date < CURDATE() THEN
SET OLD.fine = (CURDATE() - OLD.due_date) * 0.1;
END IF;
END;
```
然后创建存储过程:
```
CREATE PROCEDURE borrow_book (IN p_reader_id INT, IN p_book_id INT, IN p_borrow_date DATE, IN p_due_date DATE)
BEGIN
INSERT INTO borrowing (reader_id, book_id, borrow_date, due_date) VALUES (p_reader_id, p_book_id, p_borrow_date, p_due_date);
END;
CREATE PROCEDURE return_book (IN p_borrowing_id INT, IN p_return_date DATE)
BEGIN
UPDATE borrowing SET return_date = p_return_date WHERE borrowing_id = p_borrowing_id;
END;
```
最后建立参照完整性约束:
```
ALTER TABLE book ADD CONSTRAINT fk_book_type FOREIGN KEY (type_id) REFERENCES book_type(type_id);
ALTER TABLE borrowing ADD CONSTRAINT fk_borrowing_reader FOREIGN KEY (reader_id) REFERENCES reader(reader_id);
ALTER TABLE borrowing ADD CONSTRAINT fk_borrowing_book FOREIGN KEY (book_id) REFERENCES book(book_id);
```
帮我用sql写一个学校图书借阅管理系统 要求是学校图书借阅管理系统 (1)功能要求 ①实现图书信息、类别、出版社等信息的管理; ②实现读者信息、借阅信息的管理; ③实现图书的借阅、续借、归还管理; ④实现超期罚款管理、收款管理; (2)数据要求 ①创建触发器,分别实现借书和还书时自动更新图书信息的在册数量; ②创建视图查询各种图书的书号、书名、总数和在册数; ③创建存储过程查询指定读者借阅图书的情况; ④建立数据库相关表之间的参照完整性约束
好的,下面是一个简单的学校图书借阅管理系统的 SQL 实现,包括创建表、触发器、视图和存储过程:
创建图书信息表:
```sql
CREATE TABLE books (
id INT PRIMARY KEY,
name VARCHAR(100),
category VARCHAR(50),
publisher VARCHAR(50),
total INT,
stock INT
);
```
创建读者信息表:
```sql
CREATE TABLE readers (
id INT PRIMARY KEY,
name VARCHAR(50),
gender CHAR(1),
phone VARCHAR(20),
email VARCHAR(50)
);
```
创建借阅信息表:
```sql
CREATE TABLE borrowings (
id INT PRIMARY KEY,
book_id INT,
reader_id INT,
borrow_date DATE,
return_date DATE,
overdue_fine DECIMAL(10,2),
payment_status CHAR(1),
FOREIGN KEY (book_id) REFERENCES books(id),
FOREIGN KEY (reader_id) REFERENCES readers(id)
);
```
创建触发器,实现借书和还书时自动更新图书信息的在册数量:
```sql
CREATE TRIGGER update_stock_on_borrow
AFTER INSERT ON borrowings
FOR EACH ROW
UPDATE books SET stock = stock - 1 WHERE id = NEW.book_id;
CREATE TRIGGER update_stock_on_return
AFTER UPDATE ON borrowings
FOR EACH ROW
UPDATE books SET stock = stock + 1 WHERE id = NEW.book_id;
```
创建视图,查询各种图书的书号、书名、总数和在册数:
```sql
CREATE VIEW book_info AS
SELECT id, name, category, publisher, total, stock FROM books;
```
创建存储过程,查询指定读者借阅图书的情况:
```sql
CREATE PROCEDURE reader_borrowings (IN reader_id INT)
BEGIN
SELECT b.id, b.name, b.borrow_date, b.return_date, b.overdue_fine, b.payment_status
FROM borrowings AS b
WHERE b.reader_id = reader_id;
END;
```
创建表之间的参照完整性约束:
```sql
ALTER TABLE borrowings ADD CONSTRAINT fk_book_id FOREIGN KEY (book_id) REFERENCES books(id);
ALTER TABLE borrowings ADD CONSTRAINT fk_reader_id FOREIGN KEY (reader_id) REFERENCES readers(id);
```
这是一个简单的示例,实际的学校图书借阅管理系统可能需要更多的表和功能。
阅读全文