0
点赞
收藏
分享

微信扫一扫

密码找回安全总结-业务安全测试实操(29)

小美人鱼失去的腿 2023-07-05 阅读 69
数据库

目录

🏀什么是视图

🏀视图用途

🏀视图的优点

🏀视图缺点

🏀创建视图

🏀 创建单表视图

🏀 查看

🏀创建多表视图 

🏀 查看视图

🏀Describe 视图名

🏀 基本信息

🏀详细信息  

🏀views查询

 修改视图

Create or replace view

 Alter 修改视图

 更新视图

删除视图

 


什么是视图

视图用途

视图的优点

视图缺点

创建视图

 创建单表视图

mysql> create table t(quantity int, price int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t values (3,50);
Query OK, 1 row affected (0.04 sec)

mysql> create view view_t as select quantity,price,quantity*price from t;
Query OK, 0 rows affected (0.00 sec)

 查看

mysql> select * from view_t;
+----------+-------+----------------+
| quantity | price | quantity*price |
+----------+-------+----------------+
|        3 |    50 |            150 |
+----------+-------+----------------+
1 row in set (0.00 sec)

mysql> CREATE VIEW view_t2(qty,price,total) AS SELECT quantity,price,quantity*price FROM t;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    3 |    50 |   150 |
+------+-------+-------+
1 row in set (0.00 sec)

创建多表视图 

mysql> create table student
    -> (
    -> s_id int(3) primary key,
    -> s_name varchar(30),
    -> s_age int(3),
    -> s_sex varchar(8)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> create table stu_info
    -> (
    -> s_id int(3),
    -> class varchar(50),
    -> addr varchar(100)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into stu_info
    -> (s_id,class,addr)
    -> values
    -> (1,'erban','anhui'),
    -> (2,'sanban','chongqing'),
    -> (3,'yiban','shandong');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> CREATE VIEW stu_class(id,name,class) AS SELECT
    -> student.s_id,student.s_name,stu_info.class
    -> from student,stu_info WHERE student.s_id=stu_info.s_id;
Query OK, 0 rows affected (0.00 sec)

 查看视图

Describe 视图名

mysql> desc stu_class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(3)      | NO   |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
| class | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

 基本信息

mysql> show table status like 'stu_class'\G
*************************** 1. row ***************************
           Name: stu_class
         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: NULL
    Update_time: NULL
     Check_time: NULL
      Collation: NULL
       Checksum: NULL
 Create_options: NULL
        Comment: VIEW
1 row in set (0.00 sec)

详细信息  

mysql> SHOW CREATE VIEW view_t\G
*************************** 1. row ***************************
                View: view_t
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_t` AS select `t`.`quantity` AS `quantity`,`t`.`price` AS `price`,(`t`.`quantity` * `t`.`price`) AS `quantity*price` from `t`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

mysql> 

views查询

mysql> select * from information_schema.views\G
*************************** 1. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: li
          TABLE_NAME: stu_class
     VIEW_DEFINITION: select `li`.`student`.`s_id` AS `id`,`li`.`student`.`s_name` AS `name`,`li`.`stu_info`.`class` AS `class` from `li`.`student` join `li`.`stu_info` where (`li`.`student`.`s_id` = `li`.`stu_info`.`s_id`)
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 2. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: li
          TABLE_NAME: view_t
     VIEW_DEFINITION: select `li`.`t`.`quantity` AS `quantity`,`li`.`t`.`price` AS `price`,(`li`.`t`.`quantity` * `li`.`t`.`price`) AS `quantity*price` from `li`.`t`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 3. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: li
          TABLE_NAME: view_t2
     VIEW_DEFINITION: select `li`.`t`.`quantity` AS `qty`,`li`.`t`.`price` AS `price`,(`li`.`t`.`quantity` * `li`.`t`.`price`) AS `total` from `li`.`t`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 4. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: sys
          TABLE_NAME: host_summary
     VIEW_DEFINITION: select if(isnull(`performance_schema`.`accounts`.`HOST`),'background',`performance_schema`.`accounts`.`HOST`) AS `host`,sum(`stmt`.`total`) AS `statements`,`sys`.`format_time`(sum(`stmt`.`total_latency`)) AS `statement_latency`,`sys`.`format_time`(ifnull((sum(`stmt`.`total_latency`) / nullif(sum(`stmt`.`total`),0)),0)) AS `statement_avg_latency`,sum(`stmt`.`full_scans`) AS `table_scans`,sum(`io`.`ios`) AS `file_ios`,`sys`.`format_time`(sum(`io`.`io_latency`)) AS `file_io_latency`,sum(`performance_schema`.`accounts`.`CURRENT_CONNECTIONS`) AS `current_connections`,sum(`performance_schema`.`accounts`.`TOTAL_CONNECTIONS`) AS `total_connections`,count(distinct `performance_schema`.`accounts`.`USER`) AS `unique_users`,`sys`.`format_bytes`(sum(`mem`.`current_allocated`)) AS `current_memory`,`sys`.`format_bytes`(sum(`mem`.`total_allocated`)) AS `total_memory_allocated` from (((`performance_schema`.`accounts` join `sys`.`x$host_summary_by_statement_latency` `stmt` on((`performance_schema`.`accounts`.`HOST` = `stmt`.`host`))) join `sys`.`x$host_summary_by_file_io` `io` on((`performance_schema`.`accounts`.`HOST` = `io`.`host`))) join `sys`.`x$memory_by_host_by_current_bytes` `mem` on((`performance_schema`.`accounts`.`HOST` = `mem`.`host`))) group by if(isnull(`performance_schema`.`accounts`.`HOST`),'background',`performance_schema`.`accounts`.`HOST`)
        CHECK_OPTION: NONE
        IS_UPDATABLE: NO
             DEFINER: mysql.sys@localhost
       SECURITY_TYPE: INVOKER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 5. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: sys
          TABLE_NAME: host_summary_by_file_io
     VIEW_DEFINITION: select if(isnull(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`),'background',`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`) AS `host`,sum(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`COUNT_STAR`) AS `ios`,`sys`.`format_time`(sum(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`SUM_TIMER_WAIT`)) AS `io_latency` from `performance_schema`.`events_waits_summary_by_host_by_event_name` where (`performance_schema`.`events_waits_summary_by_host_by_event_name`.`EVENT_NAME` like 'wait/io/file/%') group by if(isnull(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`),'background',`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`) order by sum(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`SUM_TIMER_WAIT`) desc
        CHECK_OPTION: NONE
        IS_UPDATABLE: NO
             DEFINER: mysql.sys@localhost
       SECURITY_TYPE: INVOKER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
*************************** 6. row ***************************
       TABLE_CATALOG: def

 修改视图

Create or replace view

mysql> CREATE OR REPLACE VIEW view_t AS SELECT * FROM t;
Query OK, 0 rows affected (0.00 sec)

mysql> DESC view_t;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
| price    | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 Alter 修改视图

mysql> ALTER VIEW view_t AS SELECT quantity FROM t ;
Query OK, 0 rows affected (0.01 sec)

mysql> DESC view_t;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

 更新视图

mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> select * from view_t;
+----------+
| quantity |
+----------+
|        3 |
+----------+
1 row in set (0.01 sec)

mysql> UPDATE view_t SET quantity=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from view_t;
+----------+
| quantity |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> INSERT INTO t VALUES(3,5);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
|        3 |     5 |
+----------+-------+
2 rows in set (0.00 sec)

mysql> select * from view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
|    3 |     5 |    15 |
+------+-------+-------+
2 rows in set (0.00 sec)

mysql> select * from view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
|    3 |     5 |    15 |
+------+-------+-------+
2 rows in set (0.00 sec)

mysql> DELETE FROM view_t2 WHERE price=5;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
+------+-------+-------+
1 row in set (0.00 sec)

mysql> 

删除视图

mysql> DROP VIEW IF EXISTS stu_class;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP VIEW IF EXISTS stu_class;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------+
| Level | Code | Message                      |
+-------+------+------------------------------+
| Note  | 1051 | Unknown table 'li.stu_class' |
+-------+------+------------------------------+
1 row in set (0.00 sec)

mysql> SHOW CREATE VIEW stu_class;
ERROR 1146 (42S02): Table 'li.stu_class' doesn't exist
mysql> 

 

 

 

 

 

 

 

 

 

 

 

 

举报

相关推荐

0 条评论