当前位置:网站首页>mysql数据库基础:视图、变量
mysql数据库基础:视图、变量
2022-06-30 09:46:00 【持久的棒棒君】
视图
1、含义
简单理解为视图是一个虚拟表,行和列的数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的,只保存了sql逻辑,不保存查询结果。
- 使用场景
多个地方用到同样的查询结果,该查询结果使用的sql语句较复杂。
2、视图特点
1、宠用sql语句
2、简化复杂的sql操作,不必知道它的查询细节
3、保护数据,提高安全性(只提供客户想知道的数据)
3、创建/修改视图
- 语法
# 方式一:
create view 视图名
as
查询语句;
# 下面两个语句也可以用来修改视图
# 方式二
create or replace view 视图名
as
查询视图;
# 方式三
alter view 视图名
as
查询视图;
4、查看视图
# 方式一
desc 视图名;
# 方式二
show create view 视图名;
5、删除视图
drop view 视图名1,视图名2...;
6、更新视图
更新视图中的数据
- 语法
# 插入
insert into 视图名 varlues('值1','值2',...);
# 修改
update 视图名 set 列名='值',... where 条件
;
# 删除
delete from 视图名 where 条件;
需要注意的是一般是不允许进行视图的更新操作的,因为视图的更新会导致原表中的数据同样进行改变
以下类型的视图是不能更新的
7、视图与表的对比
| 类型 | 创建语法的关键字 | 是否实际占用物理空间 |
|---|---|---|
| 视图 | create | 只保存了sql逻辑,不占用空间 |
| 表 | create table | 保存了数据,占用空间 |
变量
1、分类
- 系统变量
- 全局变量
- 会话变量
- 自定义变量
- 用户变量
- 局部变量
2、系统变量
说明
变量由系统提供,不是用户定义,属于服务器层面。作用域
- 全局变量:服务器每次启动将为所有的全局变量赋初始值,针对于所有的会话连接有效。
- 会话变量:仅仅针对当前会话连接有效。
语法
1、查看所有的系统变量show global|【session】 variables2、查看满足条件的部分系统变量
show global|【session】variables like '%char%';3、查看指定的某个系统变量的值
select @@global|【session】.系统变量名4、为某个系统变量赋值
方式一:set global|【session】系统变量名 = 值;
方式二:set @@global|【session】.系统变量名=值;
补充:如果是全局级别,则需要加global,如果是会话级别,则需要加session,如果不写则默认为session
3、自定义变量
- 说明:
变量使用户自己定义的,不是由系统定义的 - 使用步骤:
(1)声明(2)赋值(3)使用(查看、比较、运算等)
3.1 用户变量
- 作用域:针对于当前会话连接有效,可以应用在任何地方,也就是begin end里面或begin end 外面
- 声明并初始化
方式一:
set @用户变量名=值;
方式二:set @用户变量名:=值;
方式三:select @用户变量名:=值;
- 赋值(更新用户变量的值)
方式一:通过set或select
方式一:
set @用户变量名=值;
方式二:set @用户变量名:=值;
方式三:select @用户变量名:=值;
方式二:通过select into
select 字段 into 变量名 from 表;
# 例如:将count(*)求得的值赋值到@count变量中去
select count(*) into @count;
- 使用(查看用户变量的值)
select @用户变量名;
3.2 局部变量
作用域:仅仅在定义它的begin end中有效,应用在begin end的第一句话
- 声明
方式一:
declare 变量名 类型;
方式二:declare 变量名 类型 default 值;
- 赋值
方式一:
方式一:
set 局部变量名=值;
方式二:set 局部变量名:=值;
方式三:select 局部变量名:=值;
方式二:通过select into
select 字段 into 局部变量名 from 表;
- 使用
select 局部变量名;
边栏推荐
- MySQL advanced SQL statement of database (2)
- MIT-6874-Deep Learning in the Life Sciences Week4
- GD32 RT-Thread flash驱动函数
- UAV project tracking record 83 -- PCB diagram completion
- Magnetic levitation 3D lamp
- 孙安民作品《莲花净心》数字藏品上线长城数艺
- Koreano essential creates a professional style
- 那个程序员,被打了。
- Dyson design award, changing the world with sustainable design
- Harvester ch1 of CKB and HNS, connection tutorial analysis
猜你喜欢

Appium automation test foundation - 12 Introduction to appium automated testing framework

MySQL log management, backup and recovery of databases (2)

2022第六季完美童模 合肥赛区 初赛圆满落幕

MIT-6874-Deep Learning in the Life Sciences Week6

Quick completion guide for mechanical arm (V): end effector

SolidWorks质量特性详解(惯性张量、转动惯量、惯性主轴)

The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art

The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded

KOREANO ESSENTIAL打造气质职场范

开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
随机推荐
[ark UI] implementation of the startup page of harmoniyos ETS
Detailed explanation of commissioning methods and techniques
unable to convert expression into double array
MySQL advanced SQL statement of database (1)
IPhone address book import into Excel
Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
Some domestic image sources
MySQL index, transaction and storage engine of database (2)
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.
keras ‘InputLayer‘ object is not iterable
100个句子记完7000个雅思词汇,实际只有1043个词汇(包括 I and you 等简单词汇)
Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Tooltips in the era of touch
Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】
华南产业集团发力数字经济,城链科技发布会成功召开
How to seize the opportunity of NFT's "chaos"?
