Table of Contents
view视图
一、视图定义
二、创建视图
三、视图与表的关系
Home Database Mysql Tutorial 小贝_mysqlview视图学习_MySQL

小贝_mysqlview视图学习_MySQL

Jun 01, 2016 pm 12:59 PM
view

view视图

简要:
1、视图定义
2、创建视图
3、视图与表的关系
4、视图种类

一、视图定义

视图起因: 查询每个栏目最贵的商品=>

selectgoods_id,goods_name,shop_price from goods order by cat_id asc,shop_price desc; 查询结果,当成一张表看

如果某个查询结果出现的非常频繁,也就是说,拿这个结果进行子查询出现的非常频繁.

createtable g2 like goods;insert into g2 select ….

上面两句,是想保存一个查询结果到表里面,供其它查询用。

视图定义: 视图是由查询结果形成的一张虚拟表。

 

二、创建视图

mysql>CREATE TABLE t (qty INT, price INT);

mysql>INSERT INTO t VALUES(3, 50);

mysql>CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;

mysql>SELECT * FROM v;

 

为什么需要视图?

a、简化查询语句

b、可以控制列权限

把表的权限封闭,但是开放相对应的视图权限。视图里只开放部分列部分数据

c、大数据分表时可以用视图把所分的表形成一张视图

 

三、视图与表的关系

测试时用到的表结构及数据

create table t

(id int primary keyauto_increment,

type tinyint unsigned,

name char(5) not nulldefault '',

money tinyint unsigned

)engine=myisamcharset=utf8;

insert intot(type,name,money) values(1,'1a',4),

(1,'1b',3),(2,'2a',3),(1,'1c',4),(2,'2b',5),(3,'3a',5);

问题1: 视图是表的查询结果,因此表的数据改变了,会影响视图的结果

\

\

修改原始表t的id为6的数据

\

查看视图:

 

\

问题2: 视图改变了呢?

情况一:视图的数据与表的数据一一对应,如上述例子中的表t与视图t_view1

1、修改视图t_view1的id为6的数据

 

\

查看原始表t

\

2、往视图t_view1插入数据

\

查看原始表t

\

3、删除视图t_view1id为7的数据

\

查看原始表t

\

情况2: 视图只是原始表的统计结果,并不与原始表数据一一对应

(备注: 创建统计t表相同type的money字段总和的视图)

\

试着修改下视图t_view2的数据

\

假设可以进行修改,那么type为1的money总和加上3后,怎么对应原始表各个type为1的数据。显然,这是没办法的。因此报错

从上面这两种情况,得到:

1、视图增删改也会影响表,但是,视图并不总能增删改

2、视图的数据与表的数据一一对应时,则可以

3、对于视图insert还应注意,视图必须包含表中没有默认值的列

四、视图的种类

4.1、查看视图创建语法

\

 

针对algorithm的用法:

a、undefined: 由系统默认

b、merge:当algorithm为此值时,创建的视图只是单纯的sql语句存在而不是以虚拟结果表的形式存在。但使用此视图时,创建视图的sql语句会与后面的sql语句进行优化,最后得到一条新的sql语句

以merge视图的方式,求得表t中type相同的money最大值

 

\

再对视图t_view3进行分组

\

显然,这个结果不是我们想要的结果。运行 上述sql语句等同于

\

 

这是因为merge不是以虚拟结果表存在,而仅仅是sql语句,因此会与后面的sql语句进行优化后,再进行执行。

c、temptable:会把视图当做虚拟结果表

以temptable视图的方式,求得表t中type相同的money最大值

\

再进行对type进行分组

\

 

从上面这两种情况,得到:

创建视图建议指定视图的类型为临时表

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement editable tables in Vue How to implement editable tables in Vue Nov 08, 2023 pm 12:51 PM

Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

iOS 17's standby mode turns a charging iPhone into a home hub iOS 17's standby mode turns a charging iPhone into a home hub Jun 06, 2023 am 08:20 AM

In iOS 17 Apple is introducing Standby Mode, a new display experience designed for charging iPhones in a horizontal orientation. In this position, the iPhone is able to display a series of full-screen widgets, turning it into a useful home hub. Standby mode automatically activates on an iPhone running iOS 17 placed horizontally on the charger. You can view time, weather, calendar, music controls, photos, and more. You can swipe left or right through the available standby options and then long press or swipe up/down to customize. For example, you can choose from analog view, digital view, bubble font, and daylight view, where the background color changes based on time as time passes. There are some options

Understand the differences and comparisons between SpringBoot and SpringMVC Understand the differences and comparisons between SpringBoot and SpringMVC Dec 29, 2023 am 09:20 AM

Compare SpringBoot and SpringMVC and understand their differences. With the continuous development of Java development, the Spring framework has become the first choice for many developers and enterprises. In the Spring ecosystem, SpringBoot and SpringMVC are two very important components. Although they are both based on the Spring framework, there are some differences in functions and usage. This article will focus on comparing SpringBoot and Spring

How to use CodeIgniter4 framework in php? How to use CodeIgniter4 framework in php? May 31, 2023 pm 02:51 PM

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Laravel development: How to generate views using Laravel View? Laravel development: How to generate views using Laravel View? Jun 14, 2023 pm 03:28 PM

Laravel is one of the most popular PHP frameworks currently, and its powerful view generation capabilities are impressive. A view is a page or visual element displayed to the user in a web application, which contains code such as HTML, CSS, and JavaScript. LaravelView allows developers to use a structured template language to build web pages and generate corresponding views through controllers and routing. In this article, we will explore how to generate views using LaravelView. 1. What

What are the views in Word? What are the views in Word? Mar 19, 2024 pm 06:10 PM

I guess that many students want to learn the typesetting skills of Word, but the editor secretly tells you that before learning the typesetting skills, you need to understand the word views clearly. In Word2007, 5 views are provided for users to choose. These 5 views include pages. View, reading layout view, web layout view, outline view and normal view, let’s learn about these 5 word views with the editor today. 1. Page view Page view can display the appearance of the print result of the Word2007 document, which mainly includes headers, footers, graphic objects, column settings, page margins and other elements. It is the page view closest to the print result. 2. Reading layout view Reading layout view displays Word2007 documents and Office in the column style of a book

Detailed explanation of views in Django framework Detailed explanation of views in Django framework Jun 17, 2023 am 10:18 AM

Django is a highly customizable web framework that provides many convenient tools and libraries to help developers quickly create high-performance, scalable web applications. Among them, views are one of the most important components of the Django framework. Views are responsible for processing requests from clients and returning corresponding responses. In this article, we'll take a deep dive into views in the Django framework and explain how to use it to create high-performance, customizable web applications. 1. The basic concept of views in Django

Microsoft updates Visual Studio Code 1.80 Microsoft updates Visual Studio Code 1.80 Jul 10, 2023 pm 08:13 PM

Microsoft recently launched Visual Studio Code 1.80. Although this update was released in July, the official still calls it the June update. After users install this update, the built-in terminal can directly display images, and auxiliary functions have also been optimized. In the previously released preview version of Visual Studio Code, support for images in the terminal has been supported, and it is enabled by default in the new 1.80 version. In order to be able to display the image in the terminal, the image pixel data is converted into text through a special escape sequence and finally written to the terminal. If you want to pipe typical PNG, GIF or JPEG files to the terminal, you need to install the imgcatpython package and then run imgc in the terminal

See all articles