用SQL语句: 分布式查询优化计算机系每个职称的人数(5分) (11)分布式查询优化比王维利借书次数多的借阅名单(5分) (12)分布式查询与王纯在同一栋楼的读者名单(5分) (13)使用分布式存储过程实现添加读者信息;(5分) (14)使用分布式事务控制提交图书表新增和修改图书作者数据(5分) (15)使用分布式事务控制回滚向读者表新增多条数据操作(5分) (16)使用分布式事务在图书表新增一条语句前设置回滚点,在新增后回滚至回滚点,修改图书单价正常提交(5分
时间: 2024-01-21 09:02:00 浏览: 307
以下是可能的SQL实现:
10. 分布式查询优化计算机系每个职称的人数:
```
SELECT job_title, COUNT(*) AS num_of_people
FROM reader
WHERE department = '计算机系'
GROUP BY job_title;
```
11. 分布式查询优化比王维利借书次数多的借阅名单:
```
SELECT reader_name, borrowed_times
FROM (
SELECT r.reader_name, COUNT(*) AS borrowed_times
FROM reader r
JOIN borrow b ON r.reader_id = b.reader_id
GROUP BY r.reader_name
) a
JOIN (
SELECT COUNT(*) AS borrowed_times
FROM reader r
JOIN borrow b ON r.reader_id = b.reader_id
WHERE r.reader_name = '王维利'
) b ON a.borrowed_times > b.borrowed_times;
```
12. 分布式查询与王纯在同一栋楼的读者名单:
```
SELECT r.reader_name
FROM reader r
JOIN building_info b ON r.building_id = b.building_id
WHERE b.building_name = (
SELECT building_name
FROM reader
JOIN building_info ON reader.building_id = building_info.building_id
WHERE reader.reader_name = '王纯'
);
```
13. 使用分布式存储过程实现添加读者信息:
```
CREATE PROCEDURE add_reader (
IN r_id INT,
IN r_name VARCHAR(20),
IN r_dept VARCHAR(20),
IN r_job_title VARCHAR(20)
)
BEGIN
INSERT INTO reader(reader_id, reader_name, department, job_title)
VALUES (r_id, r_name, r_dept, r_job_title);
END;
```
14. 使用分布式事务控制提交图书表新增和修改图书作者数据:
```
START TRANSACTION;
INSERT INTO book(book_id, book_name, author)
VALUES (1, 'book1', 'author1');
UPDATE book SET author = 'author2' WHERE book_id = 1;
COMMIT;
```
15. 使用分布式事务控制回滚向读者表新增多条数据操作:
```
START TRANSACTION;
INSERT INTO reader(reader_id, reader_name) VALUES (1, 'reader1');
INSERT INTO reader(reader_id, reader_name) VALUES (2, 'reader2');
-- 此处出现异常,回滚操作
ROLLBACK;
```
16. 使用分布式事务在图书表新增一条语句前设置回滚点,在新增后回滚至回滚点,修改图书单价正常提交:
```
START TRANSACTION;
SAVEPOINT before_insert;
INSERT INTO book(book_id, book_name, price)
VALUES (1, 'book1', 10);
-- 此处出现异常,回滚至回滚点
ROLLBACK TO before_insert;
UPDATE book SET price = 20 WHERE book_id = 1;
COMMIT;
```
阅读全文