我的MYSQL学习心得(十一)
我的MYSQL学习心得(一)
我的MYSQL学习心得(二)
我的MYSQL学习心得(三)
我的MYSQL学习心得(四)
我的MYSQL学习心得(五)
我的MYSQL学习心得(六)
我的MYSQL学习心得(七)
我的MYSQL学习心得(八)
我的MYSQL学习心得(九)
我的MYSQL学习心得(十)
这一篇《我的MYSQL学习心得(十一)》将会讲解MYSQL的视图
使用视图的理由是什么?
1、安全性:一般是这样做的:创建一个视图,定义好该视图所操作的数据。
之后将用户权限与视图绑定,这样的方式是使用到了一个特性:grant语句可以针对视图进行授予权限。
2、查询性能提高
3、有灵活性的功能需求后,需要改动表的结构而导致工作量比较大,那么可以使用虚拟表的形式达到少修改的效果。
这是在实际开发中比较有用的
4、复杂的查询需求,可以进行问题分解,然后将创建多个视图获取数据。将视图联合起来就能得到需要的结果了。
创建视图
创建视图的语法
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION]
其中,CREATE:表示新建视图;
REPLACE:表示替换已有视图
ALGORITHM :表示视图选择算法
view_name :视图名
column_list:属性列
select_statement:表示select语句
[WITH [CASCADED | LOCAL] CHECK OPTION]参数表示视图在更新时保证在视图的权限范围之内
可选的ALGORITHM子句是对标准SQL的MySQL扩展。
ALGORITHM可取三个值:MERGE、TEMPTABLE或UNDEFINED。
如果没有ALGORITHM子句,默认算法是UNDEFINED(未定义的)。算法会影响MySQL处理视图的方式。
对于MERGE,会将引用视图的语句的文本与视图定义合并起来,使得视图定义的某一部分取代语句的对应部分。
对于TEMPTABLE,视图的结果将被置于临时表中,然后使用它执行语句。
对于UNDEFINED,MySQL自己选择所要使用的算法。如果可能,它倾向于MERGE而不是TEMPTABLE,
这是因为MERGE通常更有效,而且如果使用了临时表,视图是不可更新的。
LOCAL和CASCADED为可选参数,决定了检查测试的范围,默认值为CASCADED。
脚本 视图的数据来自于两个表
CREATE TABLE student (stuno INT ,stuname NVARCHAR(60))CREATE TABLE stuinfo (stuno INT ,class NVARCHAR(60),city NVARCHAR(60))INSERT INTO student VALUES(1,'wanglin'),(2,'gaoli'),(3,'zhanghai')INSERT INTO stuinfo VALUES(1,'wuban','henan'),(2,'liuban','hebei'),(3,'qiban','shandong')-- 创建视图CREATE VIEW stu_class(id,NAME,glass) AS SELECT student.`stuno`,student.`stuname`,stuinfo.`class`FROM student ,stuinfo WHERE student.`stuno`=stuinfo.`stuno`SELECT * FROM stu_class
查看视图
查看视图必须要有SHOW VIEW权限
查看视图的方法包括:DESCRIBE、SHOW TABLE STATUS、SHOW CREATE VIEW
DESCRIBE查看视图基本信息
DESCRIBE 视图名
DESCRIBE stu_class
结果显示了视图的字段定义、字段的数据类型、是否为空、是否为主/外键、默认值和额外信息
DESCRIBE一般都简写成DESC
SHOW TABLE STATUS语句查看查看视图基本信息
查看视图的信息可以通过SHOW TABLE STATUS的方法
SHOW TABLE STATUS LIKE 'stu_class'
Name Engine Version Row_format Rows Avg_row_length Data_length Max_data_length Index_length Data_free Auto_increment Create_time Update_time Check_time Collation Checksum Create_options Comment--------- ------ ------- ---------- ------ -------------- ----------- --------------- ------------ --------- -------------- ----------- ----------- ---------- --------- -------- -------------- -------stu_class (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) (NULL) VIEW
COMMENT的值为VIEW说明该表为视图,其他的信息为NULL说明这是一个虚表,如果是基表那么会基表的信息,这是基表和视图的区别
SHOW CREATE VIEW语句查看视图详细信息
SHOW CREATE VIEW stu_class
View Create View character_set_client collation_connection--------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------- --------------------stu_class CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class` AS select `student`.`stuno` AS `id`,`student`.`stuname` AS `name`,`stuinfo`.`class` AS `class` from (`student` join `stuinfo`) where (`student`.`stuno` = `stuinfo`.`stuno`) utf8 utf8_general_ci
执行结果显示视图的名称、创建视图的语句等信息
在VIEWS表中查看视图的详细信息
在MYSQL中,INFORMATION_SCHEMA VIEWS表存储了关于数据库中的视图的信息
通过对VIEWS表的查询可以查看数据库中所有视图的详细信息
SELECT * FROM `information_schema`.`VIEWS`
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION IS_UPDATABLE DEFINER SECURITY_TYPE CHARACTER_SET_CLIENT COLLATION_CONNECTION------------- ------------ ---------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------ ------------ -------------- ------------- -------------------- --------------------def school stu_class select `school`.`student`.`stuno` AS `id`,`school`.`student`.`stuname` AS `name`,`school`.`stuinfo`.`class` AS `class` from `school`.`student` join `school`.`stuinfo` where (`school`.`student`.`stuno` = `school`.`stuinfo`.`stuno`) NONE YES root@localhost DEFINER utf8 utf8_general_ci
当前实例下只有一个视图stu_class
修改视图
修改视图是指修改数据库中存在的视图,当基本表的某些字段发生变化时,可以通过修改视图来保持与基本表的一致性。
MYSQL中通过CREATE OR REPLACE VIEW 语句和ALTER语句来修改视图
语法如下:
ALTER OR REPLACE [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]VIEW view_name [(column_list)]AS select_statement[WITH [CASCADED | LOCAL] CHECK OPTION]
该语句用于更改已有视图的定义。其语法与CREATE VIEW类似。当视图不存在时创建,存在时进行修改
修改视图
DELIMITER $$CREATE OR REPLACE VIEW `stu_class` AS SELECT `student`.`stuno` AS `id`FROM (`student` JOIN `stuinfo`)WHERE (`student`.`stuno` = `stuinfo`.`stuno`)$$DELIMITER ;
通过DESC来查看更改之后的视图定义
DESC stu_class
可以看到只查询一个字段
ALTER语句修改视图
ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]VIEW view_name [(column_list)]AS select_statement[WITH [CASCADED | LOCAL] CHECK OPTION]
这里关键字跟前面的一样,这里不做介绍
使用ALTER语句修改视图 stu_class
ALTER VIEW stu_class AS SELECT stuno FROM student;
使用DESC查看
DESC stu_class
更新视图
更新视图是指通过视图来插入、更新、删除表数据,因为视图是虚表,其中没有数据。
通过视图更新的时候都是转到基表进行更新,如果对视图增加或者删除记录,实际上是对基表增加或删除记录
我们先修改一下视图定义
ALTER VIEW stu_class AS SELECT stuno,stuname FROM student;
查询视图数据
UPDATE
UPDATE stu_class SET stuname='xiaofang' WHERE stuno=2
查询视图数据
更新成功
INSERT
INSERT INTO stu_class VALUES(6,'haojie')
插入成功
DELETE
DELETE FROM stu_class WHERE stuno=1
删除成功
当视图中包含如下内容的时候,视图的更新操作将不能被执行
(1)视图中包含基本中被定义为非空的列
(2)定义视图的SELECT语句后的字段列表中使用了数学表达式
(3)定义视图的SELECT语句后的字段列表中使用聚合函数
(4)定义视图的SELECT语句中使用了DISTINCT、UNION、TOP、GROUP BY 、HAVING子句
删除视图
删除视图使用DROP VIEW语法
DROP VIEW [IF EXISTS]view_name [, view_name] ...[RESTRICT | CASCADE]
DROP VIEW能够删除1个或多个视图。必须在每个视图上拥有DROP权限
可以使用关键字IF EXISTS来防止因不存在的视图而出错
删除stu_class视图
DROP VIEW IF EXISTS stu_class
如果名称为 stu_class 的视图存在则删除
使用SHOW CREATE VIEW语句查看结果
SHOW CREATE VIEW stu_class
Query: -- update stu_class set stuname='xiaofang' where stuno=2; -- delete from stu_class where stuno=1 -- select * from stu_class; -- ...Error Code: 1146Table 'school.stu_class' doesn't existExecution Time : 0 secTransfer Time : 0 secTotal Time : 0.004 sec---------------------------------------------------
该视图不存在,删除成功
总结
SQLSERVER里实际上跟MYSQL一样,也是有信息架构视图的
信息架构视图 (Transact-SQL)
信息架构视图是 SQL Server 提供的几种获取元数据的方法之一。
信息架构视图提供独立于系统表的内部 SQL Server 元数据视图。
尽管已经对基础系统表进行了重要的修改,信息架构视图仍然可使应用程序正常工作。
SQL Server 中包含的信息架构视图符合 ISO 标准中的信息架构定义。
信息架构视图的数据是存放在系统数据库Resource 数据库里面
mssqlsystemresource.mdf
利用INFORMATION_SCHEMA视图来拼接 SQL 语句
如有不对的地方,欢迎大家拍砖o(∩_∩)o

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),