zoukankan      html  css  js  c++  java
  • mysql 视图

     创建视图

    mysql> select * from test;
    +----+------------+-------+-----------+
    | id | name       | score | subject   |
    +----+------------+-------+-----------+
    |  1 | xiaoming   |    89 | shuxue    |
    |  2 | xiaohong   |    89 | shuxue    |
    |  3 | xiaohong   |    80 | english   |
    |  4 | xiaohong   |    80 | physics   |
    |  5 | xiaohong   |    80 | astronaut |
    |  6 | xiaoming   |    80 | physics   |
    |  7 | xiaoming   |    80 | astronaut |
    |  8 | xiaoming   |    80 | english   |
    |  9 | xiaobai    |    80 | astronaut |
    | 10 | 1.2xiaobai |    80 | english   |
    | 11 | 2.2xiaobai |    80 | physics   |
    | 12 | 3xiaobai   |    80 | shuxue    |
    | 13 | 123xiaohei |    80 | astronaut |
    | 14 | xiaohei    |    80 | shuxue    |
    | 15 | xiaohei    |    80 | physics   |
    | 16 | .12xiaohei |    80 | english   |
    +----+------------+-------+-----------+
    16 rows in set (0.00 sec)
    
    
    
    mysql> select * from user;
    +------+----------+-----------+
    | id   | name     | address   |
    +------+----------+-----------+
    |    2 | xiaobai  | shandong  |
    |    3 | xiaohong | suzhou    |
    |    4 | xiaohei  | changchun |
    |    1 | xiaoming | beijing   |
    +------+----------+-----------+
    4 rows in set (0.00 sec)
    
    
    
    mysql> select test.name,score,subject,address from test,user where user.name=test.name;
    +----------+-------+-----------+-----------+
    | name     | score | subject   | address   |
    +----------+-------+-----------+-----------+
    | xiaoming |    89 | shuxue    | beijing   |
    | xiaohong |    89 | shuxue    | suzhou    |
    | xiaohong |    80 | english   | suzhou    |
    | xiaohong |    80 | physics   | suzhou    |
    | xiaohong |    80 | astronaut | suzhou    |
    | xiaoming |    80 | physics   | beijing   |
    | xiaoming |    80 | astronaut | beijing   |
    | xiaoming |    80 | english   | beijing   |
    | xiaobai  |    80 | astronaut | shandong  |
    | xiaohei  |    80 | shuxue    | changchun |
    | xiaohei  |    80 | physics   | changchun |
    +----------+-------+-----------+-----------+
    11 rows in set (0.00 sec)
    
    
    
    mysql> create view testview as (select test.name,score,subject,address from test,user where user.name=test.name);
    Query OK, 0 rows affected (0.01 sec)
    
    
    
    mysql> select * from testview;
    +----------+-------+-----------+-----------+
    | name     | score | subject   | address   |
    +----------+-------+-----------+-----------+
    | xiaoming |    89 | shuxue    | beijing   |
    | xiaohong |    89 | shuxue    | suzhou    |
    | xiaohong |    80 | english   | suzhou    |
    | xiaohong |    80 | physics   | suzhou    |
    | xiaohong |    80 | astronaut | suzhou    |
    | xiaoming |    80 | physics   | beijing   |
    | xiaoming |    80 | astronaut | beijing   |
    | xiaoming |    80 | english   | beijing   |
    | xiaobai  |    80 | astronaut | shandong  |
    | xiaohei  |    80 | shuxue    | changchun |
    | xiaohei  |    80 | physics   | changchun |
    +----------+-------+-----------+-----------+
    11 rows in set (0.00 sec)

     查看视图

    mysql> show create view testview;
    +----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
    | View     | Create View                                                                                                                                                                                                                                                                  | character_set_client | collation_connection |
    +----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
    | testview | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `testview` AS (select `test`.`name` AS `name`,`test`.`score` AS `score`,`test`.`subject` AS `subject`,`user`.`address` AS `address` from (`test` join `user`) where (`user`.`name` = `test`.`name`)) | utf8                 | utf8_general_ci      |
    +----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
    1 row in set (0.00 sec)

    删除视图

    mysql> drop view testview;
    Query OK, 0 rows affected (0.00 sec)

     参考:

    https://www.2cto.com/database/201803/726747.html

  • 相关阅读:
    企业库应用实践系列五:创建模板引擎Library
    关于HtmlHelper:是MVC自作聪明,还是我不够明白?
    企业库应用实践系列二:对象创建原理详解
    企业库应用实践系列三:自定义构造函数
    专业导论 计算机科学概论
    企业短期项目,缺人手
    光脚丫学LINQ(040):引发未将对象引用设置到对象的实例的异常
    光脚丫学LINQ(045):如何表示计算所得列(LINQ to SQL)
    光脚丫学LINQ(042):如何将列表示为由数据库生成的列
    光脚丫学LINQ(043):为实体类的列成员指定在数据库中的数据类型
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11314779.html
Copyright © 2011-2022 走看看