视图,函数,存储过程

视图

创建一个视图 player_above_avg_height

1
2
3
4
CREATE VIEW player_above_avg_height AS
SELECT player_id, height
FROM player
WHERE height > (SELECT AVG(height) from player)

实际效果

1
2
3
4
5
6
7
SELECT * FROM player_above_avg_height

# 相当于

SELECT player_id, height
FROM player
WHERE height > (SELECT AVG(height) from player)

函数

存储过程

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE PROCEDURE `add_num`(IN n INT)
BEGIN
DECLARE i INT;
DECLARE sum INT;

SET i = 1;
SET sum = 0;
WHILE i <= n DO
SET sum = sum + i;
SET i = i +1;
END WHILE;
SELECT sum;
END