当前位置:网站首页>MySQL高级_视图
MySQL高级_视图
2022-07-29 11:30:00 【清风不是明月】
一:视图相对于普通的表的优势主要包括以下几项。
- 简单:使用视图的用户完全不需要关心后面对应的表的结构、关联条件和筛选条件,对用户来说已经是过滤好的复合条件的结果集。
- 安全:使用视图的用户只能访问他们被允许查询的结果集,对表的权限管理并不能限制到某个行某个列,但是通过视图就可以简单的实现。
- 数据独立:一旦视图的结构确定了,可以屏蔽表结构变化对用户的影响,源表增加列对视图没有影响;源表修改列名,则可以通过修改视图来解决,不会造成对访问者的影响。
二:视图语法
1)创建视图
-- 语法
CREATE [OR REPLACE] [ALGORITHM = {
UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
--选项 :
-- WITH [CASCADED | LOCAL] CHECK OPTION 决定了是否允许更新数据使记录不再满足视图的条件。
-- LOCAL : 只要满足本视图的条件就可以更新。
-- CASCADED : 必须满足所有针对该视图的所有视图的条件才可以更新。 这个是默认值.
-- 语法说明
-- CREATE:创建视图
-- CREATE OR REPLACE:创建并替换
-- view_name:视图名称
-- column_list:一个或多个字段名
-- select_statement:select语句
-- 举例(创建名为view_city_country 的视图,AS 后为普通的select 语句)
CREATE VIEW view_city_country AS
SELECT
c.*,
t.country_name
FROM city c
JOIN country t ON c.country_id = t.country_id
2)更新视图(视图可以更新,但是不建议更新视图)
-- 注意:如果更新视图,更新的是基础表的数据
-- 方法一:更新视图举例(和普通sql语句一样)
update view_city_country set city_name ='西安市' where city_id = 1
--方法二:
ALTER [ALGORITHM = {
UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
3)查看视图
从 MySQL 5.1 版本开始,使用 SHOW TABLES 命令的时候不仅显示表的名字,同时也会显示视图的名字,而不存在单独显示视图的 SHOW VIEWS 命令。
4)删除视图
-- 语法:
DROP VIEW [IF EXISTS] view_name [, view_name] ...[RESTRICT | CASCADE]
-- 举例
drop view view_city_country
边栏推荐
- 2022最新 wifi大师小程序独立版3.0.8
- ES6 arrow function this points to
- mysql单行,多行子查询
- 从零开始Blazor Server(3)--添加cookie授权
- 迁徙数据平台简单介绍
- Talk about the establishment of performance testing environment
- matplotlib中文问题
- 如何开始为您的 Kubernetes 应用程序编写 Helm 图表
- PaddleLite 编译以及代码跑通复盘
- INVALID_ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
猜你喜欢

游戏合作伙伴专题:BreederDAO 与《王国联盟》结成联盟

TCP and UDP

开源峰会抢先看 | 7月29日分论坛&活动议程速览

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

牛客网刷题

谷歌“消灭” Cookie 计划延至 2024 年

幸运抽奖系统带后台源码

Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition

AI全流程开发难题破解之钥

Starrocks technology insider: how to have both real-time update and fast query
随机推荐
ES6-箭头函数this指向
Spark efficient data analysis 01. Establishment of idea development environment
PaddleLite 编译以及代码跑通复盘
leetcode-位运算
Gbase8s core data backup
Peking University open classes are coming! Welcome to the "AI for science" class
"Knowledge Collection" article to understand mysql index!!(recommended collection)
Learning with Recoverable Forgetting阅读心得
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
Mall mall based on flask --- user module
Lucky draw system with background source code
Self collection online computer wallpaper PHP source code v2.0 adaptive end
Package delivery (greedy)
Starrocks technology insider: how to have both real-time update and fast query
LeetCode_278_第一个错误的版本
【Untitled】
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
一键搭建博客:如何使用WordPress插件搭建专属博客
Talk about the establishment of performance testing environment
8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice