SQL语句实现
时间: 2023-07-07 18:17:25 浏览: 85
好的,下面是一个简单的图书管理系统的MySQL数据库设计,包含了书籍信息表、借阅记录表和用户信息表。您可以根据这个设计来编写相应的SQL语句实现。
```sql
CREATE TABLE book_info (
book_id INT PRIMARY KEY AUTO_INCREMENT,
book_name VARCHAR(50) NOT NULL,
author VARCHAR(50) NOT NULL,
press VARCHAR(50) NOT NULL,
publish_date DATE NOT NULL,
price FLOAT NOT NULL,
total_amount INT NOT NULL,
available_amount INT NOT NULL
);
CREATE TABLE borrow_record (
record_id INT PRIMARY KEY AUTO_INCREMENT,
book_id INT NOT NULL,
user_id INT NOT NULL,
borrow_date DATE NOT NULL,
return_date DATE NOT NULL,
FOREIGN KEY (book_id) REFERENCES book_info(book_id),
FOREIGN KEY (user_id) REFERENCES user_info(user_id)
);
CREATE TABLE user_info (
user_id INT PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(50) NOT NULL,
user_type ENUM('student', 'teacher', 'staff') NOT NULL,
email VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL,
password VARCHAR(50) NOT NULL
);
```
下面是一些常用的SQL语句实现,供您参考:
1. 查询所有书籍信息
```sql
SELECT * FROM book_info;
```
2. 查询可借阅的书籍信息
```sql
SELECT * FROM book_info WHERE available_amount > 0;
```
3. 根据书名或作者名查询书籍信息
```sql
SELECT * FROM book_info WHERE book_name LIKE '%关键词%' OR author LIKE '%关键词%';
```
4. 借阅书籍
```sql
UPDATE book_info SET available_amount = available_amount - 1 WHERE book_id = '要借阅的书籍ID';
INSERT INTO borrow_record (book_id, user_id, borrow_date, return_date) VALUES ('要借阅的书籍ID', '借阅者ID', '借阅日期', '应还日期');
```
5. 归还书籍
```sql
UPDATE book_info SET available_amount = available_amount + 1 WHERE book_id = '要归还的书籍ID';
UPDATE borrow_record SET return_date = '归还日期' WHERE book_id = '要归还的书籍ID' AND user_id = '借阅者ID' AND return_date = '应还日期';
```
6. 查询借阅记录
```sql
SELECT borrow_record.*, book_info.book_name, user_info.user_name
FROM borrow_record
INNER JOIN book_info ON borrow_record.book_id = book_info.book_id
INNER JOIN user_info ON borrow_record.user_id = user_info.user_id;
```
以上是一些基本的SQL语句实现,您可以根据实际情况进行修改和补充。
阅读全文