1、创建表并插入数据
字段名 数据类型 主键 外键 非空 唯一 自增
id INT 是 否 是 是 否
name VARCHAR(50) 否 否 是 否 否
glass VARCHAR(50) 否 否 是 否 否
sch 表内容
id name glass
1 xiaoming glass 1
2 xiaojun glass 2
mysql> create table sch(
-> id int primary key auto_increment,
-> name varchar(50) NOT NULL,
-> glass varchar(50) NOT NULL);
Query OK, 0 rows affected (0.00 sec)
mysql> insert sch values
-> (1,'xiaoming','glass 1'),(2,'xiaojun','glass 2');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from sch;
+----+----------+---------+
| id | name | glass |
+----+----------+---------+
| 1 | xiaoming | glass 1 |
| 2 | xiaojun | glass 2 |
+----+----------+---------+
2 rows in set (0.00 sec)
2、要求以及解答
1、创建一个可以统计表格内记录条数的存储函数 ,函数名为count_sch()
mysql> \d $$
mysql> create function count_sch()
-> returns int
-> begin
-> declare a int default 0;
-> select count(*) into a from sch;
-> return a;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> \d;
mysql> select count_sch();
+-------------+
| count_sch() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
2、创建一个存储过程avg_sai,有3个参数,分别是deptno,job,接收平均工资,功能查询emp表dept为30,job为销售员的平均工资。
mysql> \d $$
mysql> create procedure svg_sai(in a int,in b varchar(20),out c int)
-> begin
-> select avg(sai) into c from emp
-> where deptno = a and job = b;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> \d;
mysql> call avg_sai(30,'销售员',@a);
Query OK, 1 row affected (0.01 sec)
mysql> select @a;
+-------+
| @a |
+-------+
| 14000 |
+-------+
1 row in set (0.00 sec)