字段名 数据类型 主键 外键 非空 唯一 自增
id INT 是 否 是 是 否
name VARCHAR(50) 否 否 是 否 否
glass VARCHAR(50) 否 否 是 否 否
mysql> create table sch (-> id int primary key not null unique,-> name varchar(50) not null,-> glass varchar(50) not null-> );
sch 表内容
id name glass
1 xiaommg glass 1
2 xiaojun glass 2
insert into sch values('1','xiaommg','glass 1'),('2','xiaojun','glass 2');
select * from sch ;
1、创建一个可以统计表格内记录条数的存储函数 ,函数名为count_sch()
1.1创建函数
mysql> \d $$
mysql> create procedure count_sch()-> begin -> select count(*) from sch;-> end $$
Query OK, 0 rows affected (0.00 sec)mysql> \d ;
1.2叫一下函数
mysql> call count_sch();
2、创建一个存储过程avg_sai,有3个参数,分别是deptno,job,接收平均工资,功能查询emp表dept为30,job为销售员的平均工资。(使用之前的emp表)
2.1创建emp表
CREATE TABLE `emp` (`empno` int(4) NOT NULL,`ename` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`job` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`mgr` int(4) NULL DEFAULT NULL,`hiredate` date NOT NULL,`sai` int(255) NOT NULL,`comm` int(255) NULL DEFAULT NULL,`deptno` int(2) NOT NULL,PRIMARY KEY (`empno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
2.2插入数据
INSERT INTO `emp` VALUES (1001, '甘宁', '文员', 1013, '2000-12-17', 8000, NULL, 20);
INSERT INTO `emp` VALUES (1002, '黛绮丝', '销售员', 1006, '2001-02-20', 16000, 3000, 30);
INSERT INTO `emp` VALUES (1003, '殷天正', '销售员', 1006, '2001-02-22', 12500, 5000, 30);
INSERT INTO `emp` VALUES (1004, '刘备', '经理', 1009, '2001-04-02', 29750, NULL, 20);
INSERT INTO `emp` VALUES (1005, '谢逊', '销售员', 1006, '2001-09-28', 12500, 14000, 30);
INSERT INTO `emp` VALUES (1006, '关羽', '经理', 1009, '2001-05-01', 28500, NULL, 30);
INSERT INTO `emp` VALUES (1007, '张飞', '经理', 1009, '2001-09-01', 24500, NULL, 10);
INSERT INTO `emp` VALUES (1008, '诸葛亮', '分析师', 1004, '2007-04-19', 30000, NULL, 20);
INSERT INTO `emp` VALUES (1009, '曾阿牛', '董事长', NULL, '2001-11-17', 50000, NULL, 10);
INSERT INTO `emp` VALUES (1010, '韦一笑', '销售员', 1006, '2001-09-08', 15000, 0, 30);
INSERT INTO `emp` VALUES (1011, '周泰', '文员', 1006, '2007-05-23', 11000, NULL, 20);
INSERT INTO `emp` VALUES (1012, '程普', '文员', 1006, '2001-12-03', 9500, NULL, 30);
INSERT INTO `emp` VALUES (1013, '庞统', '分析师', 1004, '2001-12-03', 30000, NULL, 20);
INSERT INTO `emp` VALUES (1014, '黄盖', '文员', 1007, '2002-01-23', 13000, NULL, 10);
INSERT INTO `emp` VALUES (1015, '张三', '保洁员', 1001, '2013-05-01', 80000, 50000, 50);
2.3创建avg_sai
mysql>\d $$
mysql> create procedure avg_sai(in a1 int ,in a2 varchar(50),out a3 int)-> begin-> select avg(sai) into a3 from emp-> where deptno = a1 and job = a2;-> end $$
Query OK, 0 rows affected (0.00 sec)mysql> \d ;
mysql> call avg_sai(30,'销售员',@xx);
ERROR 1366 (HY000): Incorrect string value: '\xE9\x94\x80\xE5\x94\xAE...' for column 'a2' at row 1
炸裂
有待完善。