更新 ch05_SQL高级处理.md

This commit is contained in:
mba1398 2025-04-07 15:23:43 +08:00 committed by GitHub
parent f1162f944e
commit dadcb3d4d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 33 deletions

View File

@ -235,56 +235,40 @@ CREATE
- 查询 - 查询
下面的示例显示了一个简单的存储过程,给定一个国家代码,计算在 `world` 数据库的城市表中出现的该国家的城市数量。使用 `IN` 参数传递国家代码,使用 `OUT` 参数返回城市计数: 下面的示例显示了一个简单的存储过程,给定一个国家代码,计算在 `world` 数据库的城市表中出现的该国家的城市数量。使用 `IN` 参数传递国家代码,使用 `OUT` 参数返回城市计数:
```sql ```sql
mysql> DELIMITER // DELIMITER //
mysql> DROP PROCEDURE IF EXISTS citycount //
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT) DROP PROCEDURE IF EXISTS citycount //
CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)
BEGIN BEGIN
SELECT COUNT(*) INTO cities FROM world.city SELECT COUNT(*) INTO cities FROM world.city
WHERE CountryCode = country; WHERE CountryCode = country;
END// END//
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ; DELIMITER ;
mysql> CALL citycount('CHN', @cities); -- cities in China
Query OK, 1 row affected (0.01 sec)
-> SELECT @cities; CALL citycount('CHN', @cities); -- cities in China
+---------+
| @cities | SELECT @cities;
+---------+
| 363 |
+---------+
1 row in set (0.04 sec)
``` ```
- 创建表 - 创建表
```SQL ```SQL
mysql> use world; use world;
Database changed
mysql> DELIMITER $$ DELIMITER $$
mysql> CREATE DEFINER=`root`@`localhost` PROCEDURE `product_test`()
CREATE DEFINER=`root`@`localhost` PROCEDURE `product_test`()
BEGIN BEGIN
#Routine body goes here... #Routine body goes here...
CREATE TABLE product_test like shop.product; CREATE TABLE product_test like shop.product;
END$$ END$$
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER; DELIMITER;
mysql> call `product_test`();
Query OK, 0 rows affected (0.04 sec)
mysql> show tables; call `product_test`();
+-----------------+
| Tables_in_world | show tables;
+-----------------+
| city |
| country |
| countrylanguage |
| product_test |
+-----------------+
4 rows in set (0.02 sec)
``` ```
- 插入数据 - 插入数据
```SQL ```SQL