数据库实现
时间: 2023-08-17 15:16:23 浏览: 91
数据库的实现
好的,下面是一个简单的图书管理系统的MySQL数据库实现,包含了书籍信息表、借阅记录表和用户信息表。您可以根据这个实现来创建相应的数据库。
1. 首先创建一个名为 `book_management` 的数据库:
```sql
CREATE DATABASE book_management;
```
2. 切换到 `book_management` 数据库:
```sql
USE book_management;
```
3. 创建书籍信息表 `book_info`:
```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
);
```
4. 创建借阅记录表 `borrow_record`:
```sql
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)
);
```
5. 创建用户信息表 `user_info`:
```sql
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
);
```
6. 创建一个管理员用户:
```sql
INSERT INTO user_info (user_name, user_type, email, phone, password) VALUES ('admin', 'staff', 'admin@example.com', '123456789', 'admin123');
```
7. 创建一个普通用户:
```sql
INSERT INTO user_info (user_name, user_type, email, phone, password) VALUES ('user', 'student', 'user@example.com', '987654321', 'user123');
```
8. 插入一些书籍信息:
```sql
INSERT INTO book_info (book_name, author, press, publish_date, price, total_amount, available_amount) VALUES ('Java编程思想', 'Bruce Eckel', '机械工业出版社', '2002-03-01', 98, 100, 100);
INSERT INTO book_info (book_name, author, press, publish_date, price, total_amount, available_amount) VALUES ('算法导论', 'Thomas H. Cormen', '机械工业出版社', '2013-05-01', 128, 50, 50);
INSERT INTO book_info (book_name, author, press, publish_date, price, total_amount, available_amount) VALUES ('深入理解JVM', '周志明', '机械工业出版社', '2014-01-01', 89, 80, 80);
```
9. 模拟借阅和归还操作:
```sql
-- 借阅算法导论
UPDATE book_info SET available_amount = available_amount - 1 WHERE book_id = 2;
INSERT INTO borrow_record (book_id, user_id, borrow_date, return_date) VALUES (2, 2, '2021-10-01', '2021-10-15');
-- 查询借阅记录
SELECT * FROM borrow_record;
-- 归还算法导论
UPDATE book_info SET available_amount = available_amount + 1 WHERE book_id = 2;
UPDATE borrow_record SET return_date = '2021-10-16' WHERE book_id = 2 AND user_id = 2 AND return_date = '2021-10-15';
-- 查询借阅记录
SELECT * FROM borrow_record;
```
以上是一个简单的图书管理系统的MySQL数据库实现,您可以根据实际情况进行修改和补充。
阅读全文