建设部建造师强制注销网站,h5响应式网站模板下载,自己怎么制作一个公众号,网络营销推广策划案例一 子查询
也被称作内查询或者嵌套查询#xff0c;是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句
是先于主查询语句被执行的#xff0c;其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
①子语句可以与主语句所查询的表相同#xff0c;也可以是不…一 子查询
也被称作内查询或者嵌套查询是指在一个查询语句里面还嵌套着另一个查询语 句。子查询语句
是先于主查询语句被执行的其结果作为外层的条件返回给主查询进行下一 步的查询过滤。
①子语句可以与主语句所查询的表相同也可以是不同表
②子语句中的sql语句是为了最后过滤出一个结果集用于主语句的判断条件
③ in: 将主表和子表关联/连接的语法
环境准备
mysql use kgc_ky35;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql show tables;
--------------------
| Tables_in_kgc_ky35 |
--------------------
| HP |
| test01 |
| test02 |
| test03 |
--------------------
4 rows in set (0.00 sec)mysql select * from HP;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)in查询已知数据记录
IN 用来判断某个值是否在给定的结果集中通常结合子查询来使用 mysql create table football (id int);
Query OK, 0 rows affected (0.01 sec)mysql insert into football values(1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql select * from football;
------
| id |
------
| 1 |
| 2 |
| 3 |
------
3 rows in set (0.00 sec)mysql select id,name,score from HP where id in (select id from football);
-------------------
| id | name | score |
-------------------
| 1 | hz | 90.00 |
| 2 | mdq | 80.00 |
| 2 | cx | 60.00 |
-------------------
3 rows in set (0.00 sec)#先查询football数据表中的id字段列 将查询到的结果id字段列作为一个已知的值的数据记录再根据已知的值的数据记录查询football数据表中id,name,score字段列
多表查询举例
mysql select name,score from HP where id in (select id from football where score60);
-------------
| name | score |
-------------
| hz | 90.00 |
| mdq | 80.00 |
-------------
2 rows in set (0.00 sec)mysql select name,score from HP where id in (select id from HP where score70);
-------------
| name | score |
-------------
| hz | 90.00 |
| mdq | 80.00 |
| cx | 60.00 |
-------------
3 rows in set (0.00 sec)多表查询
子查询不仅可以在 SELECT 语句中使用在 INERT、UPDATE、DELETE 中也同样适用。在嵌套
的时候子查询内部还可以再次嵌套新的子查询也就是说可以多层嵌套。
①语法
IN 用来判断某个值是否在给定的结果集中通常结合子查询来使用
表达式 [NOT] IN 子查询
当表达式与子查询返回的结果集中的某个值相等时返回 TRUE否则返回 FALSE。 若启用了 NOT 关键字则返回值相反。
注意子查询只能返回一列数据如果需 求比较复杂一列解决不了问题可以使用多层嵌套的
方式来应对。 多数情况下子查询都是与 SELECT 语句一起使用的
表示 先匹配出member表内的id字段为基础匹配的结果集2,3)然后再执行主语句以主语句的id 为基础 进行where 条件判断/过滤
②插入
mysql select * from test03;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
-----------------------------------
2 rows in set (0.00 sec)mysql delete from test03;
Query OK, 2 rows affected (0.01 sec)mysql insert into test03 select * from test01 where id in ( select id from HP);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql select * from test03;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)③修改数据
update语句也可以使用子查询。update 内的子查询在 set 更新内容时可以是单独的一
列也可以是多列。
mysql select * from test03;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql update test03 set score44 where id in (select id from HP where id1);
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql select * from test03;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 44.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)④删除
在 IN 前面还可以添加 NOT其作用与IN相反表示否定即不在子查询的结果集里面
DELETE 也适用于子查询
环境准备
mysql show tables;
--------------------
| Tables_in_kgc_ky35 |
--------------------
| HP |
| football |
| jinjin |
| kangkang |
| test01 |
| test02 |
| test03 |
| test1 |
| test2 |
| v_HP |
| v_hc |
| v_score |
--------------------
12 rows in set (0.00 sec)mysql select * from test02;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql delete from test02 where id in (select id from test02 where score 85);
ERROR 1093 (HY000): You cant specify target table test02 for update in FROM clause
mysql select * from test02 where score 85;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
-----------------------------------
1 row in set (0.00 sec)mysql delete from test02 where id in (select id from test02 where score 85);
ERROR 1093 (HY000): You cant specify target table test02 for update in FROM clause
mysql select * from test01;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)多表删除至少两个表且有相同的部分才行mysql update test01 set score96,hobby2 where id1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql select * from test01;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 96.00 | tianye | 2 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.01 sec)mysql delete from test02 where id in (select id from test01 where score 85);
Query OK, 1 row affected (0.01 sec)结果
mysql select * from test01;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 96.00 | tianye | 2 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql select * from test02;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
2 rows in set (0.00 sec)
⑤exists
主要用于判断子查询的结果集是否为空。如果不为空 则返回 TRUE反之则返回 FALSE
mysql select * from test02;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql select sum(score) from test02 where exists(select id from test02 where score 80);
------------
| sum(score) |
------------
| 230.00 |
------------
1 row in set (0.00 sec)mysql select sum(score) from test02 where exists(select id from test02 where score 90);
------------
| sum(score) |
------------
| NULL |
------------
1 row in set (0.00 sec)⑥not取反
在 IN 前面还可以添加 NOT其作用与IN相反表示否定即不在子查询的结果集里面
mysql select * from test01;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 96.00 | tianye | 2 |
| 2 | mdq | 80.00 | renduo | 3 |
| 3 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql select * from test02;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 4 | jhg | 85.00 | nanjing | 4 |
| 2 | mdq | 80.00 | renduo | 3 |
| 3 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)
mysql select * from test01 where id not in(select id from test02 where id);
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 96.00 | tianye | 2 |
-----------------------------------
1 row in set (0.00 sec)⑦别名as
查询info表id,name 字段select id,name from info; 可以查看到info表的内容
将结果集做为一张表进行查询的时候我们也需要用到别名示例
需求从info表中的id和name字段的内容做为内容 输出id的部分
mysql select * from test02;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql select id ,name from HP;
------------
| id | name |
------------
| 1 | hz |
| 2 | mdq |
| 2 | cx |
------------
3 rows in set (0.00 sec)mysql select id ,name from (select id,name from HP) a;
------------
| id | name |
------------
| 1 | hz |
| 2 | mdq |
| 2 | cx |
------------
3 rows in set (0.01 sec)二 视图优化操作安全方案
数据库中的虚拟表这张虚拟表中不包含真实数据只是做了真实数据的映射 视图可以理解为镜花水月/倒影动态保存结果集数据
作用场景:
针对不同的人权限身份提供不同结果集的“表”以表格的形式展示
展示的部分是info表
展示的一张或多张表
功能
简化查询结果集、灵活查询、可以针对不同用户呈现不同结果集、相对有更高的安全性
本质而言视图是一种select(结果集的呈现)
视图适合于多表连接浏览时使用!不适合增、删、改
而存储过程适合于使用较频繁的SQL语句这样可以提高执行效率!
视图和表的区别
① 视图是已经编译好的sql语句。而表不是
② 视图没有实际的物理记录。而表有。 show table status\G
③ 表只用物理空间而视图不占用物理空间视图只是逻辑概念的存在表可以及时对它进行修改但视图只能有创建的语句来修改
④ 视图是查看数据表的一种方法可以查询数据表中某些字段构成的数据只是一些SQL语句的集合。从安全的角度说视图可以不给用户接触数据表从而不知道表结构。
⑤ 表属于全局模式中的表是实表视图属于局部模式的表是虚表。
⑥ 视图的建立和删除只影响视图本身不影响对应的基本表。但是更新视图数据是会影响到基本表的
联系
视图(view)是在基本表之上建立的表它的结构(即所定义的列)和内容(即所有数据行)都来自基本
表它依据基本表存在而存在。一个视图可以对应一个基本表也可以对应多个基本表。视图是基
本表的抽象和在逻辑意义上建立的新关系。
单表演练
mysql desc test02;
--------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
--------------------------------------------------
| id | int(10) | YES | | NULL | |
| name | varchar(16) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| hobby | int(8) | YES | | NULL | |
--------------------------------------------------
5 rows in set (0.00 sec)mysql create view v_score as select * from test02 where score90;
Query OK, 0 rows affected (0.01 sec)
#创建视图mysql show table status\G #查看表状态
*************************** 1. row ***************************Name: HPEngine: MyISAMVersion: 10Row_format: DynamicRows: 3Avg_row_length: 28Data_length: 84
Max_data_length: 281474976710655Index_length: 2048Data_free: 0Auto_increment: NULLCreate_time: 2024-03-26 15:53:59Update_time: 2024-03-26 15:58:45Check_time: NULLCollation: utf8_general_ciChecksum: NULLCreate_options: Comment:
*************************** 2. row ***************************
mysql desc v_score; #查看视图与源表结构
--------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
--------------------------------------------------
| id | int(10) | YES | | NULL | |
| name | varchar(16) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| hobby | int(8) | YES | | NULL | |
--------------------------------------------------
5 rows in set (0.00 sec)多表视图创立
相同的名字或相同的值都可以导入里面
mysql create table kangkang(id int,name varchar(15),age varchar(16));
Query OK, 0 rows affected (0.00 sec)mysql select * from HP;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql insert into kangkang values(1,liubei,56);
Query OK, 1 row affected (0.00 sec)mysql insert into kangkang values(2,lubu,40);
Query OK, 1 row affected (0.00 sec)mysql insert into kangkang values(3,jiaxu,75);
Query OK, 1 row affected (0.00 sec)mysql insert into kangkang values(4,mazhong,28);
Query OK, 1 row affected (0.01 sec)mysql select * from kangkang;
---------------------
| id | name | age |
---------------------
| 1 | liubei | 56 |
| 2 | lubu | 40 |
| 3 | jiaxu | 75 |
| 4 | mazhong | 28 |
---------------------
4 rows in set (0.00 sec)
4 rows in set (0.00 sec)mysql create view v_HP(id,name,score,age) as select k.id,k.name,k.score,s.age from HP k,kangkang s where k.names.name;
Query OK, 0 rows affected (0.01 sec)mysql select * from v_HP;
Empty set (0.01 sec)
#因为名字不相同需要改一下
mysql create view v_hc(id,name,score,age) as select k.id,k.name,k.score,s.age from HP k,kangkang s where k.names.name;
Query OK, 0 rows affected (0.01 sec)
# v_hc,可任意取名
mysql select * from v_hc;
-------------------------
| id | name | score | age |
-------------------------
| 2 | cx | 60.00 | 28 |
-------------------------
修改表数据
修改表不能修改以函数、复合函数方式计算出来的字段
mysql select * from HP;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)
mysql update HP set score77 where namecx;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql select * from v_HP;
-------------------------
| id | name | score | age |
-------------------------
| 2 | cx | 77.00 | 28 |
-------------------------修改视图
修改视图时也修改表的数据
mysql select * from v_HP;
-------------------------
| id | name | score | age |
-------------------------
| 2 | cx | 77.00 | 28 |
-------------------------
1 row in set (0.00 sec)mysql update v_HP set score98 where namecx;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql select * from v_HP;
-------------------------
| id | name | score | age |
-------------------------
| 2 | cx | 98.00 | 28 |
-------------------------
1 row in set (0.00 sec)mysql select * from HP;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 85.00 | renduo | 3 |
| 2 | cx | 98.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)三 NULL 值--缺失
在 SQL 语句使用过程中经常会碰到 NULL 这几个字符。通常使用 NULL 来表示缺失 的值也就是在表中该字段是没有值的。如果在创建表时限制某些字段不为空则可以使用 NOT NULL 关键字不使用则默认可以为空。在向表内插入记录或者更新记录时如果该字段没有 NOT NULL 并且没有值这时候新记录的该字段将被保存为 NULL。需要注意 的是NULL 值与数字 0 或者空白spaces的字段是不同的值为 NULL 的字段是没有 值的。在 SQL 语句中使用 IS NULL 可以判断表内的某个字段是不是 NULL 值相反的用 IS NOT NULL 可以判断不是 NULL 值。
null值与空值的区别(空气与真空)
空值长度为0不占空间NULL值的长度为null占用空间
is null无法判断空值空值使用“或者”来处理
count计算时NULL会忽略空值会加入计算 一般三种模式
mysql select length(null),length(),length(ad);
----------------------------------------
| length(null) | length() | length(ad) |
----------------------------------------
| NULL | 0 | 2 |
----------------------------------------
1 row in set (0.00 sec)
mysql show tables;
--------------------
| Tables_in_kgc_ky35 |
--------------------
| HP |
| football |
| jinjin |
| kangkang |
| test01 |
| test02 |
| test03 |
| test1 |
| test2 |
| v_HP |
| v_hc |
| v_score |
--------------------
12 rows in set (0.00 sec)mysql select * from HP;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 85.00 | renduo | 3 |
| 2 | cx | 98.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 sec)mysql alter table HP add length varchar(18);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql select * from HP;
-------------------------------------------
| id | name | score | address | hobby | length |
-------------------------------------------
| 1 | hz | 90.00 | tianye | 3 | NULL |
| 2 | mdq | 85.00 | renduo | 3 | NULL |
| 2 | cx | 98.00 | guancai | 3 | NULL |
-------------------------------------------
3 rows in set (0.00 sec)
mysql select * from HP where length is null;
-------------------------------------------
| id | name | score | address | hobby | length |
-------------------------------------------
| 1 | hz | 90.00 | tianye | 3 | NULL |
| 2 | mdq | 85.00 | renduo | 3 | NULL |
| 2 | cx | 98.00 | guancai | 3 | NULL |
-------------------------------------------
3 rows in set (0.00 sec)mysql select count(length) from HP;
---------------
| count(length) |
---------------
| 0 |
---------------
1 row in set (0.00 sec)四 连接查询
将来自两个或多个表的记录行结合起来基于这些表之间的 共同字段进行数据的查询。
尽量两个表查询三个表会延缓显示时间
内连接左连接右连接
环境准备
mysql select * from test02;
-----------------------------------
| id | name | score | address | hobby |
-----------------------------------
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
-----------------------------------
3 rows in set (0.00 secmysql select * from kangkang;
--------------------
| id | name | age |
--------------------
| 1 | liubei | 56 |
| 2 | lubu | 40 |
| 3 | jiaxu | 75 |
| 4 | cx | 28 |
--------------------
4 rows in set (0.00 sec)①内连接
就是两张或多张表中同时符合某种条件的数据记录的组合。通常在 FROM 子句中使用关键字 INNER JOIN 来连接多张表并使用 ON 子句设置连接条件内连接是系统默认的表连接所以在 FROM 子句后可以省略 INNER 关键字只使用 关键字 JOIN。同时有多个表时也可以连续使用 INNER JOIN 来实现多表的内连接
注意不过为了更好的性能建议最好不要超过三个表
格式 select 表1[2].字段1,表1[2].字段2,... from 表1 inner join 表2 on 表1.同名字段表2.同名字段;
mysql select test02.id,test02.name from test02 inner join kangkang on test02.namekangkang.name;
------------
| id | name |
------------
| 2 | cx |
------------
1 row in set (0.00 sec)
内连查询通过inner join 的方式将两张表指定的相同字段的记录行输出出来
②左连接
在 FROM 子句中使用 LEFT JOIN 或者 LEFT OUTER JOIN 关键字来表示。左连接以左侧表为基础表接收左表的所有行并用这些行与右侧参 考表中的记录进行匹配也就是说匹配左表中的所有行以及右表中符合条件的行 格式
select * from 表1 left join 表2 on 表1.同名字段表2.同名字段;
左连接中左表的记录将会全部表示出来而右表只会显示符合搜索条件的记录右表记录不足的地方均为 NULL。
mysql select * from test02 left join kangkang on test02.namekangkang.name;
-----------------------------------------------------
| id | name | score | address | hobby | id | name | age |
-----------------------------------------------------
| 2 | cx | 60.00 | guancai | 3 | 4 | cx | 28 |
| 1 | hz | 90.00 | tianye | 3 | NULL | NULL | NULL |
| 2 | mdq | 80.00 | renduo | 3 | NULL | NULL | NULL |
-----------------------------------------------------
3 rows in set (0.00 sec)③右连接
在 FROM 子句中使用 RIGHT JOIN 或者 RIGHT OUTER JOIN 关键字来表示。右连接跟左连接正好相反它是以右表为基础表用于接收右表中的所有行并用这些记录与左表中的行进行匹配
mysql select * from test02 right join kangkang on test02.namekangkang.name;
-------------------------------------------------------
| id | name | score | address | hobby | id | name | age |
-------------------------------------------------------
| 2 | cx | 60.00 | guancai | 3 | 4 | cx | 28 |
| NULL | NULL | NULL | NULL | NULL | 1 | liubei | 56 |
| NULL | NULL | NULL | NULL | NULL | 2 | lubu | 40 |
| NULL | NULL | NULL | NULL | NULL | 3 | jiaxu | 75 |
-------------------------------------------------------
4 rows in set (0.00 sec)在右连接的查询结果集中除了符合匹配规则的行外还包括右表中有但是左表中不匹 配的行这些记录在左表中以 NULL 补足
五 存储过程
前面学习的 MySQL 相关知识都是针对一个表或几个表的单条 SQL 语句使用这样的SQL 语句虽
然可以完成用户的需求但在实际的数据库应用中
有些数据库操作可能会非常复杂可能会需要多条 SQL 语句一起去处理才能够完成这时候就可
以使用存储过程 轻松而高效的去完成这个需求有点类似shell脚本里的函数
1 存储过程是一组为了完成特定功能的SQL语句集合。 两个点 第一 触发器定时任务 第二个判断 2 存储过程这个功能是从5.0版本才开始支持的它可以加快数据库的处理速度增强数据库在实际应用中的灵活性。存储过程在使用过程中是将常用或者复杂的工作预先使用SQL语句写好并用一个指定的名称存储起来这个过程经编译和优化后存储在数据库服务器中。当需要使用该存储过程时只需要调用它即可。操作数据库的传统 SQL 语句在执行时需要先编译然后再去执行跟存储过程一对比明显存储过程在执行上速度更快效率更高
1 存储过程的优点
①执行一次后会将生成的二进制代码驻留缓冲区提高执行效率
②SQL语句加上控制语句的集合灵活性高
③在服务器端存储客户端调用时降低网络负载
④可多次重复被调用可随时修改不影响客户端调用
⑤可完成所有的数据库操作也可控制数据库的信息访问权限
语法
CREATE PROCEDURE 过程名 ( [过程参数[,…] ] ) 过程体
[过程参数[,…] ] 格式
过程名尽量避免与内置的函数或字段重名
过程体语句
[ IN | OUT | INOUT ] 参数名类型
mysql delimiter $$
#将语句的结束符号从分号;临时改为两个$$(可以自定义)
mysql create procedure class()
#创建存储过程过程名为class不带参数- begin
#过程体以关键字 BEGIN 开始- create table class3(id int,name varchar(8),score decimal(5,2));- insert into class3 values(1,wsc,98),(2,ljc,95);- select * from class3;
#过程体语句- END $$
#过程体以关键字 END 结束
Query OK, 0 rows affected (0.12 sec)
mysql DELIMITER ;
#将语句的结束符号恢复为分号
环境准备
mysql show tables;
--------------------
| Tables_in_kgc_ky35 |
--------------------
| HP |
| football |
| kangkang |
| test01 |
| test02 |
| test03 |
| test1 |
| test2 |
| v_HP |
| v_hc |
| v_score |
--------------------
11 rows in set (0.00 sec)mysql select * from kangkang;
--------------------
| id | name | age |
--------------------
| 1 | liubei | 56 |
| 2 | lubu | 40 |
| 3 | jiaxu | 75 |
| 4 | cx | 28 |
--------------------
4 rows in set (0.00 sec)创建存储
mysql delimiter $$
mysql create procedure kangkang ()- begin- create table jinjin(id int,name varchar(10),score int(20));- insert into jinjin values(1,dieyi,65);- select * from jinjin;- END $$
Query OK, 0 rows affected (0.00 sec)
2 调用存储 mysql call kangkang;
--------------------
| id | name | score |
--------------------
| 1 | dieyi | 65 |
--------------------
1 row in set (0.01 sec)Query OK, 0 rows affected (0.01 sec)3 查看存储
mysql show create procedure kangkang\G
*************************** 1. row ***************************Procedure: kangkangsql_mode: PIPES_AS_CONCAT,ANSI_QUOTES,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIONCreate Procedure: CREATE DEFINERrootlocalhost PROCEDURE kangkang()
begin
create table jinjin(id int,name varchar(10),score int(20));
insert into jinjin values(1,dieyi,65);
select * from jinjin;
END
character_set_client: utf8
collation_connection: utf8_general_ciDatabase Collation: utf8_general_ci
1 row in set (0.00 sec)mysql show procedure status like %kangkang%\G
*************************** 1. row ***************************Db: kgc_ky35Name: kangkangType: PROCEDUREDefiner: rootlocalhostModified: 2024-03-27 19:46:16Created: 2024-03-27 19:46:16Security_type: DEFINERComment:
character_set_client: utf8
collation_connection: utf8_general_ciDatabase Collation: utf8_general_ci
1 row in set (0.00 sec)4 存储过程参数
IN 输入参数表示调用者向过程传入值传入值可以是字面量或变量
OUT 输出参数表示过程向调用者传出值(可以返回多个值)传出值只能是变量
INOUT 输入输出参数既表示调用者向过程传入值又表示过程向调用者传出值值只能是变量
即表示调用者向过程传入值又表示过程向调用者传出值只能是变量)
mysql delimiter
mysql create procedure yinyin(in inname varchar(40))- begin- select * from kangkang where nameinname;- end
Query OK, 0 rows affected (0.01 sec)mysql call yinyin(cx);
------------------
| id | name | age |
------------------
| 4 | cx | 28 |
------------------
1 row in set (0.00 sec)5 修改存储
ALTER PROCEDURE 过程名[特征... ]
SECURITY:安全等级
invoker:当定义为INVOKER时只要执行者有执行权限就可以成功执行。
6 删除存储 存储过程内容的修改方法是通过删除原有存储过程之后再以相同的名称创建新的存储过程。
mysql drop procedure if exists cx;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql call cx();
ERROR 1305 (42000): PROCEDURE kgc_ky35.cx does not exist
mysql show create procedure cx\G;
ERROR 1305 (42000): PROCEDURE cx does not exist
ERROR:
No query specified