当前位置:网站首页>Database composition view
Database composition view
2022-07-26 12:33:00 【Hua Weiyun】
15.1 View overview
MySQL from 5.0 The version began to support views . View can facilitate developers to add data 、 Delete 、 Change 、 Check the operations such as . More Than This , Access view can restrict users' direct access to data tables in the database according to the corresponding permissions , Up to a point , It can ensure the security of the database .
15.1.1 Concept of view
Views can be generated from one or more tables in the database , Similar to data table in structure , But the view is essentially a virtual table , The data in the view is also composed of the data in one table or multiple tables . You can add data in the view 、 Delete 、 modify 、 Check and so on , You can also modify the structure of the view .
In the database , The view does not save data , The data is really saved in the data table . When adding data to the view 、 When deleting and modifying operations , The data in the data table will change accordingly ; vice versa . in other words , Whether the data in the view changes , Or the data in the data table changes , The data of the other party will change accordingly .
15.1.2 Benefits of views
Using views in a database has many advantages , Here are some advantages of using views over using data tables .
1. It's easy to operate
Define frequently used query operations as views , It can make developers do not need to care about the structure of the data table corresponding to the view 、 The relationship between tables , There is no need to care about the business logic and query conditions between data tables , Simply manipulate the view , It greatly simplifies the operation of developers on the database .
2. Data security
MySQL Restrict users' access to data to the result set of some data according to permissions , The result set of these data can be realized by using views . therefore , Users' access to data can be restricted to some views according to their permissions , Instead of directly querying or manipulating the data table , This ensures the security of the data in the data table to a certain extent .
3. Data independence
After the view is created , The structure of the view is determined , When the structure of the data table changes, it will not affect the structure of the view . When the field name of the data table changes , Simply modify the query statement of the view , It will not affect the user's query operation of data .
4. Adapt to flexible needs
When the requirements of the business system change , If you need to change the structure of the data table , The workload is relatively large , You can use views to reduce the amount of change . This method is often used in practical work .
5. Ability to decompose complex query logic
If there is complex query logic in the database , Then the problem can be decomposed , Create multiple views to get data , Then combine the created multiple views , Complete complex query logic .
15.2 Create view
MySQL Use in CREATE VIEW Statement to create a view . This section briefly introduces how to MySQL Create a view in .
Be careful : The test table used in this chapter is 8 Created in chapter t_goods Data sheets and t_goods_category Data sheet .
15.2.1 Grammar format
MySQL Can be used in CREATE VIEW Statement to create a view , The syntax format of creating a view is as follows :
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = user] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION] The syntax format is described as follows :
·CREATE: New view .
·REPLACE: Replace the existing view .
·ALGORITHM: Identify the algorithm used by the view .
·{UNDEFINED | MERGE | TEMPTABLE}: The algorithm used by the view . among ,UNDEFINED Express MySQL The algorithm will be automatically selected ;MERGE Indicates that the statement referencing the view is merged with the view definition ;TEMPTABLE Indicates that the results of the view are placed in a temporary table , Next, use the temporary table to execute the corresponding SQL sentence .
·DEFINER: The user who defines the view .
·SQL SECURITY: Security level .DEFINER It means that only the user who created the view can access the view ;INVOKER It means that users with corresponding permissions can access the view .
·view_name: Name of the created view .
·column_list: List of field names contained in the view .
·select_statement:SELECT sentence .
·[WITH [CASCADED | LOCAL] CHECK OPTION]: Ensure that the view is updated within the permission of the view .
15.2.2 Create a single table view
for example , be based on t_goods Create a data table named view_name_price The view of , The fields in the view only contain t_goods In the data sheet t_name Fields and t_price Field .
mysql> CREATE VIEW view_name_price -> AS -> SELECT t_name, t_price FROM t_goods; Query OK, 0 rows affected (0.13 sec) Results show SQL Statement executed successfully .
see view_name_price Data in view .
mysql> SELECT * FROM view_name_price; +-----------------+---------+ | t_name | t_price | +-----------------+---------+ | T T-shirt | 39.90 | | dress | 79.90 | | fleece | 79.90 | | A pair of jeans | 89.90 | | Pleated skirt | 29.90 | | Woolen coat | 399.90 | | Bicycle | 399.90 | | Mountain Bike | 1399.90 | | Alpenstocks | 59.90 | | Riding equipment | 399.90 | | Sport coat | 799.90 | | Skate | 499.90 | | grapes | 49.90 | | Banana | 39.90 | +-----------------+---------+ 14 rows in set (0.00 sec) view_name_price The view contains only t_goods Data table t_name Fields and t_price Field , The field name of the query result column is the same as t_goods The field names of the data table are the same .
You can also create views , Include all fields of the data table in the view . for example , Create name as view_all_price The view of , It includes t_goods All fields in the data table .
mysql> CREATE VIEW view_all_price -> AS -> SELECT * FROM t_goods; Query OK, 0 rows affected (0.00 sec) Inquire about view_all_price Data in view .
mysql> SELECT * FROM view_all_price; +----+---------------+-----------------+-------------+---------+---------+---------------------+ | id | t_category_id | t_category | t_name | t_price | t_stock | t_upper_time | +----+---------------+-----------------+-------------+---------+---------+---------------------+ | 1 | 1 | Women's wear / Women's Boutique | T T-shirt | 39.90 | 1000 | 2020-11-10 00:00:00 | | 2 | 1 | Women's wear / Women's Boutique | dress | 79.90 | 2500 | 2020-11-10 00:00:00 | | 3 | 1 | Women's wear / Women's Boutique | fleece | 79.90 | 1500 | 2020-11-10 00:00:00 | | 4 | 1 | Women's wear / Women's Boutique | A pair of jeans | 89.90 | 3500 | 2020-11-10 00:00:00 | | 5 | 1 | Women's wear / Women's Boutique | Pleated skirt | 29.90 | 500 | 2020-11-10 00:00:00 | | 6 | 1 | Women's wear / Women's Boutique | Woolen coat | 399.90 | 1200 | 2020-11-10 00:00:00 | | 7 | 2 | Outdoor sports | Bicycle | 399.90 | 1000 | 2020-11-10 00:00:00 | | 8 | 2 | Outdoor sports | Mountain Bike | 1399.90 | 2500 | 2020-11-10 00:00:00 | | 9 | 2 | Outdoor sports | Alpenstocks | 59.90 | 1500 | 2020-11-10 00:00:00 | | 10 | 2 | Outdoor sports | Riding equipment | 399.90 | 3500 | 2020-11-10 00:00:00 | | 11 | 2 | Outdoor sports | Sport coat | 799.90 | 500 | 2020-11-10 00:00:00 | | 12 | 2 | Outdoor sports | Skate | 499.90 | 1200 | 2020-11-10 00:00:00 | | 13 | 5 | Fruits | grapes | 49.90 | 500 | 2020-11-10 00:00:00 | | 14 | 5 | Fruits | Banana | 39.90 | 1200 | 2020-11-10 00:00:00 | +----+---------------+-----------------+-------------+---------+---------+---------------------+ 14 rows in set (0.00 sec) view_all_price The structure of the view is similar to t_goods The data table structure is the same , Also include t_goods All data in the data sheet .
By default , The field name of the created view is the same as that of the data table , You can also specify a field name for the view when you create it . For example, create a file named view_name_price_tag The view of , And specify the names of the two fields in the view as name and price.
mysql> CREATE VIEW view_name_price_tag -> (name, price) -> AS -> SELECT t_name, t_price FROM t_goods; Query OK, 0 rows affected (0.00 sec) Inquire about view_name_price_tag Data in view .
mysql> SELECT * FROM view_name_price_tag; +-------------+---------+ | name | price | +-------------+---------+ | T T-shirt | 39.90 | | dress | 79.90 | | fleece | 79.90 | | A pair of jeans | 89.90 | | Pleated skirt | 29.90 | | Woolen coat | 399.90 | | Bicycle | 399.90 | | Mountain Bike | 1399.90 | | Alpenstocks | 59.90 | | Riding equipment | 399.90 | | Sport coat | 799.90 | | Skate | 499.90 | | grapes | 49.90 | | Banana | 39.90 | +-------------+---------+ 14 rows in set (0.00 sec) The name of the query result column is the field name specified for the view when creating the view .
MySQL It supports creating views as SELECT Statement to set query conditions , When setting query criteria , Only data that meets the query criteria can appear in the view . for example , Create name as view_goods_consition And specify the query criteria .
mysql> CREATE VIEW view_goods_consition -> (name, price) -> AS -> SELECT t_name, t_price FROM t_goods -> WHERE id = 1; Query OK, 0 rows affected (0.01 sec) When creating a view , by SELECT The query condition specified by the statement is id=1, here , The view will only contain t_goods In the data table id The value is 1 The data of . see view_goods_consition Data in view .
mysql> SELECT * FROM view_goods_consition; +-------------+---------+ | name | price | +-------------+---------+ | T T-shirt | 39.90 | +-------------+---------+ 1 row in set (0.00 sec) view_goods_consition The view contains only id by 1 The name and price of the goods .
15.2.3 Create a multi table union view
MySQL Support the creation of joint views on multiple data tables , for example , stay t_goods_category Data sheets and t_goods Create a data table named view_category_goods The view of .
mysql> CREATE VIEW view_category_goods -> (category, name, price) -> AS -> SELECT category.t_category, goods.t_name, goods.t_price -> FROM t_goods_category category, t_goods goods -> WHERE category.id = goods.t_category_id; Query OK, 0 rows affected (0.00 sec) see view_category_goods Data in view .
mysql> SELECT * FROM view_category_goods; +-----------------+-------------+---------+ | category | name | price | +-----------------+-------------+---------+ | Women's wear / Women's Boutique | T T-shirt | 39.90 | | Women's wear / Women's Boutique | dress | 79.90 | | Women's wear / Women's Boutique | fleece | 79.90 | | Women's wear / Women's Boutique | A pair of jeans | 89.90 | | Women's wear / Women's Boutique | Pleated skirt | 29.90 | | Women's wear / Women's Boutique | Woolen coat | 399.90 | | Outdoor sports | Bicycle | 399.90 | | Outdoor sports | Mountain Bike | 1399.90 | | Outdoor sports | Alpenstocks | 59.90 | | Outdoor sports | Riding equipment | 399.90 | | Outdoor sports | Sport coat | 799.90 | | Outdoor sports | Skate | 499.90 | +-----------------+-------------+---------+ 12 rows in set (0.00 sec) stay view_category_goods In the view ,caregory The data in the field is from t_goods_category Obtained from the data table ,name Fields and price The data in the field is from t_goods Obtained from the data table .
You can also use JOIN Statement to associate multiple tables . For example, create a file named view_category_join_goods The view of , Structure and view_category_goods Same view .
mysql> CREATE VIEW view_category_join_goods -> (category, name, price) -> AS -> SELECT category.t_category, goods.t_name, goods.t_price -> FROM t_goods_category category -> INNER JOIN t_goods goods -> ON category.id = goods.t_category_id; Query OK, 0 rows affected (0.01 sec) see view_category_join_goods Data in view .
mysql> SELECT * FROM view_category_join_goods; +-----------------+-------------+---------+ | category | name | price | +-----------------+-------------+---------+ | Women's wear / Women's Boutique | T T-shirt | 39.90 | | Women's wear / Women's Boutique | dress | 79.90 | | Women's wear / Women's Boutique | fleece | 79.90 | | Women's wear / Women's Boutique | A pair of jeans | 89.90 | | Women's wear / Women's Boutique | Pleated skirt | 29.90 | | Women's wear / Women's Boutique | Woolen coat | 399.90 | | Outdoor sports | Bicycle | 399.90 | | Outdoor sports | Mountain Bike | 1399.90 | | Outdoor sports | Alpenstocks | 59.90 | | Outdoor sports | Riding equipment | 399.90 | | Outdoor sports | Sport coat | 799.90 | | Outdoor sports | Skate | 499.90 | +-----------------+-------------+---------+ 12 rows in set (0.00 sec) view_category_join_goods The structure and data of the view are similar to view_category_goods The structure of the view is the same as the data .
15.3 View view
MySQL You can use SHOW TABLES sentence 、DESCRIBE/DESC sentence 、SHOW TABLE STATUS Statement and SHOW CREATE VIEW sentence . This section will briefly introduce how to MySQL View in .
15.3.1 Use SHOW TABLES Statement view view
from MySQL 5.1 Version start ,SHOW TABLES Statement can not only display the name of the data table in the current database , It can also display the view name in the current database .
for example , Use SHOW TABLES Statement to view the data table and view under the current database .
mysql> SHOW TABLES; +--------------------------+ | Tables_in_goods | +--------------------------+ | t_goods | | t_goods_category | | vew_all_price | | view_category_goods | | view_category_join_goods | | view_goods_consition | | view_name_price | | view_name_price_tag | +--------------------------+ 8 rows in set (0.00 sec) You can see ,SHOW TABLE Statement displays both the name of the data table and the name of the view in the current database .
Be careful :MySQL The use of... Is not supported in SHOW VIEWS Statement view view , Examples are as follows :
mysql> SHOW VIEWS; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VIEWS' at line 1 Results show , When using SHOW VIEWS Statement when viewing the view ,MySQL Report errors .
15.3.2 Use DESCRIBE/DESC Statement view view
Use DESCRIBE/DESC The syntax format of the statement view is as follows :
DESCRIBE view_name perhaps :
DESC view_name among ,view_name Is the name of the view .
for example , Use DESCRIBE Statement view view_category_goods View information .
mysql> DESCRIBE view_category_goods; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | category | varchar(30) | NO | | | | | name | varchar(50) | YES | | | | | price | decimal(10,2) | YES | | 0.00 | | +----------+---------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) Use DESCRIBE Statement to view the fields in the view , And the data type used by the field , Whether the data in the field is allowed to be NULL, Whether the field is a primary key or a foreign key , Whether there is a default value in the field , Whether there is additional information .
DESC The function of the statement is the same as DESCRIBE Statements have exactly the same effect , Use DESC Statement view view_category_goods View information .
mysql> DESC view_category_goods; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | category | varchar(30) | NO | | | | | name | varchar(50) | YES | | | | | price | decimal(10,2) | YES | | 0.00 | | +----------+---------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) You can see , And DESCRIBE The result information viewed by the statement is exactly the same .
15.3.3 Use SHOW TABLE STATUS Statement view view
Use SHOW TABLE STATUS Statement view view , The syntax is as follows :
SHOW TABLE STATUS LIKE ‘view_name’ among ,view_name Represents the name of the view .
see view_category_goods View information .
mysql> SHOW TABLE STATUS LIKE 'view_category_goods' \G *************************** 1. row *************************** Name: view_category_goods Engine: NULL Version: NULL Row_format: NULL Rows: NULL Avg_row_length: NULL Data_length: NULL Max_data_length: NULL Index_length: NULL Data_free: NULL Auto_increment: NULL Create_time: 2019-12-26 13:38:08 Update_time: NULL Check_time: NULL Collation: NULL Checksum: NULL Create_options: NULL Comment: VIEW 1 row in set (0.00 sec) Comment The value of the property is VIEW, explain view_category_goods For the view , Other information is NULL, The description view is a virtual table . In order to better compare the information in the data table , Next , Use SHOW TABLE STATUS Statement view t_goods_category Information from the data table .
mysql> SHOW TABLE STATUS LIKE 't_goods_category' \G *************************** 1. row *************************** Name: t_goods_category Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 4 Avg_row_length: 4096 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 4 Create_time: 2019-12-20 21:36:46 Update_time: NULL Check_time: NULL Collation: utf8mb4_0900_ai_ci Checksum: NULL Create_options: Comment: 1 row in set (0.00 sec) Use SHOW TABLE STATUS Statement to view the information of data , The storage engine of the data table will be displayed 、 edition 、 Information such as the number of data rows and data size .
15.3.4 Use SHOW CREATE VIEW Statement view view
Use SHOW CREATE VIEW Statement view view , The syntax is as follows :
SHOW CREATE VIEW 'view_name' among ,view_name Is the name of the view .
for example , see view_category_goods View information .
mysql> SHOW CREATE VIEW view_category_goods \G *************************** 1. row *************************** View: view_category_goods Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_category_goods` (`category`,`name`,`price`) AS select `category`.`t_category` AS `t_category`, `goods`.`t_name` AS `t_name`,`goods`.`t_price` AS `t_price` from (`t_goods_category` `category` join `t_goods` `goods`) where (`category`.`id` = `goods`.`t_category_id`) character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci 1 row in set (0.00 sec) You can see , The name of the view and MySQL The underlying execution creates a complete view SQL Statement information .
15.3.5 see views View information in the data table
MySQL The information of the view will be stored in information_schema Database based views In the data table , You can see views Data table to view the information of the view .
mysql> SELECT * FROM information_schema.views \G ################ Omit here n Row data ######################### TABLE_CATALOG: def TABLE_SCHEMA: goods TABLE_NAME: view_category_goods VIEW_DEFINITION: select `category`.`t_category` AS `t_category`,`goods`.`t_name` AS `t_name`, `goods`.`t_price` AS `t_price` from `goods`.`t_goods_category` `category` join `goods`.`t_goods` `goods` where (`category`.`id` = `goods`.`t_category_id`) CHECK_OPTION: NONE IS_UPDATABLE: YES DEFINER: [email protected] SECURITY_TYPE: DEFINER CHARACTER_SET_CLIENT: utf8mb4 COLLATION_CONNECTION: utf8mb4_general_ci *************************** 106. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: goods TABLE_NAME: view_category_join_goods VIEW_DEFINITION: select `category`.`t_category` AS `t_category`,`goods`.`t_name` AS `t_name`, `goods`.`t_price` AS `t_price` from (`goods`.`t_goods_category` `category` join `goods`.`t_goods` `goods` on((`category`.`id` = `goods`.`t_category_id`))) CHECK_OPTION: NONE IS_UPDATABLE: YES DEFINER: [email protected] SECURITY_TYPE: DEFINER CHARACTER_SET_CLIENT: utf8mb4 COLLATION_CONNECTION: utf8mb4_general_ci 106 rows in set (0.00 sec) The results will show information about all views created in the database .
15.4 Modify the structure of the view
MySQL Supported in CREATE OR REPLACE VIEW Statement and ALTER Statement to modify the structural information of the view , This section will briefly introduce how to modify the structural information of the view .
15.4.1 Use CREATE OR REPLACE VIEW Statement to modify the view structure
Use CREATE OR REPLACE VIEW Statement to modify the view structure , The syntax is as follows :
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = user] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION] The syntax format is the same as that of creating views , I won't repeat .
CREATE OR REPLACE VIEW Statement means if the view does not exist , Create view ; Update the view if it exists .
for example , Create name as view_create_replace The view of , The fields contained in the view are t_goods_category In the data sheet id Fields and t_category Field .
mysql> CREATE VIEW view_create_replace -> (id, category) -> AS -> SELECT id, t_category FROM t_goods_category; Query OK, 0 rows affected (0.01 sec) SQL Statement executed successfully . Use DESC Statement view view_create_replace View information .
mysql> DESC view_create_replace; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | category | varchar(30) | NO | | | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) The results show that view_create_replace Field information of the view .
Create name as view_name_price The view of , Because I created view_name_price View , therefore , Use CREATE OR REPLACE VIEW The statement will be modified view_name_price Structure of view . First , see view_name_price View information .
mysql> DESC view_name_price; +---------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+---------+-------+ | t_name | varchar(50) | YES | | | | | t_price | decimal(10,2) | YES | | 0.00 | | +---------+---------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) here ,view_name_price The view contains only t_name and t_price Two fields .
Use CREATE OR REPLACE VIEW Statement modification view_name_price Structure of view .
mysql> CREATE OR REPLACE VIEW view_name_price -> (category, name, price) -> AS -> SELECT category.t_category, goods.t_name, goods.t_price -> FROM t_goods_category category -> INNER JOIN -> t_goods goods -> ON category.id = goods.t_category_id; Query OK, 0 rows affected (0.00 sec) SQL Statement executed successfully . Look again view_name_price Structure of view .
mysql> DESC view_name_price; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | category | varchar(30) | NO | | | | | name | varchar(50) | YES | | | | | price | decimal(10,2) | YES | | 0.00 | | +----------+---------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) here ,view_name_price The view contains category、name and price Three fields , Description successfully modified view_name_price Structure of view .
15.4.2 Use ALTER Statement to modify the view structure
Use ALTER Statement to modify the view structure , The syntax is as follows :
ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = user] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION] From the perspective of grammatical format , except ALTER Out of statement , Other information is in the same syntax format as when creating views , I won't repeat .
for example , Use ALTER Statement modification view_goods_consition Structure of view . First , see view_goods_consition Structure of view .
mysql> DESC view_goods_consition; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | name | varchar(50) | YES | | | | | price | decimal(10,2) | YES | | 0.00 | | +----------+---------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) here ,view_goods_consition There are name and price Two fields . Use ALTER Statement modification view_goods_consition Structure of view .
mysql> ALTER VIEW view_goods_consition AS -> SELECT * FROM t_goods -> WHERE id BETWEEN 1 AND 3; Query OK, 0 rows affected (0.01 sec) SQL Statement executed successfully , see view_goods_consition Structure of view .
mysql> DESC view_goods_consition; +---------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | t_category_id | int(11) | YES | | 0 | | | t_category | varchar(30) | YES | | | | | t_name | varchar(50) | YES | | | | | t_price | decimal(10,2) | YES | | 0.00 | | | t_stock | int(11) | YES | | 0 | | | t_upper_time | datetime | YES | | NULL | | +---------------+---------------+------+-----+---------+-------+ 7 rows in set (0.01 sec) view_goods_consition The structure of the view has changed , Instruction use ALTER Statement successfully modified the structure information of the view .
15.5 Update data for view
MySQL Support use INSERT、UPDATE and DELETE Statement to insert data in a view 、 Update and delete operations . When the data in the view changes , The data in the data table will also change , vice versa .
15.5.1 Update view data directly
Create a view_category The view of .
mysql> CREATE VIEW view_category -> AS -> SELECT * FROM t_goods_category; Query OK, 0 rows affected (0.00 sec) see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | +----+---------------------+ 4 rows in set (0.00 sec) see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | +----+---------------------+ 4 rows in set (0.00 sec) adopt INSERT、UPDATE and DELETE The statement of view_category The data in the view is inserted 、 Update and delete operations .
1. Insert data into the view
towards view_category Insert data into the view .
mysql> INSERT INTO view_category(id, t_category) VALUES (5, ' Fruits '); Query OK, 1 row affected (0.01 sec) SQL Statement executed successfully , see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | Fruits | +----+---------------------+ 5 rows in set (0.00 sec) here ,view_category A new item is added in the view id by 5 The data of .
see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | Fruits | +----+---------------------+ 5 rows in set (0.00 sec) here ,t_goods_category A synchronized entry has been added to the data table id by 5 The data of .
2. Update the data in the view
for example , take view_category In the view id by 5 Data. t_category The field value is updated to “ The book ”.
mysql> UPDATE view_category SET t_category = ' The book ' WHERE id = 5; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | The book | +----+---------------------+ 5 rows in set (0.00 sec) view_category In the view id by 5 The data of is updated to “ The book ” Category .
see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | The book | +----+---------------------+ 5 rows in set (0.00 sec) here ,t_goods_category In the data table id by 5 The data of has been synchronously modified to “ The book ”.
3. Delete data from view
for example , Delete view_category In the view id by 5 The data of .
mysql> DELETE FROM view_category WHERE id = 5; Query OK, 1 row affected (0.00 sec) SQL Statement executed successfully . see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | +----+---------------------+ 4 rows in set (0.00 sec) here ,view_category In the view id by 5 Your data has been deleted .
see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | +----+---------------------+ 4 rows in set (0.00 sec) You can see ,t_goods_category In the data table id by 5 The data of has been deleted synchronously .
15.5.2 Update view data indirectly
Indirectly updating view data is to update view data by updating the data in the data table .
1. Insert data into the data table
for example , towards t_goods_category Insert a line into the data table id by 5、t_category by “ Electronic equipment ” The record of .
mysql> INSERT INTO t_goods_category(id, t_category) VALUES (5, ' Electronic equipment '); Query OK, 1 row affected (0.00 sec) SQL Statement executed successfully . see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | Electronic equipment | +----+---------------------+ 5 rows in set (0.00 sec) here ,t_goods_category Successfully inserted a entry in the data table id by 5 Data record of .
see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | Electronic equipment | +----+---------------------+ 5 rows in set (0.00 sec) You can see ,view_category View has been synchronously added id by 5 Data record of .
2. Update the data in the data table
for example , take t_goods_category In the data table id by 5 The data for is updated to “ Vehicle accessories ”.
mysql> UPDATE t_goods_category SET t_category = ' Vehicle accessories ' WHERE id = 5; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 SQL Statement executed successfully . see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | Vehicle accessories | +----+---------------------+ 5 rows in set (0.00 sec) here ,t_goods_category In the data table id by 5 The data of is modified to “ Vehicle accessories ”.
see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | | 5 | Vehicle accessories | +----+---------------------+ 5 rows in set (0.00 sec) You can see ,view_category In the view id by 5 The data of is synchronously modified to “ Vehicle accessories ”.
3. Delete data from data table
Delete t_goods_category In the data table id by 5 The data of .
mysql> DELETE FROM t_goods_category WHERE id = 5; Query OK, 1 row affected (0.00 sec) SQL Statement executed successfully . see t_goods_category Data in the data table .
mysql> SELECT * FROM t_goods_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | +----+---------------------+ 4 rows in set (0.00 sec) here ,t_goods_category In the data table id by 5 Your data has been deleted .
see view_category Data in view .
mysql> SELECT * FROM view_category; +----+---------------------+ | id | t_category | +----+---------------------+ | 1 | Women's wear / Women's Boutique | | 2 | Outdoor sports | | 3 | men's wear | | 4 | Children's wear | +----+---------------------+ 4 rows in set (0.00 sec) You can see ,view_category In the view id by 5 The data of has been deleted synchronously .
15.6 Delete view
When the database no longer needs views , You can delete the view . The syntax format of deleting a view is as follows :
DROP VIEW [IF EXISTS] view_name [, view_name] ... [RESTRICT | CASCADE] for example , Delete the name as view_category The view of .
mysql> DROP VIEW view_category; Query OK, 0 rows affected (0.00 sec) see view_category Structure of view .
mysql> DESC view_category; ERROR 1146 (42S02): Table 'goods.view_category' doesn't exist 边栏推荐
- HTAP是有代价的
- .eslintrc.js配置说明
- 2、 Container_
- 儿童玩乐场所如何运营?
- 敲黑板画重点:七种常见“分布式事务”详解
- LCD笔记(4)分析内核自带的LCD驱动程序
- 数字化时代,是什么“黄金宝藏”在推动百年药企发展?
- Use the jsonobject object in fastjason to simplify post request parameter passing
- How much do you know about the two infrastructures of the badminton stadium?
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)
猜你喜欢
随机推荐
Ds-24c/dc220v time relay
敲黑板画重点:七种常见“分布式事务”详解
Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
FPGA入门学习(一) - 第一个FPGA工程
Some common writing methods and skills
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
Pytoch deep learning quick start tutorial -- mound tutorial notes (I)
华为超融合FusionCube解决方案笔记
Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)
uniapp h5、app引用外部在线js
Introduction to FPGA (I) - the first FPGA project
Ds-112 time relay
Removable tablespace
尤雨溪向初学者推荐Vite 【为什么使用Vite】
Optical distance sensing chip 4530a combining ambient light, proximity sensing and infrared ranging
Introduction to FPGA (II) - one out of two selector
Flutter JNI confusion introduction.So file release package flash back
Backtracking - question 51 Queen n -- a classic backtracking problem that must be overcome
数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
行业案例|指标中台如何助力银行业普惠金融可持续发展









