-- 圖書借閱數(shù)據(jù)庫設(shè)計(jì)
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了連平免費(fèi)建站歡迎大家使用!
-- 記錄每一本圖書借出和還回的操作
-- 書的信息
DROP DATABASE IF EXISTS db_lib;
CREATE DATABASE db_lib;
use db_lib
SET @READER_MAX = 10;
SET @EVERY_DAY_FEE=0.1;
SELECT 10 INTO @temp;
-- 新建相關(guān)的表
CREATE TABLE books (
id INT PRIMARY KEY AUTO_INCREMENT,
name CHAR(255) NOT NULL,
price DECIMAL(8,2) DEFAULT 0
);
CREATE TABLE readers (
id INT PRIMARY KEY AUTO_INCREMENT,
name CHAR(255) NOT NULL,
borrowed INT DEFAULT 0
);
CREATE TABLE lib_books (
id INT PRIMARY KEY AUTO_INCREMENT,
info INT,
FOREIGN KEY(info) REFERENCES books(id),
stat CHAR(10) NOT NULL DEFAULT "在館"
);
-- 記錄借書和還書
CREATE TABLE lib_loan (
id INT AUTO_INCREMENT,
who INT,
which INT,
check_out DATETIME NOT NULL DEFAULT NOW(),
check_in DATETIME DEFAULT NULL,
renew BOOLEAN NOT NULL DEFAULT 0,
latest DATE NOT NULL,
fee DECIMAL(8,2) DEFAULT NULL,
PRIMARY KEY(id),
FOREIGN KEY(who) REFERENCES readers(id),
FOREIGN KEY(which) REFERENCES lib_books(id)
);
-- 給館藏圖書添加10本書
-- 存儲(chǔ)過程
DELIMITER $$$
CREATE PROCEDURE add_books( IN n INT)
BEGIN
-- 需要有多行數(shù)據(jù)放入變量,需要用 游標(biāo) cursor
-- i 局部變量
DECLARE i INT DEFAULT 1;
-- @temp全局變量
SELECT COUNT(*) INTO @temp
FROM books;
WHILE i=n DO
INSERT INTO lib_books (info) VALUES ( FLOOR(RAND()*@temp)+1 );
SET i = i+1;
END WHILE;
END $$$
DELIMITER ;
-- 觸發(fā)器 trigger ,管理圖書被借出后,藏書和讀者信息的維護(hù)
-- 表中數(shù)據(jù)相關(guān)的操作: 增 刪 改 (查)
-- 一個(gè)表中某條記錄被修改:OLD 代表原有的那條記錄 NEW代表新的那條記錄
-- [BEFORE | AFTER] [INSERT | UPDATE | (DELETE) ]
DELIMITER $$$
CREATE TRIGGER before_insert_loan
BEFORE INSERT ON lib_loan
FOR EACH ROW
BEGIN
-- OLD NEW 已知
DECLARE v1 CHAR(100);
DECLARE v2 INT;
SET NEW.latest =DATE_ADD(NEW.check_out,INTERVAL 1 month);
-- 只能修改NEW
SELECT stat INTO v1
FROM lib_books
WHERE id=NEW.which;
SELECT borrowed INTO v2
FROM readers
WHERE id=NEW.who;
IF ( v1"在館" ) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = "圖書已借出!";
END IF ;
IF (v2=@READER_MAX) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = "讀者已經(jīng)到達(dá)最大借閱數(shù)!";
END IF ;
END $$$
DELIMITER ;
-- 書借出的數(shù)據(jù)維護(hù)
DELIMITER $$$
CREATE TRIGGER after_insert_loan
AFTER INSERT ON lib_loan
FOR EACH ROW
BEGIN
UPDATE lib_books
SET stat = "已借出"
WHERE id = NEW.which ;
UPDATE readers
SET borrowed = borrowed + 1
WHERE id = NEW.who;
END $$$
DELIMITER ;
-- 書歸還的數(shù)據(jù)維護(hù)
DELIMITER $$$
CREATE TRIGGER after_update_loan
AFTER UPDATE ON lib_loan
FOR EACH ROW
BEGIN
IF(OLD.check_in is NULL AND NEW.check_in is NOT NULL) THEN
UPDATE lib_books
SET stat="在館"
WHERE id= NEW.which;
UPDATE readers
SET borrowed=borrowed -1
WHERE id=OLD.who;
END IF;
END $$$
DELIMITER ;
DELIMITER $$$
CREATE TRIGGER before_update_loan
BEFORE UPDATE ON lib_loan
FOR EACH ROW
BEGIN
DECLARE x INT;
IF(OLD.renew=0 AND NEW.renew=1 ) THEN
SET NEW.latest=DATE_ADD(OLD.latest,INTERVAL 1 month);
END IF;
-- 計(jì)算超期費(fèi)用
IF(OLD.check_in is NULL AND NEW.check_in is NOT NULL) THEN
SET x=DATEDIFF(NEW.check_in,NEW.latest);
IF(x=0 )THEN
SET NEW.fee =0 ;
ELSE
SET NEW.fee=@EVERY_DAY_FEE*x;
END IF;
END IF;
END $$$
DELIMITER ;
-- 添加若干的測試數(shù)據(jù)
INSERT INTO books (name, price)
VALUES
("javascript程序設(shè)計(jì)", 19.8),
("數(shù)據(jù)庫系統(tǒng)開發(fā)", 20.8),
("mysql數(shù)據(jù)庫必知必會(huì)", 28.8);
INSERT INTO readers ( name )
VALUES
("Alice"),
("Bob"),
("Carl");
-- 測試語句
SHOW TABLES;
-- 根據(jù)書的信息添加10本館藏圖書
CALL add_books( 10) ;
SELECT * from lib_books;
-- 借書
-- 2號讀者借了2號書
INSERT INTO lib_loan (who , which ) VALUES (2,2);
INSERT INTO lib_loan (who , which ) VALUES (2,3);
INSERT INTO lib_loan (who , which ) VALUES (2,4);
INSERT INTO lib_loan (who , which ) VALUES (1,2);
INSERT INTO lib_loan (who , which ) VALUES (1,6);
INSERT INTO lib_loan (who , which ) VALUES (2,1);
INSERT INTO lib_loan (who , which ) VALUES (1,1);
-- 還書
UPDATE lib_loan
SET check_in='2022-12-15 12:00:00'
WHERE id = 1;
-- 續(xù)借
UPDATE lib_loan
SET renew = 1
WHERE id = 4;
SELECT * FROM lib_books;
SELECT * FROM readers;
SELECT * FROM lib_loan;
HashMap的常用方法
map的存儲(chǔ)和修改方式?map.put(key, value);
map是否包含某個(gè)元素map.containsKey(key);
map某個(gè)key的value值map.get(key);
參考代碼
import?java.util.HashMap;
public?class?BookTest?{
static?HashMapString,?Integer?map=new?HashMapString,?Integer();
static{//初始化圖書信息
map.put("三國志",?8);
map.put("紅樓夢",?0);
map.put("西游記",?15);
}
public?static?void?main(String[]?args)?{//main方法
jieShu("水滸");
jieShu("三國志");
huanShu("三國志");
jieShu("紅樓夢");
huanShu("紅樓夢");
jieShu("紅樓夢");
}
public?static?void?jieShu(String?name){//借書
if(!map.containsKey(name)){//查詢圖書館是否有這本書
System.out.println("書籍:"+name+"\t查無此書!");
return;
}
if(map.get(name)=0){//查詢圖書數(shù)量是否為0
System.out.println("書籍:"+name+"\t已全部借出,請明日在來");
return;
}
map.put(name,?map.get(name)-1);
System.out.println("書籍:"+name+"\t借書成功。"+"\t庫存:"+map.get(name));
}
public?static?void?huanShu(String?name){//還書
if(!map.containsKey(name)){
System.out.println("查無此書");
}else{
map.put(name,?map.get(name)+1);
System.out.println("書籍:"+name+"\t還書成功。"+"\t庫存:"+map.get(name));
}
}
}
測試輸出
書籍:水滸 查無此書!
書籍:三國志 借書成功。 庫存:7
書籍:三國志 還書成功。 庫存:8
書籍:紅樓夢 已全部借出,請明日在來
書籍:紅樓夢 還書成功。 庫存:1
書籍:紅樓夢 借書成功。 庫存:0
switch(key){
case 1:
//借書
break;
case 2:
//還書
break;
}