Auto commit
This commit is contained in:
parent
dceb62f983
commit
a2e6ffbfde
@ -114,3 +114,82 @@ SELECT @result;
|
||||
```
|
||||
|
||||
### 第二套
|
||||
|
||||
```sql
|
||||
#1 创建表
|
||||
CREATE TABLE t_booking(
|
||||
bid CHAR(10) PRIMARY KEY,
|
||||
rid CHAR(10) NOT NULL,
|
||||
customerName VARCHAR(50),
|
||||
tel VARCHAR(30),
|
||||
btime DATE
|
||||
);
|
||||
|
||||
#2 插入记录
|
||||
INSERT INTO t_booking()
|
||||
VALUES('b0001', 'r0002', '诸葛亮', '1821234567', '2023-10-01');
|
||||
|
||||
#3 添加字段
|
||||
ALTER TABLE t_hotel ADD introduce VARCHAR(255);
|
||||
|
||||
#4 更新记录
|
||||
UPDATE t_room r
|
||||
SET r.price = 500
|
||||
WHERE r.rid = 'r0003';
|
||||
|
||||
#5 tj11
|
||||
SELECT *
|
||||
FROM t_hotel h
|
||||
WHERE h.`level` IN ('二星', '三星');
|
||||
|
||||
#6 tj12
|
||||
SELECT h.hid, ROUND(AVG(r.price), 2)
|
||||
FROM t_hotel h, t_room r
|
||||
WHERE h.hid = r.hid
|
||||
GROUP BY h.hid;
|
||||
|
||||
#7 子查询 + 视图
|
||||
CREATE VIEW v_hotel AS
|
||||
SELECT h.hname, r.rname, r.price
|
||||
FROM t_hotel h, t_room r
|
||||
WHERE h.hid = r.hid AND r.price =(
|
||||
SELECT MAX(price)
|
||||
FROM t_room
|
||||
WHERE hid = h.hid
|
||||
);
|
||||
|
||||
#8 触发器
|
||||
DELIMITER $$
|
||||
DROP TRIGGER IF EXISTS tri_updateInteroduce;
|
||||
CREATE TRIGGER tri_updateInteroduce
|
||||
BEFORE UPDATE ON t_hotel
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.`level` <> OLD.`level` THEN
|
||||
IF NEW.`level` IN ('一星', '二星') THEN
|
||||
SET NEW.introduce = '实用';
|
||||
ELSEIF NEW.`level` IN ('三星', '四星') THEN
|
||||
SET NEW.introduce = '豪华';
|
||||
ELSE
|
||||
SET NEW.introduce = '奢华';
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
END $$
|
||||
DELIMITER ;
|
||||
|
||||
#9 存储过程
|
||||
DELIMITER $$
|
||||
DROP PROCEDURE IF EXISTS pro_GetLowPrice;
|
||||
CREATE PROCEDURE pro_GetLowPrice(IN in_rname VARCHAR(30), OUT out_hname VARCHAR(30))
|
||||
BEGIN
|
||||
SELECT h.hname INTO out_hname
|
||||
FROM t_hotel h, t_room r
|
||||
WHERE h.hid = r.hid AND r.rname = in_rname
|
||||
ORDER BY r.price
|
||||
LIMIT 1;
|
||||
|
||||
END $$
|
||||
DELIMITER ;
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user