1. 创建学生食堂饭卡管理数据库:CardDB 2.创建数据表:T_card、T_add_money、T_consume_money 3. 创建主外键约束,以及默认约束 期中默认约束,T_card表中,饭卡生成时间”2022-1-1” 4. 给每个表插入6条以上数据,使插入数据符合真实数据库逻辑 5. 完成以下查询条件 (1)查询所有“李”姓同学,最大的饭卡余额和最小饭卡余额 (2)查询在充值时间段“2022-1-2”到“2023-3-5”之间,饭卡充值金额由小到大进行排序 (3)要求在充值时间“2022-3-1”号到“2022-6-1”这三个月中,给每个学生的饭卡余额都统一增长10%,输出学生姓名,饭卡余额,充值时间 (4)要求当饭卡余额不足100元时,要求输出本次消费金额以后,饭卡还剩多少余额,且输出学生姓名,和饭卡最后余额 (5) 要求学号在(1001\1005\1003)三位同学中,所有的充值金额总和,本次消费金额综合。
时间: 2023-12-21 17:06:44 浏览: 183
以下是基于 MySQL 数据库的实现:
1. 创建学生食堂饭卡管理数据库:CardDB
```mysql
CREATE DATABASE CardDB;
USE CardDB;
```
2. 创建数据表:T_card、T_add_money、T_consume_money
```mysql
CREATE TABLE T_card (
s_id INT PRIMARY KEY,
s_name VARCHAR(20) NOT NULL,
s_gender CHAR(1) NOT NULL,
s_card_id CHAR(10) NOT NULL UNIQUE,
s_card_balance DECIMAL(10, 2) DEFAULT 0.00,
s_card_create_time DATE DEFAULT '2022-01-01'
);
CREATE TABLE T_add_money (
add_id INT PRIMARY KEY AUTO_INCREMENT,
s_id INT,
add_money DECIMAL(10, 2) NOT NULL,
add_time DATE NOT NULL,
FOREIGN KEY (s_id) REFERENCES T_card(s_id)
);
CREATE TABLE T_consume_money (
consume_id INT PRIMARY KEY AUTO_INCREMENT,
s_id INT,
consume_money DECIMAL(10, 2) NOT NULL,
consume_time DATE NOT NULL,
FOREIGN KEY (s_id) REFERENCES T_card(s_id)
);
```
3. 创建主外键约束,以及默认约束
```mysql
-- 创建主键约束
ALTER TABLE T_card ADD CONSTRAINT pk_s_id PRIMARY KEY (s_id);
-- 创建外键约束
ALTER TABLE T_add_money ADD CONSTRAINT fk_s_id FOREIGN KEY (s_id) REFERENCES T_card(s_id);
ALTER TABLE T_consume_money ADD CONSTRAINT fk_s_id FOREIGN KEY (s_id) REFERENCES T_card(s_id);
-- 创建默认约束
ALTER TABLE T_card ALTER COLUMN s_card_create_time SET DEFAULT '2022-01-01';
```
4. 给每个表插入6条以上数据,使插入数据符合真实数据库逻辑
```mysql
-- 插入数据到 T_card 表
INSERT INTO T_card (s_id, s_name, s_gender, s_card_id, s_card_balance) VALUES
(1001, '张三', '男', '1001', 50.00),
(1002, '李四', '女', '1002', 100.00),
(1003, '王五', '男', '1003', 200.00),
(1004, '赵六', '女', '1004', 150.00),
(1005, '孙七', '男', '1005', 80.00),
(1006, '周八', '女', '1006', 120.00);
-- 插入数据到 T_add_money 表
INSERT INTO T_add_money (s_id, add_money, add_time) VALUES
(1001, 20.00, '2022-01-05'),
(1002, 30.00, '2022-02-10'),
(1003, 50.00, '2022-02-15'),
(1004, 40.00, '2022-03-05'),
(1005, 10.00, '2022-03-20'),
(1006, 60.00, '2022-04-01');
-- 插入数据到 T_consume_money 表
INSERT INTO T_consume_money (s_id, consume_money, consume_time) VALUES
(1001, 10.00, '2022-01-06'),
(1002, 20.00, '2022-02-12'),
(1003, 30.00, '2022-02-18'),
(1004, 50.00, '2022-03-10'),
(1005, 15.00, '2022-03-22'),
(1006, 40.00, '2022-04-05');
```
5. 完成以下查询条件
(1)查询所有“李”姓同学,最大的饭卡余额和最小饭卡余额
```mysql
SELECT
s_name, MAX(s_card_balance) AS max_balance, MIN(s_card_balance) AS min_balance
FROM
T_card
WHERE
s_name LIKE '李%';
```
(2)查询在充值时间段“2022-1-2”到“2023-3-5”之间,饭卡充值金额由小到大进行排序
```mysql
SELECT
T_card.s_name, T_add_money.add_money, T_add_money.add_time
FROM
T_card
JOIN
T_add_money
ON
T_card.s_id = T_add_money.s_id
WHERE
T_add_money.add_time BETWEEN '2022-01-02' AND '2023-03-05'
ORDER BY
T_add_money.add_money ASC;
```
(3)要求在充值时间“2022-3-1”号到“2022-6-1”这三个月中,给每个学生的饭卡余额都统一增长10%,输出学生姓名,饭卡余额,充值时间
```mysql
UPDATE
T_card
SET
s_card_balance = s_card_balance * 1.1
WHERE
s_id IN (
SELECT
s_id
FROM
T_add_money
WHERE
add_time BETWEEN '2022-03-01' AND '2022-06-01'
);
SELECT
T_card.s_name, T_card.s_card_balance, T_add_money.add_time
FROM
T_card
JOIN
T_add_money
ON
T_card.s_id = T_add_money.s_id
WHERE
T_add_money.add_time BETWEEN '2022-03-01' AND '2022-06-01';
```
(4)要求当饭卡余额不足100元时,要求输出本次消费金额以后,饭卡还剩多少余额,且输出学生姓名,和饭卡最后余额
```mysql
SELECT
T_card.s_name,
CASE
WHEN T_card.s_card_balance - T_consume_money.consume_money >= 100.00 THEN T_consume_money.consume_money
ELSE T_card.s_card_balance - 100.00
END AS after_consume_balance
FROM
T_card
JOIN
T_consume_money
ON
T_card.s_id = T_consume_money.s_id;
```
(5)要求学号在(1001\1005\1003)三位同学中,所有的充值金额总和,本次消费金额综合。
```mysql
SELECT
T_card.s_id,
SUM(T_add_money.add_money) AS add_total,
SUM(T_consume_money.consume_money) AS consume_total
FROM
T_card
LEFT JOIN
T_add_money
ON
T_card.s_id = T_add_money.s_id
LEFT JOIN
T_consume_money
ON
T_card.s_id = T_consume_money.s_id
WHERE
T_card.s_id IN (1001, 1005, 1003)
GROUP BY
T_card.s_id;
```
阅读全文