MySQL Quick Query-View

MySQL quick check

Because I often forget some mysql statements, keywords, operations, etc. in my daily work and study, I recently took some time to write the following content about mysql. It's like a dictionary


Reset mysql password
Data type
operator
Common functions
Data integrity
Basic operations of the database
Operations on the table itself Operations
on data in the table
Subquery
Multi-table connection
index
This article
preprocesses SQL statements
Custom functions and stored procedures
Programming in MySQL


Introduction

A view is a virtual table derived from one or more tables or other views through a select statement. No data is stored in the view. The data obtained from the view comes from the tables it references.

Table used by the example

mysql> create table people(id int primary key auto_increment,
	name char(10) not null, 
	age int not null,sex enum('f','m'),sar float);
Query OK, 0 rows affected (0.02 sec)


mysql> desc people;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int           | NO   | PRI | NULL    | auto_increment |
| name  | char(10)      | NO   |     | NULL    |                |
| age   | int           | NO   |     | NULL    |                |
| sex   | enum('f','m') | YES  |     | NULL    |                |
| sar   | float         | YES  |     | NULL    |                |
+-------+---------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

Data from the table used

mysql> insert into people(name,age,sex,sar) values('铁子',23,'m',20000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into people(name,age,sex,sar) values('潘子',18,'m',5000.4);
Query OK, 1 row affected (0.01 sec)

mysql> insert into people(name,age,sex,sar) values('嘎子',18,'m',5400.4);
Query OK, 1 row affected (0.01 sec)

mysql> insert into people(name,age,sex,sar) values('嘎叔',60,'m',15400.4);
Query OK, 1 row affected (0.01 sec)

mysql> insert into people(name,age,sex,sar) values('潘叔',60,'m',15450.8);
Query OK, 1 row affected (0.01 sec)

mysql> insert into people(name,age,sex,sar) values('老同志',69,'m',15450.8);
Query OK, 1 row affected (0.01 sec)

mysql> select * from people;
+----+-----------+-----+------+---------+
| id | name      | age | sex  | sar     |
+----+-----------+-----+------+---------+
|  1 | 铁子      |  23 | m    |   20000 |
|  2 | 潘子      |  18 | m    |  5000.4 |
|  3 | 嘎子      |  18 | m    |  5400.4 |
|  4 | 嘎叔      |  60 | m    | 15400.4 |
|  5 | 潘叔      |  60 | m    | 15450.8 |
|  6 | 老同志    |  69 | m    | 15450.8 |
+----+-----------+-----+------+---------+
6 rows in set (0.00 sec)

Create view

CREATE
    [OR REPLACE]
    [ALGORITHM = {
   
   UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = 用户]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW 视图名 [(视图字段名,...)]
    AS 查询语句
    [WITH [CASCADED | LOCAL] CHECK OPTION]
  • create view: Create a view, add or replace to replace the existing view
  • algorithm: view algorithm:
    • undefined: MySQL will automatically select the algorithm
    • merge: merge the view definition and the view statement so that a certain part of the view definition replaces the corresponding part of the statement
    • temptable: Store the view results in a temporary table, and then use the temporary table to execute statements
    • definer: Definer, indicates the user who created the view, if not written, the default is the current user
    • sql security: Specify who has permission to execute:
      • definer: Execute with the permissions owned by the definer, default value
      • invoker: Execute with the caller's permissions
  • with check option: used to limit the insertion or update of rows in the table referenced by the view. If you use columns from other sources in a select statement, you must have select statement permissions. If there is or replace, you also need drop permissions.
  • cascaded: The default value, which means that the conditions of all related views and tables must be met when updating the view.
  • local: means that when updating the view, it only needs to meet the definition of the view itself.
# 创建一个视图将表people中年龄大于等于60的人
mysql> create or replace view v_old_people (姓名,年龄,性别)
	as select name,age,sex from people where age >= 60;
Query OK, 0 rows affected (0.00 sec)

More

Delete view

DROP VIEW [IF EXISTS]
    视图名 [, 视图名] ...


# 删除视图v_old_people
mysql> drop view v_old_people;
Query OK, 0 rows affected (0.01 sec)

More

Modify view

ALTER
    [ALGORITHM = {
   
   UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = user]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

Explanation Same as Create
ViewMore

view view

View definition

SHOW CREATE VIEW 视图名;

show table status like '视图名';



mysql> SHOW CREATE VIEW v_old_people;
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View         | Create View                                                                                                                                                                                                                                                | character_set_client | collation_connection |
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| v_old_people | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_old_people` (`姓名`,`年龄`,`性别`) AS select `people`.`name` AS `name`,`people`.`age` AS `age`,`people`.`sex` AS `sex` from `people` where (`people`.`age` >= 60)       | utf8mb4              | utf8mb4_0900_ai_ci   |
+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)


mysql> show table status like 'v_old_people';
+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| 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 |
+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| v_old_people | NULL   |    NULL | NULL       | NULL |           NULL |        NULL |            NULL |         NULL |      NULL |           NULL | 2021-05-27 14:45:52 | NULL        | NULL       | NULL      |     NULL | NULL           | VIEW    |
+--------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
1 row in set (0.00 sec)

View data

select 字段名[,...] from 视图名;

mysql> select * from v_old_people;
+-----------+--------+--------+
| 姓名      | 年龄   | 性别   |
+-----------+--------+--------+
| 嘎叔      |     60 | m      |
| 潘叔      |     60 | m      |
| 老同志    |     69 | m      |
+-----------+--------+--------+
3 rows in set (0.00 sec)

More

Guess you like

Origin blog.csdn.net/weixin_45345384/article/details/117326786